Solved

WM_CTLCOLOR ...

Posted on 1997-03-26
6
844 Views
Last Modified: 2013-11-20
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
{  
public:
    CMainWnd ();
private:
    DECLARE_MESSAGE_MAP();
    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())
        {
            pDC->SetTextColor(RGB(0,255,0));
            pDC->SetBkColor(RGB(0,0,0));        
            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
fine.
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.
0
Comment
Question by:garfield020497
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:serge010697
ID: 1300691
Have you tried to return real brush handle instead of NULL from OnCtlColor?
0
 
LVL 1

Expert Comment

by:serge010697
ID: 1300692
Have you tried to return real brush handle instead of NULL from OnCtlColor?
0
 

Author Comment

by:garfield020497
ID: 1300693
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...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
xbwen earned 100 total points
ID: 1300694
1. Don't forget
    BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
      //{{AFX_MSG_MAP(CMyWnd)
      ...
        ON_WM_CTLCOLOR()
      ...
      //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
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.

0
 

Expert Comment

by:syed_dgreat
ID: 1300695
Try this one. I think this will work:-
// This will display white text on black background!!
CBrush *pBrush = CBrush::FromHandle(
(HBRUSH)::GetStockObject(BLACK_BRUSH));
pDC->SelectObject(pBrush);
pDC->SetTextColor(RGB(0xff,0xff,0xff));
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)(*pBrush);
0
 

Author Comment

by:garfield020497
ID: 1300696
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 :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Named range not carried over 10 66
iSeries DB2 Query 2 98
Hibernate methods 2 83
Doc'in system (example?) BA 7 120
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question