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
- Add a bitmap background to the main form client area
- void Cbackimagedlg::onpaint ()
- {
- Cdialog::onpaint () //First Call
- CPaintDC DC (this); //device context for painting
- CRect rect;
- GetClientRect (&rect);
- CDC Dcmem; //Define a Toolbox (device context)
- Dcmem.createcompatibledc (&DC); /// establish an associated DC
- CBitmap Bmpbackground; //Bitmap Object
- Bmpbackground.loadbitmap (IDB_BITMAP1); //idb_bitmap is your own figure corresponding to the ID
- BITMAP BITMAP;
- Bmpbackground.getbitmap (&BITMAP); //Establish a binding relationship
- CBitmap *pbmpold=dcmem.selectobject (&bmpbackground); //Save the original CDC object and select the new CDC object into the DC
- dc. Setstretchbltmode (Coloroncolor); //Prevent BMP image distortion
- dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0, bitmap.bmwidth,bitmap.bmheight,srccopy);
- Dcmem.selectobject (Pbmpold);
- Bmpbackground.deleteobject ();
- Dcmem.deletedc ();
- }
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
- void Cbackimagedlg::onpaint ()
- {
- CPaintDC DC (this); //device context for painting
- CRect rect;
- GetClientRect (&rect);
- CDC Dcmem; //Define a Toolbox (device context)
- Dcmem.createcompatibledc (&DC); /// establish an associated DC
- CBitmap Bmpbackground; //Bitmap Object
- Bmpbackground.loadbitmap (IDB_BITMAP1); //idb_bitmap is your own figure corresponding to the ID
- BITMAP BITMAP;
- Bmpbackground.getbitmap (&BITMAP); //Establish a binding relationship
- CBitmap *pbmpold=dcmem.selectobject (&bmpbackground); //Save the original CDC object and select the new CDC object into the DC
- dc. Setstretchbltmode (Coloroncolor); //Prevent BMP image distortion
- dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0, bitmap.bmwidth,bitmap.bmheight,srccopy);
- Dcmem.selectobject (Pbmpold);
- Bmpbackground.deleteobject ();
- Dcmem.deletedc ();
- Cdialog::onpaint (); //Last Call
- }
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