MFC tank positioning

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.