Hi, all ...

I have been desperatly trying, in vain, to change the color of
a static control, using VC++ under Windows 3.1.
Any help appreciated !
I used the following declaration :

class CMainWnd : public CFrameWnd
    CMainWnd ();
    CStatic lblFancy;
    afx_msg HBRUSH OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor);    

And the body of the callback is :

afx_msg HBRUSH CMainWnd::OnCtlColor (CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    if (nCtlColor == CTLCOLOR_STATIC)
        if (pWnd->GetSafeHwnd() == lblFancy.GetSafeHwnd())
            return (HBRUSH) NULL;              
    return (HBRUSH) NULL;    

I tried to remove all the 'ifs', but it didn't change anything.
I know that this code is executed, (I added debugging messages and beeps),
but it doesn't change the color of my static control at all.

I used this technique without MFCs, using "pure" SDK C, and it worked
Therefore, I would appreciate any help !

By the way, I have another question : I also need to change the
color of a combo-box, but the help files are not very clear about
that, and I don't know how to do. But first, I must be able to
change the color of a simple static control !

Thanks by advance.

Cedric Simon.
Who is Participating?
xbwenConnect With a Mentor Commented:
1. Don't forget
2. can you try this way:
   afx_msg HBRUSH CWappView::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
    static CBrush TxtBkBrush(RGB(0, 255, 0));
    if (pWnd == (CWnd *)(&lblFancy) && nCtlColor ==                      CTLCOLOR_STATIC)
        pDC->SetTextColor(RGB(0, 0, 0));
        pDC->SetBkColor(RGB(255, 255, 0));
        return (HBRUSH)TxtBkBrush.GetSafeHandle();
    return NULL;
    these code work very well in my application.

Have you tried to return real brush handle instead of NULL from OnCtlColor?
Have you tried to return real brush handle instead of NULL from OnCtlColor?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

garfield020497Author Commented:
Yes, Serge, I tried to return BLACK_BRUSH, but it didn't work
better, that's why I tried to retrun NULL, just to see...
Try this one. I think this will work:-
// This will display white text on black background!!
CBrush *pBrush = CBrush::FromHandle(
return (HBRUSH)(*pBrush);
garfield020497Author Commented:
Hard luck !
None of these two pieces of code work in my application !
I did not forget the "BEGIN_MESSAGE_MAP" stuff, and I said
that my code IS executed but seems to have no effect.
My problem must be elsewhere.
And there is still the second part of my question
(this is now the fourth time I submit this question, but
nobody seem to want to answer it !!!!!!!) :

I would like to change the colors for a combobox. You can
explain me everything you want, but it would be so easy
to understand with a small sample of code !
(assuming that I can solve my color problem with a static
control, first :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.