Vc/mfc Setting dialog box background color

Source: Internet
Author: User

Method One: Call the member function of the CWinApp class SetDialogBkColor to implement. (This function is deprecated)

1
void SetDialogBkColor (COLORREF CLRCTLBK = RGB (192, 192, 192), COLORREF clrctltext = RGB (0, 0, 0));
Where the first parameter of the function specifies the background color, the second parameter specifies the text color.

Add the following code in InitInstance ():

1
SetDialogBkColor (RGB (0,0,255), RGB (255,0,0));
It is important to note that SetDialogBkColor must be called before calling DoModal ().

This method has been verified by the author and cannot change the background of the dialog box

Method Two: Overload OnPaint (), which is the WM_PAINT message

1
2
3
4
CRect rect;
CPAINTDC DC (this);
GetClientRect (rect);
dc. Fillsolidrect (Rect,rgb (0,255,0)); Set to green background
Method Three: Overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message

. h header file add CBrush m_brush;

. cpp source file add M_brush. CreateSolidBrush (RGB (255,0,0));

1
2
3
4
5
6
7
OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
/* downlink code to comment out
Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
*/
return m_brush; Add Red Brush
}
Method Four: or overload OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor), which is the WM_CTLCOLOR message

. h header file add CBrush m_brush;

. cpp source file add M_brush. CreateSolidBrush (RGB (255,0,0));

1
2
3
4
5
6
7
8
OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
In this add-on is a dialog box is a judgment statement
if (nCtlColor ==ctlcolor_dlg)
return m_brush; Add Red Brush
return HBR;
}
Different paint brushes can be returned depending on the type of control to achieve different color settings for controls

1
2
3
4
5
6
7
Ctlcolor_btn Button controls
Ctlcolor_dlg dialog box
Ctlcolor_edit edit Box
Ctlcolor_listbox List control
Ctlcolor_msgbox Message Control
Ctlcolor_scrollbar scroll bar control
Ctlcolor_static Static controls
1
It is important to note here that OnCtlColor can change the color of the child controls such as static, and must set its properties for the button owner draw to True to change the button background color (the font color of the CButton text does not pass
SetBkColor to change, you need to redraw the cbutton yourself and implement it in DrawItem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Hbrush Cxxxdlg::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nCtlColor)
{
Hbrush HBR = Cdialog::onctlcolor (PDC, PWnd, nCtlColor);
TODO: Change any of the properties of the DC here
if (NCTLCOLOR==CTLCOLOR_BTN)//Change button color
{
Pdc->setbkmode (TRANSPARENT);
Pdc->settextcolor (RGB (0,0,0));
Pdc->setbkcolor (RGB (121,121,255));
Hbrush B=createsolidbrush (RGB (121,121,255));
return b;
}
else if (Nctlcolor==ctlcolor_scrollbar)//
{
Pdc->setbkmode (TRANSPARENT);
Pdc->settextcolor (RGB (0,0,0));
Pdc->setbkcolor (RGB (233,233,220));
Hbrush B=createsolidbrush (RGB (233,233,220));
return b;
}
else if (nctlcolor==ctlcolor_edit)//change edit box
{
Pdc->setbkmode (TRANSPARENT);
Pdc->settextcolor (RGB (0,0,0));
Pdc->setbkcolor (RGB (165,254,236));
Hbrush B=createsolidbrush (RGB (165,254,236));
return b;
}
else if (nctlcolor==ctlcolor_static)//change static text
{
Pdc->settextcolor (RGB (0,0,0));
Pdc->setbkcolor (RGB (166,254,1));
Hbrush B=createsolidbrush (RGB (166,254,1));
return b;
}
else if (NCTLCOLOR==CTLCOLOR_DLG)//Change dialog box background color
{
Pdc->settextcolor (RGB (0,0,0));
Pdc->setbkcolor (RGB (166,254,1));
Hbrush B=createsolidbrush (RGB (166,254,1));
return b;
}
TODO: If the default is not the desired brush, another brush is returned
return HBR;
}

Vc/mfc Setting dialog box background color (GO)

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.