locked
How to change color of a groupbox RRS feed

  • Question

  •  

    Hello,

    how could I change the color of a groupbox. I didnt see a method like

    myGroup.SetBkColor(R,G,B)

    or something. I tried find out, if there are any messages to use them with

    my.Group.SendMessage(MSG_SETCOLOR...)

    but i didnt find anything.

     

    Someone knows how to do it, change a color of a group box?

     

    Thanks for support.

    Wednesday, August 22, 2007 11:46 AM

Answers

  • The easiest way is to paint it from parent dialog, in parent's OnPaint method, you can add something like this:

    Code Snippet

    CPaintDC dc(this); // device context for painting
     CRect rc;

     CWnd * pW = this->GetDlgItem(IDC_GROUPBOX);
     pW->GetClientRect(&rc);
     pW->ClientToScreen(&rc);
     this->ScreenToClient(&rc);

     dc.FillSolidRect(&rc,RGB(255,0,0));

     

    hope it helps

    rico

     

    Monday, August 27, 2007 9:04 AM

All replies

  • Which item of a group box you whant customize by the color:

    1. Caption text?

    2. Frame?

    3. Background edge of entire group box?

    Wednesday, August 22, 2007 12:51 PM
  • Ok, I found out, that i have to react on the ON_WM_CTLCOLOR.

    I creted a afx_msg methode to change the color for STATICS:

     

    Code Snippet

    HBRUSH CTestprogrammDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

    {

    // Brush für den Dialog Hintergrund anlegen

    HBRUSH hBGBrush = CreateSolidBrush(RGB(128, 184, 223));

    // Brush für Dialog Hintergrund zurück geben

    return hBGBrush;

    }

     

     

    Now I have the problem, that my whole dialog is colored  in RGB(128, 184, 223). But I wanted to color just the area concluded by groupbox, not the dialog.
    Wednesday, August 22, 2007 1:19 PM
  • Check a pWnd param of the member.

    if( pWnd->GetDlgCtrlID () == ???)

    {

    return hBGBrush;

    }

     

    Wednesday, August 22, 2007 1:24 PM
  • I did it so. But now only the capture of the groupbox is colored. not the whole area of it.

     

     

    Code Snippet

    HBRUSH CTestprogrammDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

    {

    HBRUSH hBGBrush;

     

    if( pWnd->GetDlgCtrlID() == IDC_STATIC_GROUP_OPTIONS)

    {

     

    hBGBrush = CreateSolidBrush(RGB(128, 184, 223));

    return hBGBrush;

    }

    }

     

     

    Wednesday, August 22, 2007 1:40 PM
  • I am not sure that you will be able to do ctlcolor painting of a groupbox, as it is derived from the "BUTTON" windowstyle, and this style can only do drawing via Owner Draw Styles.

     

    Also, take note of the following article:

    http://support.microsoft.com/kb/79982

     

    Wednesday, August 22, 2007 8:45 PM
  • do drawing via Owner Draw Styles

     

    OK. How to do it?

    Thursday, August 23, 2007 5:15 AM
  • After doing more research, I find that OwnerDraw style cannot be set for a group box.

    However, the problem with using ctlcolor on this object is that it is not a solid object, as it is normally drawn with a frame and text and we wish to see the objects inside the frame, thus part of the object is coloured with a brush and part will be transparent.

     

    Dealing with this could be a problem.

    Have you thought of the alternative of using a static text label, and framing the area with 4 thin static border controls in a rectangle ?

     

     

    Thursday, August 23, 2007 9:41 PM
  • The easiest way is to paint it from parent dialog, in parent's OnPaint method, you can add something like this:

    Code Snippet

    CPaintDC dc(this); // device context for painting
     CRect rc;

     CWnd * pW = this->GetDlgItem(IDC_GROUPBOX);
     pW->GetClientRect(&rc);
     pW->ClientToScreen(&rc);
     this->ScreenToClient(&rc);

     dc.FillSolidRect(&rc,RGB(255,0,0));

     

    hope it helps

    rico

     

    Monday, August 27, 2007 9:04 AM