How to change the background color of the dialog in VC6.0

zhaozj2021-02-08  291

Before the yellow base (Guilin, Guangxi)

---- The author discussed how to change the color of the control, but there is a considerable part of the reader to letter: A dialog-based MFC AppWizard application, how to change the dialog Background color? For this problem, it can be implemented by several different ways, as shown below (the crude hull code is added):

---- 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 is to set the application dialog to a blue background and red text, steps below:

---- 1 New Dialog-based MFC AppWizard Application Exampledlg.

---- 2 Add the following code in Cexampledlgapp :: InitInstance ():

Bool Cexampledlgapp:: InitInstance ()

{

...

Cexampledlgdlg DLG;

m_pmainwnd = & dlg;

// Call the Domodal (), set the dialog box to blue background, red text

SetDialogbkcolor (RGB (0,0,255), RGB (255, 0));

INT nresponse = dlg.domodal ();

...

}

---- Compile and run, the background color of the dialog box and text color have changed. It is worth noting that SETDIALOGBKCOLOR is called before calling Domodal (), and this method is to change all the dialog colors in the application, and cannot target a specified dialog box.

---- Method 2: Overload OnPaint (), the WM_PAINT message. The relevant code is as follows (the above example is subject to):

Void Cexampledlgdlg :: onpaint ()

{

IF (Isiconic ())

...

Else

{

CRECT RECT;

CPAINTDC DC (this);

GetClientRect (Rect);

Dc.FillSolidRect (Rect, RGB (0,255,0)); // Set to green background

CDIALOG :: onpaint ();

}

---- Method 3: Overload OnctLcolor (CDC * PDC, CWND * PWND, UINT NCTLCOLOR), the WM_CTLCOLOR message. The specific steps are as follows (above the above examples):

---- 1 In the header file of Cexampledlg, add a CBRUSH member variable:

Class Cexampledlgdlg: Public CDialog

{

...

protected:

CBRUSH M_BRUSH;

...

}

---- 2 Add the following code in the OnInitDialog () function:

Bool Cexampledlgdlg :: OnInitdialog ()

{

...

// Todo: Add Extra Initialization Here

m_brush.createsolidbrush (RGB (0, 255, 0)); // Generate a green brush

...

}

---- 3 Using ClassWizard to overload onctlcolor, ie WM_CTLCOLOR message:

Hbrush Cexampledlgdlg :: onCTLCOLOR

(CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

/ *

** Here you don't have to write any code!

** Downlink code to comment

** Hbrush Hbr = CDIALOG :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR); * /

Return m_brush; // Return green brush

}

---- Method 4: Or ONCTLCOLOR (CDC * PDC, CWND * PWND, UINT NCTLCOLOR), is the WM_CTLCOLOR message. The specific steps are as follows (above the above examples):

---- Step 1, 2 Top 1, 2 of the same method.

---- Step 3 Some of the use of ClassWizard to overload onCTLCOLOR (ie, WM_CTLCOLOR message):

Hbrush Cexampledlgdlg :: onCTLCOLOR

(CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

Hbrush Hbr = CDIALOG :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);

/ / Add a judgment statement that adds a dialog box?

IF (nctlcolor == ctlcolor_dlg)

Return m_brush; // Return green brush

Return Hbr;

}

---- Compile and run.

---- Regarding how to change the background color of the dialog box, there may be many different ways to achieve, and the author only has four common methods. The programming of the method seems to be a bit less specification. The method is more than the method three, and the contrast of the author is to broaden the programming ideas of VC programming enthusiasts, especially beginners, readers can choose one of them according to the actual situation. . If the "Software News" 2000 No. 5 change the color color on the dialog box, it is believed that "coloring" will "color" in a lot of MFC applications.

转载请注明原文地址:https://www.9cbs.com/read-2376.html

New Post(0)