C++ – Visual Studio 2005: static text control won’t display with transparent background

cvisual-c++-2005visual-studio-2005

I'm using the Dialog editor in Visual Studio 2005 to create a Dialog box with a static text control. I'd like the background of the static text control to be transparent since I'm using an static image control underneath it and the grey text background looks hideous. In the editor, I set the "Transparent" attribute to True and it causes the background to go transparent just like I want it to. But as soon as I run my app and change the text using a SendMessage(hText, WM_SETTEXT, 0L, "newtext"), the background loses its transparency and goes grey again. Any ideas? Btw, I'm doing this in C++.

Thanks in advance for your help!

Best Answer

As Anthony Johnson said, handle the WM_CTLCOLORSTATIC message in the dialog box (you don't have to handle WM_NOTIFY - I don't believe static controls use that message, anyway). But it doesn't seem to be enough to set the background mode to transparent. You also have to set the background brush to a null brush. Something like this should work (in your DialogProc):

case WM_CTLCOLORSTATIC:
    SetBkMode((HDC)wParam, TRANSPARENT);
    return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);

If you change the text on the static control, you may have to invalidate what's underneath it for it to draw correctly when you do this.