1

I'm trying to create a fully transparent window, which also has click-through behavior, except for the few UI controls on it.

All works well in principle, but I just noticed that click-through only works for certain transparency color keys. Red and blue must be equal, green doesn't matter and can be anything. Transparency seems to work for all color keys. The only hint I found was an old article about Windows 7 Aero themes interfering with specific colors. But I'm on Windows 11 and I couldn't figure out what's going on.

Does anybody know what causes this behavior and how it can be worked around? Thanks!

COLORREF const TRANS_COLOR = RGB(0xFF, 0, 0xFF); // works
//COLORREF const TRANS_COLOR = RGB(0, 0xFF, 0xFF); // doesn't work!

...

hWndMain = CreateWindowEx(WS_EX_TOPMOST, szWndClassMain, szWndTitleMain, WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInst, NULL);

//SetWindowLong(hWndMain, GWL_EXSTYLE, GetWindowLong(hWndMain, GWL_EXSTYLE) | WS_EX_TRANSPARENT);
SetWindowLong(hWndMain, GWL_EXSTYLE, GetWindowLong(hWndMain, GWL_EXSTYLE) | WS_EX_LAYERED);
SetLayeredWindowAttributes(hWndMain, TRANS_COLOR, 0, LWA_COLORKEY);

...

WNDCLASS wc;
wc.hbrBackground = CreateSolidBrush(TRANS_COLOR);
...
RegisterClass(&wc);

Simpleton
  • 632
  • 5
  • 22
  • 1
    *"The only hint I found was an old article about Windows 7 Aero themes interfering with specific colors."* - *"Old"* rarely equates to *"no longer relevant"*. Might want to add a link to said article, as it might provide just enough information to figure out what's going on and how to work around (or identify) the problematic behavior. – IInspectable Jun 06 '23 at 14:51
  • 1
    https://stackoverflow.com/questions/4448771/c-sharp-form-transparencykey-working-different-for-different-colors-why – Hans Passant Jun 06 '23 at 19:22
  • Hello, I can reproduce it on Windows 11. I'll consult it. – YangXiaoPo-MSFT Jun 07 '23 at 02:27
  • @IInspectable It was in one of the gazillion tabs I opened in search for this issue and had already closed them. Furthermore, it was on the infamous MS forum, so nothing much transpired from it. The gist was that they couldn't use the color white as transparency key and somehow traced that to Aero theme settings. I couldn't apply that to my problem because on Win11 I can't enable/disable Aero and changing any of the other color and transparency settings didn't have any effect. So it was just that: a hint. – Simpleton Jun 07 '23 at 08:47
  • Thanks @HansPassant ! So it's indeed an ancient bug that probably won't get fixed, ever. I can confirm that red and blue just have to be equal, not 255. Will update my question accordingly. Incidentally, we used Fuchsia too before we changed it and ran into this problem. :) Now, if only that could be used reliably to our advantage, as OP in your linked question did... – Simpleton Jun 07 '23 at 08:53
  • So now I tried to utilize this effect to add transparent but non-click-through areas by adding layered child windows. That didn't work, but in order to do so I had to add a manifest specifying Win10 as a target. With this manifest, the effect is gone. Transparency color keys with unequal red and blue components are now transparent and click-through. – Simpleton Jun 07 '23 at 10:47
  • 1
    That comment should really be an answer. Even though it doesn't fully explain the core issue, it sufficiently explains how to opt out of legacy/compatibility behavior. – IInspectable Jun 07 '23 at 12:10

1 Answers1

3

Combining Hans' and my own research from the comments into an answer:

  • This is an ancient Windows bug related to Aero and the DWM (desktop window manager). The red and blue components of the transparency color key must be equal to enable click-through behavior.
  • Layered top-level windows have been supported since Windows 2000. Layered child windows have been supported since Windows 8. Apparently there were some code changes which you can enable by adding an application manifest targeting Windows 8 (I used Windows 10). When you do that, the bug is no longer noticeable and all transparency color keys have click-through behavior now.
Simpleton
  • 632
  • 5
  • 22