change color on CStatic 
Author Message
 change color on CStatic

Let's say that I want to put static text on a dialog that looks like a
hyperlink (blue, underlined), how do you accomplish this? I tried to
overload OnCtlColor and OnDraw, but nothing seemed to work.

Thanks.



Mon, 31 Jul 2000 03:00:00 GMT  
 change color on CStatic

I'm using the OnCtlColor and am having no problems changing the color.  In
fact you can even change the font at that point.
If you want I'll send you a little snipit of code that shows how.  All
there is to it is to use the CWnd parameter to get the control id and if
it matches your static then do a pDC->SetTextColor(RGB(r,g,b));...
Remember that the OnCtlColor is in the parent (in this case your dialog
object).  You don't need to mess with the OnDraw at all.

Quote:

> Let's say that I want to put static text on a dialog that looks like a
> hyperlink (blue, underlined), how do you accomplish this? I tried to
> overload OnCtlColor and OnDraw, but nothing seemed to work.

> Thanks.



Mon, 31 Jul 2000 03:00:00 GMT  
 change color on CStatic

The question is what do you mean by "On a dialog"?  I gather from the
heading of your question you really mean "In a static control on a
dialog".  Here's how I do it:

Use ClassWizard to create a new class, for example, CHyperlink, which
is derived from CStatic:

class CHyperlink : public CStatic { ... }

Using the ClassWizard, select the =WM_CTLCOLOR option and add a method
in the derived class for handling the OnCtlColor message.

When the OnCtlColor method is called, you call (if I recall the name
of the parameter correctly)
        pDC->SetTextColor(RGB(0, 0, 255));

(Of course you probably don't want to hardwire the color in but get it
from some other useful place, such as the Registry, to allow the user
to select the desired hyperlink color, but that's a fine point).

Now all the text in the control should be blue.  Note that there is
one other hack, and I've forgotten exactly what happens in this case.
Note that the last line of the OnCtlColor method in your handler is
the line
        return CStatic::OnCtlColor(...);

If the default handler returns a NULL brush handle, some versions of
Win32 simply ignore any changes in the DC.  So you may have to modify
this to be
        HBRUSH result = CStatic::OnCtlColor(...);
        if(result == NULL)
            result = (HBRUSH) bkbrush.m_hGdiObject;
             return result;

what is that bkbrush?  Well, it could be declared as a static variable
such as
        static CBrush bkbrush( ::GetSysColor(COLOR_whatever) );

the object will be deleted when the program terminates because the
destructor will be called on the CBrush (not that this matters in the
Big Picture but it helps when you are using resource-leak-testing
debugging aids such as Bounds Checker for Windows).

If you want some subpiece of the text of a CStatic to be hyperlink
color while the rest is regular text, forget using anything this
simple.  You have to go to either a paint-it-yourself mechanism,
implementing the OnPaint handler for your control, or use a Rich Edit
control.
                                        joe

On Thu, 12 Feb 1998 11:47:29 -0500, Mark Cunningham

Quote:

>Let's say that I want to put static text on a dialog that looks like a
>hyperlink (blue, underlined), how do you accomplish this? I tried to
>overload OnCtlColor and OnDraw, but nothing seemed to work.

>Thanks.

Joseph M. Newcomer

http://www3.pgh.net/~newcomer


Wed, 02 Aug 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Changing color of CStatic text

2. Changing color of CStatic text

3. How do I change the color of CStatic ???

4. How to change text color in CStatic?

5. How to change color of a CStatic?

6. Changing the color of text within a CStatic Cntrl

7. Change background color of CStatic

8. Change color of a CStatic when cursor is hovering

9. change color in a CStatic control

10. how to change CStatic font color ?

11. Changing the background color of CStatic, how?

12. How do I change the color of CStatic?

 

 
Powered by phpBB® Forum Software