CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2000
    Posts
    21

    How to change all background colors for controls in a dialog box

    Hi everybody,

    Here's my problem : in a dialog based application, how can I change the text and background color of all the controls in the dialog box ?

    I don't want to change the colors for each control : if I erase one of them in the dialog resource, I have to do the same in the code. I have searched around the OnCtlColor function, but it doesn't work at all. Here is the standard implementation for that function :
    HBRUSH MyDlgClass::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    return hbr;
    }

    Instead of that code, I've tried to do something like this :
    {
    CBrush br;
    br.CreateSolidBrush(RGB(255, 0, 0)); // Red color
    return (HBRUSH) br;
    }

    Why this code doesn't colorize the controls in red ?
    This code produces the right behaviour :
    {
    if(nCtlColor == CTLCOLOR_EDIT)
    Beep(100, 10); // Multiple beep sounds are produced when the dialog appears and one beep is
    // produced when a control gets the focus
    }

    But why returning my personal brush doesn't work ?

    Thanks a lot !


  2. #2
    Join Date
    Jan 2000
    Location
    Geneva, Switzerland
    Posts
    125

    Re: How to change all background colors for controls in a dialog box

    You should change the contents of the CDC pointer. Eg. pDC->SetBkColor(RGB(255,0,0));

    However, for buttons, this doesn't work. I think the buttons have their own OnCtlColor ... Only way around that is to make them custom (I think - but I might be wrong)


    Rune Stampe Nielsen - CERN Genève

  3. #3
    Join Date
    Nov 1999
    Location
    Nova Scotia, Canada
    Posts
    59

    Re: How to change all background colors for controls in a dialog box

    In my application I have the brush which I returned as a part of the control I handle CTLCOLOR in; that is, it is not local to that function. My code looks pretty much exactly the same as yours...


    HBRUSH MyControl::CtlColor(CDC* pDC, UINT nCtlColor)
    {
    pDC->SetBkMode(TRANSPARENT); // My brush is a null brush;
    // I have a bitmap on the background I want visible...
    return HBRUSH(m_MyBackGroundBrush);
    }



    as you can see, m_MyBackGroundBrush doesn't go out of scope at the end of the function...

    James


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured