Adjust the control background color by programming
Su Jin Guo
In VC, the background color of the editing control can be adjusted because there is no direct property setting. Therefore, we need to use operations related to the graphic device interface to achieve this goal.
First, use the Class Wizard to construct the corresponding message ing for the wm_ctlcolor message. The following message processing functions are obtained:
Hbrush cmydlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
// Todo: return a different brush if the default is not desired
// Return HBr;
}
Then, check whether the nctlcolor attribute value of the ctlcolor_edit control is different from the background color of the control we want. The best way is to compare the window handle, instead of comparing two pointers. Therefore, you need to add the following to the message processing function:Code:
Hbrush cmydlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
// Obtain the control
Cedit * pedit = (cedit *) getdlgitem (idc_rededit );
If (nctlcolor = ctlcolor_edit & pedit-> getsafehwnd () = pwnd-> getsafehwnd ())
{
// Set the background Mode
PDC-> setbkmode (transparent );
// Adjust the text color to better display the new background color.
PDC-> settextcolor (RGB (255,255,255 ));
// Returns the red brush.
Return m_brush;
}
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
}
If the control is set to read-only or readonly, it is better to compare nctlcolor with ctlcolor_static. The Code is as follows:
Hbrush cmydlg: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
// Obtain a read-only control
Cedit * peditreadonly = (cedit *) getdlgitem (idc_readonlyedit );
// Obtain the window corresponding to the control
Hwnd hwndreadonly = peditreadonly-> getsafehwnd ();
If (nctlcolor = ctlcolor_static & hwndreadonly = pwnd-> getsafehwnd ()){
// Set the background color to the brush color (currently red)
PDC-> setbkcolor (RGB (255, 0, 0 ));
// Adjust the text color
PDC-> settextcolor (RGB (255,255,255 ));
// Returns the red brush.
Return m_brush;
}
Hbrush HBr = cdialog: onctlcolor (PDC, pwnd, nctlcolor );
Return HBr;
}
with the preceding Program , you can directly adjust the background color of the editing control.