MFC创建窗口及图片动态显示C++

#include <afxwin.h>
class CMyWnd :public CFrameWnd {
private:
	CDC * cdc;
	CBitmap *bmp;
	CBitmap *bmpBack;
	int order = 0;
	int pos = 500;
public:
	CMyWnd() {
		
		Create(NULL, "我的窗口");
		CClientDC dc(this);
		cdc = new CDC;
		cdc->CreateCompatibleDC(&dc);
		bmpBack = new CBitmap;
		bmpBack->m_hObject = LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
		bmp = new CBitmap;
		bmp->m_hObject = LoadImage(NULL,"boy.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
		SetTimer(1, 100, NULL);
	};
	DECLARE_MESSAGE_MAP()
	afx_msg void OnPaint();
	
	afx_msg void OnTimer(UINT_PTR nIDEvent);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};

class CMyApp :public CWinApp {
	BOOL InitInstance();

};
BOOL CMyApp::InitInstance() {
	CMyWnd *pf = new CMyWnd();
	
	pf->ShowWindow(m_nCmdShow);
	pf->UpdateWindow();
	this->m_pMainWnd = pf;
	
	return TRUE;
}

CMyApp myApp;BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_TIMER()
ON_WM_KEYDOWN()
END_MESSAGE_MAP()


void CMyWnd::OnPaint()
{
	CPaintDC dc(this); 
	
}




void CMyWnd::OnTimer(UINT_PTR nIDEvent)
{
	CClientDC dc(this);
	cdc->SelectObject(bmpBack);
	dc.BitBlt(0, 0, 800, 600, cdc, 0, 0, SRCCOPY);
	cdc->SelectObject(bmp);
	switch (order)
	{
	case 0:
		dc.BitBlt(pos, 250, 63, 154, cdc, 63, 0, SRCAND);
		dc.BitBlt(pos, 250, 63, 154, cdc, 0, 0, SRCPAINT);
		break;
	case 1:
		dc.BitBlt(pos, 250, 63, 154, cdc, 63, 154, SRCAND);
		dc.BitBlt(pos, 250, 63, 154, cdc, 0, 154, SRCPAINT);
		break;
	default:
		
		break;
	}
	order = (order == 0 ? 1 : 0);
	pos -= 10;
	CFrameWnd::OnTimer(nIDEvent);
}


void CMyWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nChar == VK_RIGHT) {
		pos += 10;
	}
	if (nChar == VK_LEFT) {
		pos -= 10;
	}
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

虽然现在游戏引擎做游戏很方便,我们使用mfc做一些东西对游戏,windows窗口的理解很有帮助。

猜你喜欢

转载自blog.csdn.net/weixin_42927264/article/details/83410840
今日推荐