Drawing static/edit control in different colors

Hi All,

This should be a simple question, but it is possible to, control the color
of text written to static text or an edit box?  I need to do this at during
InitDialog and while dialog is 'up' (DoModal).

As an aside, can change the color of static text just based on the resource
ID?  i.e. not creating a variable for the control in the dialog class.

Thanks in Advance!
Corey.


0
6/11/2004 11:04:07 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
705 Views

Similar Articles

[PageSpeed] 6

Corey Wirun wrote:

> Hi All,
> 
> This should be a simple question, but it is possible to, control the color
> of text written to static text or an edit box?  I need to do this at during
> InitDialog and while dialog is 'up' (DoModal).
> 
> As an aside, can change the color of static text just based on the resource
> ID?  i.e. not creating a variable for the control in the dialog class.
> 
> Thanks in Advance!
> Corey.
> 
> 

Yes, but the way to do it will surprise you.  See the example code in 
OnCtlColor.  It is a message handler that is called every time the 
control paints itself.

-- 
Scott McPhillips [VC++ MVP]

0
scottmcp8856 (490)
6/12/2004 12:13:54 AM
Hi Corey,

FYI: here is a small sample:
...
HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
       CWnd *pColorText = GetDlgItem(IDC_TEXT1);    //specify a custom ID 
for the static text
	
       if (pWnd->m_hWnd == pColorText->m_hWnd)
              pDC->SetTextColor(RGB(0,0,255));
	
       return hbr;
} 


Wish it helps!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

0
v-garych (605)
6/12/2004 2:12:20 AM
In article <HTqyc.27331$%i1.2776@edtnps89>, corey.wirun@nospam.ca 
says...
> Hi All,
> 
> This should be a simple question, but it is possible to, control the color
> of text written to static text or an edit box?  I need to do this at during
> InitDialog and while dialog is 'up' (DoModal).

Yes.  Just before a control draws itself, it sends a 
WM_CTLCOLORSTATIC or WM_CTLCOLOREDIT message to its parent -- oddly 
enough, a disabled or read-only edit control sends WM_CTLCOLORSTATIC.

Anyway, by default MFC reflects this message back to the control as a 
WM_CTLCOLOR message.  Create a handler for this, and it'll receive a 
CDC * for the control. Calling its SetTextColor changes the text 
color just like usual.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
0
jcoffin1 (194)
6/12/2004 4:51:46 AM
In addition to the two answers already given, not that the only thing you can control is
the color of ALL the text. It is all red, or all green, or all blue, or whatever. If you
want to control individual characters, such as in syntax highlighting, in a CStatic you
are on your own, and for an edit control, you will have to move up to a rich edit control.
I just did a syntax-highlighting editor, and there are many interesting design defects in
rich edit that make it seriously challenging.
					joe

On Fri, 11 Jun 2004 23:04:07 GMT, "Corey Wirun" <corey.wirun@nospam.ca> wrote:

>Hi All,
>
>This should be a simple question, but it is possible to, control the color
>of text written to static text or an edit box?  I need to do this at during
>InitDialog and while dialog is 'up' (DoModal).
>
>As an aside, can change the color of static text just based on the resource
>ID?  i.e. not creating a variable for the control in the dialog class.
>
>Thanks in Advance!
>Corey.
>

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
6/12/2004 6:41:58 AM
Thanks all for your help!  I'll try it out!

Corey.

"Corey Wirun" <corey.wirun@nospam.ca> wrote in message
news:HTqyc.27331$%i1.2776@edtnps89...
> Hi All,
>
> This should be a simple question, but it is possible to, control the color
> of text written to static text or an edit box?  I need to do this at
during
> InitDialog and while dialog is 'up' (DoModal).
>
> As an aside, can change the color of static text just based on the
resource
> ID?  i.e. not creating a variable for the control in the dialog class.
>
> Thanks in Advance!
> Corey.
>
>


0
6/12/2004 8:20:44 PM
Reply:

Similar Artilces: