Home > Software engineering >  Don't have to change the font color OnCtlColor MFC
Don't have to change the font color OnCtlColor MFC

Time:10-10

Due to change the color of the Static is more, if you use OnCtlColor, will write a lot if judgment, plan to write a generic function to change the color, such as
Void setcolor (ID, color) {} this way, when I click a button to call the function, so as to change the color of the specified ID, but use SetTextColor from OnCtlColor complains, there are other ways to solve?

CodePudding user response:

Try
 
HBRUSH CMyStatic: : CtlColor (CDC * pDC, UINT nCtlColor)
{
//TODO: Change any attributes of the DC here
The static CBrush Brush (RGB (255255128));

//CRect rc;
//GetClientRect (& amp; Rc);
//pDC - & gt; FillSolidRect (rc, RGB (0255255));//blue
//Set the background mode for the text to transparent
//so the background will show thru.
COLORREF color;
PDC - & gt; SetBkMode (TRANSPARENT);
//Set the text color to white
UINT id=GetDlgCtrlID ();
The switch (id)
{
Case IDC_STATIC1: color=RGB (0128128); break;
Case IDC_STATIC2: color=RGB (129128, 0); break;
Case IDC_STATIC3: color=RGB (128,0,128); break;
}
PDC - & gt; SetTextColor (color);
Return the Brush;
//TODO: the Return of a non - NULL brush if the parent 's handler should not be called
//return NULL;
}

The
ON_WM_CTLCOLOR_REFLECT ()

CodePudding user response:

reference 1st floor schlafenhamster response:
try
 
HBRUSH CMyStatic: : CtlColor (CDC * pDC, UINT nCtlColor)
{
//TODO: Change any attributes of the DC here
The static CBrush Brush (RGB (255255128));

//CRect rc;
//GetClientRect (& amp; Rc);
//pDC - & gt; FillSolidRect (rc, RGB (0255255));//blue
//Set the background mode for the text to transparent
//so the background will show thru.
COLORREF color;
PDC - & gt; SetBkMode (TRANSPARENT);
//Set the text color to white
UINT id=GetDlgCtrlID ();
The switch (id)
{
Case IDC_STATIC1: color=RGB (0128128); break;
Case IDC_STATIC2: color=RGB (129128, 0); break;
Case IDC_STATIC3: color=RGB (128,0,128); break;
}
PDC - & gt; SetTextColor (color);
Return the Brush;
//TODO: the Return of a non - NULL brush if the parent 's handler should not be called
//return NULL;
}

The
ON_WM_CTLCOLOR_REFLECT ()


Don't want to use CtlColor this function, but individual write a function to set the font color, besides CtlColor this way, there are other?

CodePudding user response:

 protected: 
COLORREF myColor [3].

HBRUSH CMyStatic: : CtlColor (CDC * pDC, UINT nCtlColor)
{
//TODO: Change any attributes of the DC here
The static CBrush Brush (RGB (255255128));
PDC - & gt; SetBkMode (TRANSPARENT);
//Set the text color
UINT id=GetDlgCtrlID ();
Id=IDC_STATIC1;
PDC - & gt; SetTextColor (myColor [id]);
Return the Brush;
//TODO: the Return of a non - NULL brush if the parent 's handler should not be called
//return NULL;
}

CMyStatic: : CMyStatic ()
{
MyColor [0]=RGB (128255, 0);
MyColor [1]=RGB (0128255);
MyColor [2]=RGB (128128255);
}

You can write a function

CodePudding user response:

Your demand with
CS_OWNDC about
In the call GetDC or BeginPaint Windows with default values to create a device context, properties of device context for all changes will be effective after the ReleaseDC or EndPaint use, if you don't want to use the default attributes, you can create CS_OWN and CS_CLASSDC device context attributes his definitions,
Wndclass. Style=CS_OWNDC now based on this window class every window has been created a device context, and exist until the window to be deleted, only need to initialize a device context ( can be done in WM_CREATE message handler news during the SetTextColor ) can, of course, when you need to change, since each call BeginPaint and GetDC function returns the device context is to use you just set the device context and style CS_OWNDC affects only GetDC BeginPaint device context, and does not affect other functions (such as GetWindowDC) device context,
About CS_CLASSDC unlike CS_OWNDC window class when using this style, only to create a device context for all Windows, all Windows share a device context (and are mutually exclusive),

CodePudding user response:

You can rewrite the CStatic class, inside its own=CTL_COLOR message, and then according to your condition called SetTextColor ()
Then define a public func (); To change your condition, and then call InvalidateRect refresh ~
  • Related