The huge difference between the first call to Cdialog::onpaint () and the Last Call to Cdialog::onpaint () in the MFC OnPaint () function

Source: Internet
Author: User
Tags bmp image

The big difference between the first call to Cdialog::onpaint () and the Last Call to Cdialog::onpaint () in the OnPaint () function is that if you do not pay attention to this problem there will be an unreasonable drawing problem-the effect is not coming out! After two hours of torture, the problem lies in the following lessons:

the Cdialog::onpaint () function is called first in the OnPaint () function:

[CPP]View PlainCopy 
  1. Add a bitmap background to the main form client area
  2. void Cbackimagedlg::onpaint ()
  3. {
  4. Cdialog::onpaint () //First Call
  5. CPaintDC DC (this); //device context for painting
  6. CRect rect;
  7. GetClientRect (&rect);
  8. CDC Dcmem; //Define a Toolbox (device context)
  9. Dcmem.createcompatibledc (&DC); /// establish an associated DC
  10. CBitmap Bmpbackground; //Bitmap Object
  11. Bmpbackground.loadbitmap (IDB_BITMAP1); //idb_bitmap is your own figure corresponding to the ID
  12. BITMAP BITMAP;
  13. Bmpbackground.getbitmap (&BITMAP); //Establish a binding relationship
  14. CBitmap *pbmpold=dcmem.selectobject (&bmpbackground); //Save the original CDC object and select the new CDC object into the DC
  15. dc. Setstretchbltmode (Coloroncolor); //Prevent BMP image distortion
  16. dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0, bitmap.bmwidth,bitmap.bmheight,srccopy);
  17. Dcmem.selectobject (Pbmpold);
  18. Bmpbackground.deleteobject ();
  19. Dcmem.deletedc ();
  20. }

Running effect: Background bitmap doesn't come out, it's hard to detect what's wrong

The last call to the Cdialog::onpaint () function in the OnPaint () function:

[CPP]View PlainCopy  
  1. void Cbackimagedlg::onpaint ()
  2. {
  3. CPaintDC DC (this); //device context for painting
  4. CRect rect;
  5. GetClientRect (&rect);
  6. CDC Dcmem; //Define a Toolbox (device context)
  7. Dcmem.createcompatibledc (&DC); /// establish an associated DC
  8. CBitmap Bmpbackground; //Bitmap Object
  9. Bmpbackground.loadbitmap (IDB_BITMAP1); //idb_bitmap is your own figure corresponding to the ID
  10. BITMAP BITMAP;
  11. Bmpbackground.getbitmap (&BITMAP); //Establish a binding relationship
  12. CBitmap *pbmpold=dcmem.selectobject (&bmpbackground); //Save the original CDC object and select the new CDC object into the DC
  13. dc. Setstretchbltmode (Coloroncolor); //Prevent BMP image distortion
  14. dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0, bitmap.bmwidth,bitmap.bmheight,srccopy);
  15. Dcmem.selectobject (Pbmpold);
  16. Bmpbackground.deleteobject ();
  17. Dcmem.deletedc ();
  18. Cdialog::onpaint (); //Last Call
  19. }


Run Effect: Background bitmap effect came out

Summarize:

A code, two positions, two effects! If you are not careful, this error is imperceptible, I used to draw for the client area has been very smooth, but today careless to make this mistake, I spent 2.5 hours to check my code (this project has more than 2000 lines of code!) Start guessing is caused by interference between the code, and finally notice that the position of Cdialog::onpaint () in the OnPaint () function is misplaced! Hey.

Leave a little notes and remind yourself!

http://blog.csdn.net/qq2399431200/article/details/17739087

The huge difference between the first call to Cdialog::onpaint () and the Last Call to Cdialog::onpaint () in the MFC OnPaint () function

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.