52

I'm creating what should be a very simple Win32 C++ app whose sole purpose it to ONLY display a semi-transparent PNG. The window shouldn't have any chrome, and all the opacity should be controlled in the PNG itself.

My problem is that the window doesn't repaint when the content under the window changes, so the transparent areas of the PNG are "stuck" with what was under the window when the application was initially started.

Here's the line where I setup the new window:

hWnd = CreateWindowEx(WS_EX_TOPMOST, szWindowClass, szTitle, WS_POPUP, 0, height/2 - 20, 40, 102, NULL, NULL, hInstance, 0);

For the call to RegisterClassEx, I have this set for the background:

wcex.hbrBackground = (HBRUSH)0;

Here is my handler for WM_PAINT message:

 case WM_PAINT:
 {
   hdc = BeginPaint(hWnd, &ps);
   Gdiplus::Graphics graphics(hdc);
   graphics.DrawImage(*m_pBitmap, 0, 0);
   EndPaint(hWnd, &ps);
   break;
 }

One thing to note is that the application is always docked to the left of the screen and doesn't move. But, what's underneath the application may change as the user opens, closes or moves windows under it.

When the application first starts, it looks perfect. The transparent (and simi-transparent) parts of the PNG show through perfectly. BUT, when the background underneath the application changes, the background DOESN'T update, it just stays the same from when the application first started. In fact, WM_PAINT (or WM_ERASEBKGND does not get called when the background changes).

I've been playing with this for quite a while and have gotten close to getting 100% right, but not quite there. For instance, I've tried setting the background to (HBRUSH) NULL_BRUSH and I've tried handling WM_ERASEBKGND.

What can be done to get the window to repaint when the contents under it changes?

danronmoon
  • 3,613
  • 5
  • 32
  • 55
adoss
  • 1,061
  • 1
  • 9
  • 9
  • SetBKMode and SetBKColor are the APIs I have used to make the transparent parent control. –  Oct 19 '10 at 15:43

2 Answers2

44

I was able to do exactly what I wanted by using the code from Part 1 and Part 2 of this series:

Displaying a Splash Screen with C++


Those blog posts are talking about displaying a splash screen in Win32 C++, but it was almost identical to what I needed to do. I believe the part that I was missing was that instead of just painting the PNG to the window using GDI+, I needed to use the UpdateLayeredWindow function with the proper BLENDFUNCTION parameter. I'll paste the SetSplashImage method below, which can be found in Part 2 in the link above:

void SetSplashImage(HWND hwndSplash, HBITMAP hbmpSplash)
{
  // get the size of the bitmap
  BITMAP bm;
  GetObject(hbmpSplash, sizeof(bm), &bm);
  SIZE sizeSplash = { bm.bmWidth, bm.bmHeight };

  // get the primary monitor's info
  POINT ptZero = { 0 };
  HMONITOR hmonPrimary = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  MONITORINFO monitorinfo = { 0 };
  monitorinfo.cbSize = sizeof(monitorinfo);
  GetMonitorInfo(hmonPrimary, &monitorinfo);

  // center the splash screen in the middle of the primary work area
  const RECT & rcWork = monitorinfo.rcWork;
  POINT ptOrigin;
  ptOrigin.x = 0;
  ptOrigin.y = rcWork.top + (rcWork.bottom - rcWork.top - sizeSplash.cy) / 2;

  // create a memory DC holding the splash bitmap
  HDC hdcScreen = GetDC(NULL);
  HDC hdcMem = CreateCompatibleDC(hdcScreen);
  HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcMem, hbmpSplash);

  // use the source image's alpha channel for blending
  BLENDFUNCTION blend = { 0 };
  blend.BlendOp = AC_SRC_OVER;
  blend.SourceConstantAlpha = 255;
  blend.AlphaFormat = AC_SRC_ALPHA;

  // paint the window (in the right location) with the alpha-blended bitmap
  UpdateLayeredWindow(hwndSplash, hdcScreen, &ptOrigin, &sizeSplash,
      hdcMem, &ptZero, RGB(0, 0, 0), &blend, ULW_ALPHA);

  // delete temporary objects
  SelectObject(hdcMem, hbmpOld);
  DeleteDC(hdcMem);
  ReleaseDC(NULL, hdcScreen);
}
Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
adoss
  • 1,061
  • 1
  • 9
  • 9
  • best answer, also support automatic PNG alpha blend when used with WIC (Windows Imaging Component) via COM interface. – dns Jul 05 '15 at 01:51
  • Ended up wasting a lot of time with AlphaBlend. This solution is proabably one of the easiest ways to achieve Per-Pixel Alpha Blending, specially when you are updating the window. – TheBlueNotebook Jan 11 '16 at 12:30
21

Use the SetLayeredWindowAttributesarchive function, this allows you to set a mask color that will become transparent, thus allowing the background to show through.

You will also need to configure your window with the layered flag, e.g.:

SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

After that it's fairly simple:

// Make red pixels transparent:
SetLayeredWindowAttributes(hwnd, RGB(255,0,0), 0, LWA_COLORKEY);

When your PNG contains semi-transparent pixels that you want to blend with the background, this becomes more complicated. You could try looking at the approach in this CodeProject article:

Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above

Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
Simon Steele
  • 11,288
  • 3
  • 42
  • 67
  • 1
    I've tried this an it ALMOST works. The issue is that there are some areas of the PNG which are simi-transparent, and the color key method only makes the pixels which are 100% opacity of the color key color value transparent. If all else fails, my fallback will be to remove all the semi-transparent regions, but I'd really rather avoid doing that. – adoss Oct 19 '10 at 16:24
  • You can try more complicated hacks like the one in the article I've linked, but essentially as soon as you're looking to blend a pre-existing image with the desktop contents things start getting complicated. – Simon Steele Oct 19 '10 at 16:45
  • Check out the answer that I posted. I like that code a whole lot better than what I was seeing in this CodeProject article. Not only that, but I believe what I found is the proper solution instead of a hack. Thanks for the help! – adoss Oct 19 '10 at 19:05
  • Ah nice, I hadn't seen the BLENDFUNCTION stuff before - you're welcome! – Simon Steele Oct 20 '10 at 08:54
  • why don't you set the forth parameter of [SetLayeredWindowAttributes](https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setlayeredwindowattributes) to LWA_ALPHA – Valen Apr 18 '21 at 02:21