I recently learned about MFC and wrote a tank interface that is moved from top to bottom on the keyboard ,:
Use VC ++ to create a simple MFC project.
1. Add tank image resources: omitted
2. Add three variables: x, y, m_bitmap
3. Load bitmap in the initialization method and set the initial coordinates:
->m_bitmap.LoadBitmapW(IDB_BITMAP2); ->m_bitmap.GetBitmap(&bmpInfo); ->GetWindowRect(&r); ->x=(r.right-r.left)/-bmpInfo.bmWidth/; ->y=(r.bottom-r.top)/-bmpInfo.bmHeight/;
4. Rewrite the OnPaint function:
CClientDC d(&&->m_bitmap.GetBitmap(&->x, ->y, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, , , SRCCOPY);
5. Mount the keyboard event:
->x-=->x+=->y-=->y+=->InvalidateRect(NULL);
6. Due to the event mechanism in Dialog, you need to add a message preprocessing function to make the program truly receive the keydown event, as shown below:
BOOL Ctest3Dlg::PreTranslateMessage(MSG* (pMsg->message ==->wParam, pMsg->
The above is relatively simple, and event association is not written in, but it is not written because of the help of the UI wizard. For example:
Haha, now the tank can be controlled up and down.