Add image background and Change Background color in the VC dialog box

Source: Internet
Author: User

In the VC dialog box, add an image background and change the background color (15:19:07)

I. How to change the background color of the dialog box

Method 1: Call the member function setdialogbkcolor of the cwinapp class.

---- The first parameter of the function specifies the background color, and the second parameter specifies the text color. The following example shows how to set the application dialog box to blue background and red text:

---- ① Create a dialog-based MFC Appwizard application exampledlg.

---- ② Add the following code to cexampledlgapp: initinstance:

Bool cexampledlgapp: initinstance ()
{
...
    Cexampledlgdlg DLG;
    M_pmainwnd = & DLG;

// Call domodal () to set the dialog box to blue background and red text
    Setdialogbkcolor (RGB (255,), RGB (, 0 ));
    Int nresponse = DLG. domodal ();
...
}

---- Compile and run the dialog box. The background color and text color of the dialog box have changed. It is worth noting that setdialogbkcolor must be called before domodal () is called, and this method will change the color of all the dialogs in the application and cannot target a specified dialog box.
---- Method 2: Reload onPaint (), that is, the message wm_paint. The Code is as follows (the above example project prevails ):

Void cexampledlgdlg: OnPaint ()
{
    If (isiconic ())
...
  Else
  {
        Crect rect;
        Cpaintdc DC (this );
        Getclientrect (rect );
        DC. fillsolidrect (rect, RGB (0,255, 0 )); // Set it to a green background

        Cdialog: OnPaint ();
  }

---- Method 3: Reload onCtlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor), that is, wm_ctlcolor message. The specific steps are as follows (the above project prevails ):
---- ① Add a member variable of cbrush to the header file of cexampledlgdlg:

Class cexampledlgdlg: Public cdialog
{
...
Protected:
Cbrush m_brush;
...
};

---- ② OnAdd the following code to the initdialog () function:
Bool cexampledlgdlg: OnInitdialog ()
{
...
// Todo: add extra initialization here
M_brush.createsolidbrush (RGB (0,255, 0); // generate a green brush
...
}

---- ③ Use classwizard to reload onCtlcolor (...), That is, wm_ctlcolor message:
Hbrush cexampledlgdlg: OnCtlcolor
(CDC * PDC, cwnd * pwnd, uint nctlcolor)
{

Return m_brush; // Add a green brush
}

---- Method 4: Reload the onCtlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor), that is, wm_ctlcolor message. The specific steps are as follows (the above project prevails ):
---- Steps ① and ② are the steps ① and ② above in method 3.

---- Step ③ use classwizard to reload onCtlcolor (...) (Wm_ctlcolor message) is somewhat different:

Hbrush cexampledlgdlg: OnCtlcolor
(CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: OnCtlcolor (PDC, pwnd, nctlcolor );

// Add a judgment statement in the dialog box
If (nctlcolor = ctlcolor_dlg)
Return m_brush; // Add a green brush
Return HBr;
}

**************************************** **************************************** **************************************** ************

Ii. How to add an image background

Method 1:

 Void about: OnPaint ()
{
 Cpaintdc DC (this); // device context for painting
 
 // Todo: add your message handler CoDe here
    Cpaintdc DCC (this ); 
         Crect Rect; 
         Getclientrect (& rect ); 
         CDC Dcmem; 
         Dcmem. createcompatibledc (& DC ); 
         Cbitmap BMP background; 
         BMP background. loadbitmap (idb_bitmap1 ); 
                 // Idb_bitmap is the ID of your own Graph 
         Bitmap Bitmap; 
         BMP background. getbitmap (& Bitmap ); 
         Cbitmap * Pbmpold = dcmem. SelectObject (& BMP background ); 
         DC. stretchblt (0, 0, rect. Width (), rect. Height (), & dcmem, 0, 0, 
        Bitmap. bmwidth, bitmap. bmheight, srccopy ); 
 // Do not call cdialog: OnPaint () for painting messages
}

Method 2:

Response onThe erasebkgnd message is displayed in the message function!

Bool cxxdlg: OnErasebkgnd (CDC * PDC)
{
 Bitmap bm;
 M_bmp .getbitmap (& BM );
 M_pbmcurrent = & m_bmp;
 CDC dcmem;
 Dcmem. createcompatibledc (PDC );
 Cbitmap * poldbitmap = dcmem. SelectObject (m_pbmcurrent );
 PDC-> bitblt (0, 0, BM. bmwidth, BM. bmheight, & dcmem, 0, srccopy );
 Dcmem. SelectObject (poldbitmap );
}

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.