How To Use UpdateLayeredWindow
In this post I will briefly explain how to use layered windows and specifically how to use UpdateLayeredWindow.
The first thing you need to do is add the WS_EX_LAYERED style to your window. This can for example be done with a call to CreateWindowEx:
hWnd = CreateWindowEx(WS_EX_LAYERED, szWindowClass, szTitle, 0,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
After your window is created we will load a PNG file with an alpha channel and use UpdateLayeredWindow to render the PNG on the window using the alpha channel of the PNG file as the transparency level for the window. This is done as follows:
// Load our PNG image
CImage img;
img.Load("circle.png");
// Get dimensions
int iWidth = img.GetWidth();
int iHeight = img.GetHeight();
// Make mem DC + mem bitmap
HDC hdcScreen = GetDC(NULL);
HDC hDC = CreateCompatibleDC(hdcScreen);
HBITMAP hBmp = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);
HBITMAP hBmpOld = (HBITMAP)SelectObject(hDC, hBmp);
// Draw image to memory DC
img.Draw(hDC, 0, 0, iWidth, iHeight, 0, 0, iWidth, iHeight);
// Call UpdateLayeredWindow
BLENDFUNCTION blend = {0};
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
blend.AlphaFormat = AC_SRC_ALPHA;
POINT ptPos = {0, 0};
SIZE sizeWnd = {iWidth, iHeight};
POINT ptSrc = {0, 0};
UpdateLayeredWindow(hWnd, hdcScreen, &ptPos, &sizeWnd, hDC, &ptSrc, 0, &blend, ULW_ALPHA);
SelectObject(hDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hDC);
ReleaseDC(NULL, hdcScreen);
Because I’m using CImage, you need to include the atlimage.h header.
That’s all that is required for the basics of UpdateLayeredWindow.
NOTE: The example above does not include any error checking. That is left for the reader as an excercise.

Kay said,
Wrote on June 17, 2009 @ 10:30 am
Hi, thanks for that post.
Could you maybe extend this example to show how to use UpdateLayeredWindow with UI Controls like buttons etc?:)
I cant find any solution for this. What i exactly need is to know how i can Use my PNG with alpha channel as background and a set of other pngs as buttons/ui controlls
Marc Gregoire said,
Wrote on June 17, 2009 @ 12:21 pm
In my example I have this device context called hDC which will be used in the call to UpdateLayeredWindow. This is just a 32bit device context, so RGB and A channels. If you want to add PNG based buttons, load the PNGs (maybe using CImage class) and then render those PNGs at the right position in the hDC. Store the coordinates of your PNG buttons somewhere so you can use the coordinates in your WM_MOUSEMOVE and mouse button messages. Once everything is rendered to hDC, call UpdateLayeredWindow. I think this should give you what you want.
Ovidiu said,
Wrote on July 5, 2009 @ 7:12 pm
Hi, Marc,
Is it possible the inverse operation, i.e. saving A channel in a (png) file given a layered window?
Marc Gregoire said,
Wrote on July 5, 2009 @ 7:34 pm
Hi Ovidiu
I don’t know if that would be possible. I never tried it myself. Why do you need something like that?
Did you try to get the HDC of the layered window? Maybe that HDC is a 32bit device context including the alpha channel…
Tom said,
Wrote on December 13, 2009 @ 1:09 am
Hello
I have VC++ 6.0 which doesn’t seem to have the CImage class (atlimage.h).
Could you possibly write another version that simply loads a BMP?
I believe the latest version of BMP does support transparency.
Thank you
Marc Gregoire said,
Wrote on December 13, 2009 @ 12:34 pm
I tried to make a BMP file with an alpha channel in two photo editors but they don’t allow it.
Maybe you can use another library to load PNG files?
Maybe this one: http://www.codeguru.com/cpp/g-m/bitmap/otherformats/article.php/c4899/ ?
neptunecentury said,
Wrote on December 29, 2009 @ 6:13 pm
@Marc
GIMP for Windows is an excellent image editor and supports 32-bit BMPs. I have created transparent bitmaps with editor for use in my applications, but I have no use for 32-bit bmps as I now use the GDI+ library and it supports loading png files. However, if you want to create an example with loading just BMPs, you could create them with GIMP. http://www.gimp.org/windows/
Marc Gregoire said,
Wrote on December 30, 2009 @ 1:39 pm
Interesting. I have used GIMP before, but didn’t know it supports 32 bit BMP files.
When I find some more time I might give it a try.