Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent Window Randomly Losing Transperancy #156

Closed
Dealman opened this issue Jan 21, 2024 · 11 comments
Closed

Transparent Window Randomly Losing Transperancy #156

Dealman opened this issue Jan 21, 2024 · 11 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Dealman
Copy link

Dealman commented Jan 21, 2024

This is mostly reproducible when moving a window around close to the edges of a monitor, or when resizing a lot/quickly.

image

These windows are usually a green tint, but if I resize them quickly with my mouse they will bug out like this and only a restart seems to fix it.

Not quite sure how to debug and potentially fix this.

This is the window style I use;

WindowStyle windowStyle = WindowStyle.Overlapped;
windowStyle |= WindowStyle.OverlappedWindow;
windowStyle ^= WindowStyle.ThickFrame;
this.SetWindowStyle(windowStyle);

The intended purpose is for the windows to be used as a sort of overlay and this styling disable the horrendously annoying aero snapping.

@dotMorten
Copy link
Owner

Could you please share a simple app that reproduces the issue?
Also which version of Windows?

@dotMorten dotMorten added bug Something isn't working More Info Needed labels Jan 21, 2024
@Dealman
Copy link
Author

Dealman commented Jan 21, 2024

Sure, here's a simple one setup via Template Studio. It was too big to upload directly via GitHub.

It seems to be mostly reproducible when resizing via the lower right corner, unless it's just a weird coincidence. The window may also randomly close whilst resizing.

I'm on Windows 11 Pro - Version 10.0.22631 Build 22631

Edit:

I grabbed a quick video to help showcase how I get the issue to occur.

Bug.mp4

@dotMorten
Copy link
Owner

I was able to reproduce the closing of the window. I never saw the transparency disappear, but did observe closing. No message/error was sent. To track what's going on, I added this to overlay window:

       this.Closed += OverlayWindow_Closed;
       this.AppWindow.Closing += AppWindow_Closing;
       this.AppWindow.Destroying += AppWindow_Destroying;
   }

   private void AppWindow_Destroying(Microsoft.UI.Windowing.AppWindow sender, object args)
   {
   }

   private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
   {
   }

   private void OverlayWindow_Closed(object sender, WindowEventArgs args)
   {
   }

Then the problem went away, which led me to believe this is WinAppSDK issue with references getting lost. So in MainPage.xaml.cs I created a variable to hold a reference to the Overlay window instead of just declaring a local reference to it in your click handler, and again problem went away, confirming you found an interesting C#WinRT issue, and not a WinUIEx issue.

Again I haven't been able to repro your transparency issue, but I do wonder if it also gets addressed by keeping a reference to the window?

@dotMorten
Copy link
Owner

I do wonder if you're hitting this issue that's been an issue on Windows for YEARS microsoft/WindowsAppSDK#4068

@Dealman
Copy link
Author

Dealman commented Jan 24, 2024

Very interesting, since there can potentially be more than one of these windows I'm now adding their references to a List and potentially a Dictionary in the future. Hasn't closed on me yet, the transparency however is very reproducible by just resizing it for a while.

When my window does this however it doesn't affect any other windows. Only the window I'm resizing.

Tried doing some more testing;

  1. If I have multiple instances open, they're all affected by this glitch once I've "triggered" it on one of them.
  2. If I try and open a new window after this glitch has occurred, I will get an error which I can't track down. It wants to attach a debugger, but there's already one attached.
  3. It seems to be some kind of buffer/memory relation potentially, it takes on average ~8 seconds of resizing for this glitch to trigger. Regardless if continuous or if I do it in segments with some pauses in between.
  4. Restarting the Desktop Window Manager does restore some level of transparency, but they're still glitchy.

I'm guessing some SDK version differences between us if you can't reproduce it? Unless it's hardware/driver related.

@dotMorten
Copy link
Owner

most likely hardware related. We're using the same version since I'm using your project to repro. I'm closing this out since this doesn't seem to be caused by WinUIEx itself - all the transparency rendering and maintaining it is handled by WinUI.

@dotMorten
Copy link
Owner

Could also be this: microsoft/microsoft-ui-xaml#8423
It was fixed in the latest 1.5exp2. Perhaps you could confirm with that version whether you still see it?

@Dealman
Copy link
Author

Dealman commented Feb 2, 2024

I tried updating it and sadly, no difference. I also tried disabling the SystemBackdrop on my main window but the issue persists. I've also updated my GPU drivers to the latest.

Guess maybe I should post my findings as an issue over there instead?

@softworkz
Copy link

most likely hardware related. We're using the same version since I'm using your project to repro. I'm closing this out since this doesn't seem to be caused by WinUIEx itself - all the transparency rendering and maintaining it is handled by WinUI.

@dotMorten - Not quite. You are creating a new brush for clearing the background on each call.

private bool ClearBackground(nint hwnd, nint hdc)
{
if (PInvoke.GetClientRect(new Windows.Win32.Foundation.HWND(hwnd), out var rect))
{
var brush = PInvoke.CreateSolidBrush(0);
FillRect(hdc, ref rect, brush);
return true;
}
return false;
}

This simply hits a limit at some point and then you cannot create any more new brushes - then the transparency gets lost.

To reproduce, you just need to move the window around for a while and be a bit patient, then it will happen.

You should create the brush once and cache it. On dispose, pinvoke DeleteObject, etc..

@dotMorten
Copy link
Owner

@softworkz Nice find. Not sure what I was thinking there :-)

@dotMorten dotMorten reopened this May 3, 2024
@dotMorten dotMorten added help wanted Extra attention is needed good first issue Good for newcomers and removed More Info Needed labels May 3, 2024
@softworkz
Copy link

Not sure what I was thinking there :-)

In my case it's usually "first, try whether it works and do it right later". And then forget about it... ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants