Adjust the control background color by programming

Source: Internet
Author: User
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.

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.