2
votes

I want to make a transparent dialog. I capture the OnCtlColor message in a CDialog derived class...this is the code:

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
   if(bSetBkTransparent_)
   {
      pDC->SetBkMode(TRANSPARENT);
      hbr = (HBRUSH)GetStockObject(NULL_BRUSH);        
   }


   return hbr;
}

It works fine for all the controls but the group-box (CStatic). All the labels (CStatic) are been painted with a transparent text background but the text of the group box it is not transparent.

I already googled for this but I didn't find a solutions. Does anybody know how to make a real transparent group-box?

By the way, I am working in Windows XP. And I don't want to fully draw the control to avoid having to change the code if the application is migrated to another OS.

Thanks,

Javier

Note: I finally changed the dialog so that I don't need to make it transparent. Anyway, I add this information because maybe someone is still trying to do it. The groupbox isn't a CStatic but a CButton (I know this is not new). I changed the Windows XP theme to Windows classic and then the groupbox backgraund was transparent. The bad new is that in this case the frame line gets visible beneath the text...so if someone is following this approach I think maybe he/she would better follow the Adzm's advice.

3
I just tried this in a very quick test, and the group box text is transparent (to the extent that even the group box is visible beneath the text). Possibly your problem isn't in the above code: you might want to try doing a quick minimal demo test program.DavidK

3 Answers

1
votes

You have two options.

You can not use Common Controls v6 (the XP-Styled controls), which will make your app lose the fanciness of newer windows versions. However IIRC the groupbox will respect the CTLCOLOR issue. If you are not using that anyway, and it is still not respecting your color, then you only have one option...

Which is to draw it yourself. I know you said you don't want to, but sometimes you have to. Thankfully a group box is a very simple control to draw. This page has an example for drawing a classic-style group box: http://www.codeguru.com/cpp/controls/controls/groupbox/article.php/c2273/ You can also draw it very simply using the UxTheme libraries that come with XP+.

If the application will be migrated to another OS, you will have plenty to deal with migrating over an MFC application in general. If that is your goal, then you should really look into developing with a cross-platform UI toolkit.

0
votes

Simply set the WS_EX_TRANSPARENT extended window style for the group box.

0
votes

I knows this is a 12 years old question, but it frustrates me that nobody answered it correctly so far.

All you have to do is handle WM_CTLCOLORSTATIC:

case WM_CTLCOLORSTATIC: 
{
    HDC hDC = (HDC)wParam;
    SetTextColor(hDC, RGB(255, 255, 255));
    SetBkMode(hDC, TRANSPARENT);
    return (INT_PTR)GetStockObject(HOLLOW_BRUSH);
}
break;