Win32 transparent background setting

What I'm trying to do is very simple, but really not much information. Basically, I have a normal opaque parent window, and I want to put a child window (which has its own opaque controls) inside this parent window. So the only thing I need to do is set the transparency of the background brush of the child window, but it still draws a white background. I tried using the WS_EX_LAYERED style and SetLayeredWindowAttributes, but it makes the child window invisible.

+7
source share
1 answer

There are two main ways to achieve transparent child controls in Windows:

  • Process WM_CTLCOLORxxx messages in the parent window. This is a convenient way to make existing controls transparent. Each control will send a WM_CTLCOLORxxx message corresponding to the type of control. If you create a brush representing the background skin in the dialog box and return it from each message, the clean effect will be as if each control were painted with a transparent outer area. However, this does not work if you want alpha effects or controls to physically overlap.

  • Use WS_EX_COMPOSITED in the parent window. Without this style, window managers draw the order of child windows undefined, in practice, from top to bottom. If you try to match the elements of the alpha mixture, the result will be ... unpleasant. WS_EX_COMPOSITED will ensure that child windows are colored from bottom to top. Make sure you do NOT use the WS_CLIPCHILDREN or WS_CLIPSIBLINGS styles, as this will prevent the drawing of overlapping areas.

You still need to do something smart through WM_CTLCOLORxxx messages, as standard controls will still try to fill their entire rectangle with a gray-gray dialog.

+5
source

All Articles