Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting transparent background Win32

Tags:

windows

winapi

What I'm trying to do is very simple but there doesn’t seem to be a lot of information on it. Basically I have a normal non-transparent parent window and I want to place a child window (that has its own non-transparent controls) inside that parent window. So the only thing I have to do is to set the background brush of the child window transparent, but it still draws a white background. Tried using WS_EX_LAYERED style and SetLayeredWindowAttributes but it makes the child window invisible.

like image 436
pullo_van Avatar asked May 19 '11 11:05

pullo_van


1 Answers

There are two basic ways to achieve "transparent" child controls on Windows:

  • Handle WM_CTLCOLORxxx messages in the parent window. This is a convenient way to make existing controls support transparency. Each control will send a WM_CTLCOLORxxx message appropriate to the type of the control. If you create a brush that represents the background skin of the dialog, and return that from each message, the net effect will be as if each control was painted with a transparent outer area. This fails however if you want alpha effects, or for controls to physically overlap each other.

  • Use WS_EX_COMPOSITED on the parent window. Without this style, the window managers paint order of child windows is undefined, in practice, its top to bottom. When trying to alpha blend overlapping controls the result will be... unpleasant. WS_EX_COMPOSITED will ensure the child windows are painted bottom to top. Make sure that you DON'T use either the WS_CLIPCHILDREN or WS_CLIPSIBLINGS styles as that will prevent the overlapping areas from being painted at all.

You still need to do something clever via WM_CTLCOLORxxx messages as the standard controls are still going to try and fill their entire rect with dialog-background-grey.

like image 144
Chris Becke Avatar answered Nov 15 '22 00:11

Chris Becke