Change of Control color

Source: Internet
Author: User

In VC development, there are times when you are editing resources, such as the size of a control on a dialog box, the font change, the font color change, and the color change of the controls themselves, and so on.

Only the changes to the color of the control are discussed here, and the control does not contain a font. However, this method does not work on the button control, but it still has some referential significance.

Hypothetical condition: There is a static text control, placed on the dialog box.

Method One: Respond to the message function in the dialog class OnCtlColor (), in this function to determine whether the current control ID, if satisfied with the decision, change the color of the control. The code is as follows:

Hbrush Ccolorsetdlg::onctlcolor (cdc* PDC, cwnd* pwnd, UINT nCtlColor)
{
	Hbrush HBR = Cpropertypage::onctlcolor ( PDC, pwnd, nCtlColor);

	TODO:  Change any of the DC properties here
	if (pwnd->getdlgctrlid () = = Idc_pt_color)
	{
		Pdc->setbkcolor RGB (255,0,0 ));	
	}

	TODO:  returns another brush return HBR if the default is not the desired brush
	;


Idc_pt_color is the static control ID that needs to be changed. The same code and description are commonly seen on the web. But by my own test, this is not possible. Because the SetBkColor function does not change the color of the control, it is the color of the font background on the control. If there is no font in the current control, the color of the control does not change. Under hypothetical conditions, we can set some spaces. This ensures that there are characters, that the character background color is changed, that the display effect is consistent with the color of the control, and that the font is not visible on the control. And the code is the easiest to implement.


Method Two: The same, respond to the message function in the dialog class OnCtlColor (), in this function to determine whether the current control ID, if the decision is satisfied, then return the need to set the color of the brush.

Hbrush Ccolorsetdlg::onctlcolor (cdc* PDC, cwnd* pwnd, UINT nCtlColor)
{
	Hbrush HBR = Cpropertypage::onctlcolor ( PDC, pwnd, nCtlColor);

	TODO:  Change any of the DC properties here
	if (pwnd->getdlgctrlid () = = Idc_pt_color)
	{return
		(hbrush) m_ptbrush.m_ hobject;	
	}

	TODO:  returns another brush return HBR if the default is not the desired brush
	;



Idc_pt_color is the static control ID that needs to be changed. The biggest difference between this method and method is that the method changes the color of the control itself, and the method changes the color of the font background on the control, although the display has the same effect and the internal implementation principle is quite different. This method needs to return a variable of the brush. You can now define the member variables in the header file of the dialog class

CBrush   M_ptbrush;

And then in the constructor, load the color in

M_ptbrush.createsolidbrush (RGB (255,255,255));

This combination allows you to talk about the color change of the control, but note that the last in the destructor, don't forget

M_ptbrush.deleteobject ();




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.