WM_PAINT adding Message handler functions
void Cdialog6::onpaint ()
{
CPAINTDC DC (this);//device context for painting
CRect rect;
GetClientRect (&rect);
CDC Dcmem;
Dcmem.createcompatibledc (&DC);
CBitmap Bmpbackground;
Bmpbackground.loadbitmap (IDB_BITMAP2); Idb_bitmap is your own figure corresponding to the ID BITMAP BITMAP;
Bmpbackground.getbitmap (&BITMAP); CBitmap *pbmpold=dcmem.selectobject (&bmpbackground); dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0,bitmap.bmwidth,bitmap.bmheight,srccopy);
Cdialog::onpaint () for painting messages
}
Method One:
StretchBlt in the OnPaint
In particular: Comment out cdialog::onpaint () or put it to the end (why?). ), and add the decal code
- void Cqqqqqdlg::onpaint ()
- {
- if (Isiconic ())
- {
- CPaintDC DC (this); //device context for painting
- SendMessage (Wm_iconerasebkgnd, reinterpret_cast<wparam> (DC). GETSAFEHDC ()), 0);
- //Center icon in client rectangle
- int cxicon = GetSystemMetrics (Sm_cxicon);
- int cyicon = GetSystemMetrics (Sm_cyicon);
- CRect rect;
- GetClientRect (&rect);
- int x = (rect. Width ()-Cxicon + 1)/2;
- int y = (rect. Height ()-Cyicon + 1)/2;
- //Draw the icon
- dc. DrawIcon (x, y, M_hicon);
- }
- Else
- {
- //cdialog::onpaint ();//<span style= "color: #6600cc;" > Note This sentence, if not annotated, put it to the end, why?
- </span> //paste background image
- CPaintDC DC (this);
- CBitmap bmpbk;
- BMPBK.LOADBITMAPW (IDB_BITMAP_TEMPBK);
- //M_BMPBK.LOADBITMAPW (IDB_BMPBK);
- BITMAP bmpsize;
- Bmpbk.getbitmap (&bmpsize); //Get background image size
- CRect rect;
- GetClientRect (&rect); //Get customer area size
- CDC Dcmem;
- Dcmem.createcompatibledc (&DC);
- Dcmem.selectobject (&BMPBK);
- dc. StretchBlt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0,bmpsize.bmwidth,bmpsize.bmheight,srccopy); //Stretch or compress the background image to the customer area
- //Paste background image
- }
- }
The following results are performed:
Method Two:
StretchBlt in the OnEraseBkgnd
This is: Comment out the return cdialog::onerasebkgnd (PDC) and return true directly (why not return this to return true?). ), the code is as follows:
- BOOL cqqqqqdlg::onerasebkgnd (cdc* PDC)
- {
- //Todo:add your message handler code here and/or call default
- //Paste background image
- CBitmap bmpbk;
- BMPBK.LOADBITMAPW (IDB_BITMAP_TEMPBK);
- //M_BMPBK.LOADBITMAPW (IDB_BMPBK);
- BITMAP bmpsize;
- Bmpbk.getbitmap (&bmpsize); //Get background image size
- CRect rect;
- GetClientRect (&rect); //Get customer area size
- CDC Dcmem;
- Dcmem.createcompatibledc (PDC);
- Dcmem.selectobject (&BMPBK);
- Pdc->stretchblt (0,0,rect. Width (), Rect. Height (), &dcmem,0,0,bmpsize.bmwidth,bmpsize.bmheight,srccopy); //Stretch or compress the background image to the customer area
- //Paste background image
- return true;
- //return Cdialog::onerasebkgnd (PDC);
- }
The effect is the same as the method, the picture is not affixed, see.
And here is a very interesting phenomenon, if the OnEraseBkgnd map, in the OnPaint () function does not call the base class OnPaint, that is, comment out Cdialog::onpaint (), the interface is hidden and then displayed then the control is all gone, Only dialog boxes and backgrounds. Such as:
The reason for this is that BeginPaint and EndPaint must be called once in OnPaint and can only be called once. 》
Method Three:
Returns a brush with a background bitmap in OnCtlColor
The specific is:
1. Set a background brush in the header file
- Public
- CBrush M_BRUSHBK;
2, add the following sentence in the OnInitDialog
- Todo:add Extra initialization here
- CBitmap bmp;
- Bmp. LoadBitmap (IDB_BITMAP_TEMPBK);
- M_brushbk.createpatternbrush (&bmp);
- M_brushbk.createsolidbrush (RGB (0,255,0)); Use solid color as background
- Bmp. DeleteObject ();
3. Return background brush in OnCtlColor function
- Hbrush Cxxxxxdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
- {
- hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
- //Todo:change Any attributes of the DC here
- //Todo:return A different brush if the default is not desired
- if (pwnd== this) //this represents the current dialog box window
- {
- return M_BRUSHBK;
- }
- return HBR;
- }
The effect is as follows:
Note that the IF judgment in this function, the pwnd parameter is critical.
Let's see if there is no such if judgment, directly return to M_BRUSHBK; What will be the result, the code is as follows:
- Hbrush Cqqqqqdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
- {
- hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
- //Todo:change Any attributes of the DC here
- //Todo:return A different brush if the default is not desired
- return M_BRUSHBK;
- }
As follows:
Look at the picture and talk, do not explain, you understand.
The disadvantage of this approach is the ability to automatically adapt a picture of a stretchblt function to the size of a dialog box (the target rectangle).
Originated from http://www.cnblogs.com/lidabo/archive/2012/07/04/2576172.html
Setting dialog box background picture 3 kinds of