Et3rnos
Et3rnos

Reputation: 23

Why isn't SetLayeredWindowAttributes() working on my WinUI 3 desktop overlay background?

I'm trying to create a desktop overlay using WINUI 3 (Windows App SDK 1.3).

I'm trying to make the window background transparent, just like you can do in WinForms by using TransparencyKey. However, since as far as I know, WinUI 3 doesn't have such a convenient way to achieve that, I had to use SetLayeredWindowAttributes. However, the following code does not change the window transparency in any way:

SetLayeredWindowAttributes(_hWnd, 0, 128, LWA_COLORKEY);

However, globally changing the transparency works:

SetLayeredWindowAttributes(_hWnd, 0, 128, LWA_ALPHA);

Which is very confusing to me.

This is not exactly my whole code but it's the minimum you need to replicate the problem:

InitializeComponent();

_hWnd = WindowNative.GetWindowHandle(this);

var exStyle = (User32.SetWindowLongFlags)User32.GetWindowLong(_hWnd, User32.WindowLongIndexFlags.GWL_EXSTYLE);
exStyle |= User32.SetWindowLongFlags.WS_EX_LAYERED;
User32.SetWindowLong(_hWnd, User32.WindowLongIndexFlags.GWL_EXSTYLE, exStyle);

SetLayeredWindowAttributes(_hWnd, 0, 128, LWA_COLORKEY);

This is what I am getting:

transparency not working

I was expecting the black background to become transparent.

To sum up, this line of code is not working as expected: SetLayeredWindowAttributes(_hWnd, 0, 128, LWA_COLORKEY)

Upvotes: 0

Views: 426

Answers (0)

Related Questions