Setting dialog box background picture 3 kinds of

Source: Internet
Author: User

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

  1. void Cqqqqqdlg::onpaint ()
  2. {
  3. if (Isiconic ())
  4. {
  5. CPaintDC DC (this); //device context for painting
  6. SendMessage (Wm_iconerasebkgnd, reinterpret_cast<wparam> (DC).  GETSAFEHDC ()), 0);
  7. //Center icon in client rectangle
  8. int cxicon = GetSystemMetrics (Sm_cxicon);
  9. int cyicon = GetSystemMetrics (Sm_cyicon);
  10. CRect rect;
  11. GetClientRect (&rect);
  12. int x = (rect.  Width ()-Cxicon + 1)/2;
  13. int y = (rect.  Height ()-Cyicon + 1)/2;
  14. //Draw the icon
  15. dc. DrawIcon (x, y, M_hicon);
  16. }
  17. Else
  18. {
  19. //cdialog::onpaint ();//<span style= "color: #6600cc;"   > Note This sentence, if not annotated, put it to the end, why?
  20. </span> //paste background image
  21. CPaintDC DC (this);
  22. CBitmap bmpbk;
  23. BMPBK.LOADBITMAPW (IDB_BITMAP_TEMPBK);
  24. //M_BMPBK.LOADBITMAPW (IDB_BMPBK);
  25. BITMAP bmpsize;
  26. Bmpbk.getbitmap (&bmpsize); //Get background image size
  27. CRect rect;
  28. GetClientRect (&rect); //Get customer area size
  29. CDC Dcmem;
  30. Dcmem.createcompatibledc (&DC);
  31. Dcmem.selectobject (&BMPBK);
  32. 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
  33. //Paste background image
  34. }
  35. }


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:

  1. BOOL cqqqqqdlg::onerasebkgnd (cdc* PDC)
  2. {
  3. //Todo:add your message handler code here and/or call default
  4. //Paste background image
  5. CBitmap bmpbk;
  6. BMPBK.LOADBITMAPW (IDB_BITMAP_TEMPBK);
  7. //M_BMPBK.LOADBITMAPW (IDB_BMPBK);
  8. BITMAP bmpsize;
  9. Bmpbk.getbitmap (&bmpsize); //Get background image size
  10. CRect rect;
  11. GetClientRect (&rect); //Get customer area size
  12. CDC Dcmem;
  13. Dcmem.createcompatibledc (PDC);
  14. Dcmem.selectobject (&BMPBK);
  15. 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
  16. //Paste background image
  17. return true;
  18. //return Cdialog::onerasebkgnd (PDC);
  19. }


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

    1. Public
    2. CBrush M_BRUSHBK;


2, add the following sentence in the OnInitDialog

    1. Todo:add Extra initialization here
    2. CBitmap bmp;
    3. Bmp. LoadBitmap (IDB_BITMAP_TEMPBK);
    4. M_brushbk.createpatternbrush (&bmp);
    5. M_brushbk.createsolidbrush (RGB (0,255,0)); Use solid color as background
    6. Bmp. DeleteObject ();



3. Return background brush in OnCtlColor function

  1. Hbrush Cxxxxxdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
  2. {
  3. hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
  4. //Todo:change Any attributes of the DC here
  5. //Todo:return A different brush if the default is not desired
  6. if (pwnd== this) //this represents the current dialog box window
  7. {
  8. return M_BRUSHBK;
  9. }
  10. return HBR;
  11. }

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:

  1. Hbrush Cqqqqqdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
  2. {
  3. hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
  4. //Todo:change Any attributes of the DC here
  5. //Todo:return A different brush if the default is not desired
  6. return M_BRUSHBK;
  7. }


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

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.