views:

714

answers:

3

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!

A: 

Try hiding the control, then setting the text, then showing it.

Nick
A: 

I don't know how you can do it in the dialog editor, but if you handle the WM_NOTIFY message in the static's parent window, the static will send a WM_CTLCOLORSTATIC message before drawing the static. There, if you call SetBkMode((HDC)wParam, TRANSPARENT);, that should make the static have a transparent background.

Anthony Johnson
+1  A: 

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.

Joel