⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 powerstatectrl.cpp

📁 电机控制发系统
💻 CPP
字号:
// PowerStateCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "PowerStateCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPowerStateCtrl

CPowerStateCtrl::CPowerStateCtrl()
{
	m_hPowerDC1 = NULL;
	m_hPowerDC2 = NULL;
	m_hPowerDC3 = NULL;
	m_hPowerDC4 = NULL;
	m_hMemDC = NULL;
	m_bIsPowering = TRUE;
	m_nControl = 0;
	m_cxAngle = 0;
	m_cyAngle = 0;
}

CPowerStateCtrl::~CPowerStateCtrl()
{
	if (m_hPowerDC1 != NULL)
		DeleteDC(m_hPowerDC1);
	if (m_hPowerDC2 != NULL)
		DeleteDC(m_hPowerDC2);
	if (m_hPowerDC3 != NULL)
		DeleteDC(m_hPowerDC3);
	if (m_hPowerDC4 != NULL)
		DeleteDC(m_hPowerDC4);
	if (m_hMemDC != NULL)
		DeleteDC(m_hMemDC);
}


BEGIN_MESSAGE_MAP(CPowerStateCtrl, CStatic)
	//{{AFX_MSG_MAP(CPowerStateCtrl)
	ON_WM_CREATE()
	ON_WM_ERASEBKGND()
	ON_WM_PAINT()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPowerStateCtrl message handlers



int CPowerStateCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CStatic::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	Initialize();

	return 0;
}

void CPowerStateCtrl::Initialize()
{
	CDC *pDC = GetDC();

	// Load powerstate picture1 and creates background DC
	LoadPicture(IDB_STATE01, m_hPowerDC1, m_cxAngle, m_cyAngle, pDC->m_hDC); 

	// Load powerstate picture2 and creates background DC
	LoadPicture(IDB_STATE02, m_hPowerDC2, m_cxAngle, m_cyAngle, pDC->m_hDC);

	// Load powerstate picture3 and creates background DC
	LoadPicture(IDB_STATE03, m_hPowerDC3, m_cxAngle, m_cyAngle, pDC->m_hDC); 

	// Load powerstate picture4 and creates background DC
	LoadPicture(IDB_STATE04, m_hPowerDC4, m_cxAngle, m_cyAngle, pDC->m_hDC);

	// create work area
	LoadPicture(0, m_hMemDC, m_cxAngle, m_cyAngle, pDC->m_hDC);

	ReleaseDC(pDC);

	SetTimer(1,40,NULL);
}

void CPowerStateCtrl::LoadPicture(int nResourceID, HDC &hDestinationDC, int &nWidth, int &nHeight, HDC hDC)
{
	HDC hMemDC;
	HDC hdcCompatible;
	HBITMAP hbmScreen;
 
	if (nResourceID != 0)
	{
		// if resourceid is given, load bitmap
		HBITMAP hPicture = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(nResourceID));
		
		BITMAP bm;
		GetObject(hPicture, sizeof (BITMAP), (LPSTR)&bm);

		hMemDC = CreateCompatibleDC(hDC);
		
		HBITMAP hOldBMP = (HBITMAP)SelectObject(hMemDC, hPicture);

		nWidth = bm.bmWidth;
		nHeight = bm.bmHeight;  
		
		// Create the DC
		hdcCompatible = CreateCompatibleDC(hDC);    
		// Temporary memory bitmap
		hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);

		// select bitmap into dc
		if (SelectObject(hdcCompatible, hbmScreen) == NULL)
		{
			// return null
			hDestinationDC = NULL;                                        
		}
		else 
		{
			// return the DC
			hDestinationDC = hdcCompatible;
		}
				
		if (hDestinationDC)
			BitBlt(hDestinationDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);

		SelectObject(hMemDC, hOldBMP);

		// Release temporary stuff
		DeleteDC(hMemDC);
		DeleteObject(hbmScreen);
		DeleteObject(hPicture);
	}
	else	// if no resourceid is given, create empty DC with specified width and height
	{
		// create the DC
		hdcCompatible = CreateCompatibleDC(hDC);			
		// temporary memory bitmap
		hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);		
		// if the function fails
		if (SelectObject(hdcCompatible, hbmScreen) == NULL)
		{
			// return null
			hDestinationDC = NULL;
		}
		else
		{
			 // if it succeeds, return the DC
			hDestinationDC = hdcCompatible;                                     
		}
		DeleteObject(hbmScreen);
	}
}

BOOL CPowerStateCtrl::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CStatic::OnEraseBkgnd(pDC);
}

void CPowerStateCtrl::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	// TODO: Add your message handler code here
	if(m_bIsPowering)
	{
		if(m_nControl == 0)
			BitBlt(m_hMemDC, 0, 0, m_cxAngle, m_cyAngle, m_hPowerDC1, 0, 0, SRCCOPY);
		if(m_nControl == 1)
			BitBlt(m_hMemDC, 0, 0, m_cxAngle, m_cyAngle, m_hPowerDC2, 0, 0, SRCCOPY);
		if(m_nControl == 2)
			BitBlt(m_hMemDC, 0, 0, m_cxAngle, m_cyAngle, m_hPowerDC3, 0, 0, SRCCOPY);
		if(m_nControl == 3)
			BitBlt(m_hMemDC, 0, 0, m_cxAngle, m_cyAngle, m_hPowerDC4, 0, 0, SRCCOPY);
	} 
	else
		BitBlt(m_hMemDC, 0, 0, m_cxAngle, m_cyAngle, m_hPowerDC3, 0, 0, SRCCOPY);

	// and finally, copy memory bitmap to screen
	BitBlt(dc.m_hDC, 0, 0, m_cxAngle, m_cyAngle, m_hMemDC, 0, 0, SRCCOPY);
	// Do not call CStatic::OnPaint() for painting messages
}

void CPowerStateCtrl::SetPowerCtrlState(BOOL bState)
{
	m_bIsPowering = bState;
	Invalidate();
}

BOOL CPowerStateCtrl::GetPowerCtrlState()
{
	return m_bIsPowering;
}

void CPowerStateCtrl::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	m_nControl ++;

	if(m_nControl > 3)
		m_nControl = 0;

	Invalidate();

	CStatic::OnTimer(nIDEvent);
}

void CPowerStateCtrl::SetCtrlTimer(int nTime)
{
	KillTimer(1);
	SetTimer(1,nTime,NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -