DWORD WINAPI play(LPVOID lpParam) { CMapTestDlg *c=(CMapTestDlg*)lpParam; HWND hWnd= c->GetSafeHwnd(); c->hdc = ::GetDC(hWnd); //mdc = CreateCompatibleDC(hdc); c->bufdc = CreateCompatibleDC(c->hdc); CBitmap bTank; bTank.LoadBitmap(IDB_BITMAP1); c->bird=HBITMAP(bTank); SelectObject(c->bufdc,c->bird); while(true) { //BitBlt(mdc,10,0,50,50,bufdc,0,0,SRCCOPY); BitBlt(c->hdc,c->x,c->y,50,50,c->bufdc,0,0,SRCCOPY); ::Sleep(1); } return TRUE; }
BOOL CMapTestDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg-> message == WM_KEYDOWN) { if(pMsg-> wParam== VK_UP) { y-=10; } if(pMsg-> wParam== VK_DOWN) { y+=10; } if(pMsg-> wParam== VK_LEFT) { x-=10;; } if(pMsg-> wParam== VK_RIGHT) { x+=10; } } return CDialog::PreTranslateMessage(pMsg); }
线程中每Sleep(1)发送一个消息到UI线程,在UI线程中修改变量的值,然后调用InvalidateRect刷新,OnPaint函数中绘制图形即可。建议用双缓冲绘图。