Skip to content

Causes of WindowChrome flickering identified #3193

Open

Description

@ClosetBugSlayer

People have had problems with WindowChrome flickering. In my situation I tracked the bug down to the loop in my code which was changing the window title 3 times a second to "scroll" it like WinAmp.

In WindowChromeWorker._HandleSetTextOrIcon() the WPF framework code plays a trick where it clears WS_VISIBLE before forwarding WM_SETTEXT to DefWindowProc, and then sets it back after returning. The comment says this is done to trick DefWindowProc into not redrawing the caption.

In WindowChromeWorker._UpdateSystemMenu() which is called on every WM_SIZE, the code does the same exact trick with WS_VISIBLE.

This means that every time your window is resized, or the caption is changed, or the window icon is changed, WindowChrome will clear WS_VISIBLE and then reset it.

https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChromeWorker.cs

The problem is that this is the official way to hide and show a window and will not merely behave as a private-data-only change. If the DWM catches the window style change it will hide the window during the DefWindowProc call. Usually it happens fast but it can become a nuisance. This is horrible because if you try to click the window at the wrong time when it is briefly hidden, you will click into emptiness and activate the window underneath it instead. The Windows+Tab virtual desktop switcher is less subtle; every time the WndProc clears WS_VISIBLE for text or icon, the switcher will repeatedly remove the window from the list and add it back, shifting all the other windows around and causing a lot of redraws.

I don't know what the fix should be, but the current situation is unacceptable. Perhaps something could be done like in the _HandleNCActivate() call underneath, which passes an undocumented parameter to DefWindowProc to suppress unwanted behavior. In the meantime I need to phase out all usage of WindowChrome in my code until this is fixed and I advise others to do likewise.

Activity

  1. added
    Bug
    on Jun 29, 2020
  2. added this to the Future milestone on Jun 29, 2020
  3. batzen commented on Jul 1, 2020

    @batzen
    Contributor

    @ClosetBugSlayer you could switch to the WindowChromeBehavior from https://github.com/ControlzEx/ControlzEx
    We accept PRs, in case you find the same issues there. 😉

  4. davidbangMicrosoft commented on Oct 26, 2020

    @davidbangMicrosoft

    @batzen wondering what the support on ControlzEx will be going forward? I will be using WindowChrome in an app that will probably stay in prod.

  5. davidbangMicrosoft commented on Oct 26, 2020

    @davidbangMicrosoft

    @ClosetBugSlayer @batzen does the WindowChromeBehavior have the same issues as WindowChrome when it comes to rendering? It doesn't look like the project changes the underlying issue between races in the DWM, OS and WPF. I am referencing the code here: https://github.com/ControlzEx/ControlzEx/blob/develop/src/ControlzEx/Behaviors/WindowChromeBehavior.cs

  6. batzen commented on Oct 26, 2020

    @batzen
    Contributor

    @davidbangMicrosoft ControlzEx is the building block for Fluent.Ribbon (used in the new WinDbg) and MahApps.Metro. It's actively maintained and supports net45;net462;netcoreapp3.0;netcoreapp3.1 (net50 support is currently in a separate branch).
    The WindowChromeBehavior does, at least, not have as many issues as WindowChrome has (that's the reason why we created it in the first place).
    Do you have any specific issues in mind regarding rendering?

  7. davidbangMicrosoft commented on Oct 26, 2020

    @davidbangMicrosoft

    Great to hear that @batzen . Also yes I have been noticing many issues with WindowChrome when it comes to Window Management etc. Some of these have been resolved privately on the WPF side. Also for the rendering issue, it is similar to what @ClosetBugSlayer mentions in this bug. I haven't investigated as deeply, but seems to be an issue with the Window redrawing on each change such as resize, on load, etc.

  8. davidbangMicrosoft commented on Oct 26, 2020

    @davidbangMicrosoft

    @batzen to be honest I wish I had found your project much earlier as I would have likely used it. It is very helpful to look at your fixes though as I can reference them, if you don't mind me reusing privately. Also I am curious why this change hasn't been directly integrated into the WPF project?

  9. batzen commented on Oct 26, 2020

    @batzen
    Contributor

    @davidbangMicrosoft Feel free to reuse the fixes we made.
    As this repo (WPF) does not really process PRs as of now and the WindowChromeBehavior is quite different, in regards to supported features, supported frameworks etc., from the WindowChrome in WPF itself porting the fixes does not seem worth the effort to me.
    Many of the issues we fixed in ControlzEx were reported to Microsoft years ago and, sadly, nothing happened.
    Even Visual Studio does not use WindowChrome from WPF but a custom solution instead.

  10. ClosetBugSlayer commented on Oct 31, 2020

    @ClosetBugSlayer
    Author

    It looks like WindowChromeBehavior is doing the exact same thing to cause the flicker.

    https://github.com/ControlzEx/ControlzEx/blob/develop/src/ControlzEx/Behaviors/WindowChromeBehavior.MessageHandling.cs

    `
    private IntPtr _HandleSETICONOrSETTEXT(WM uMsg, IntPtr wParam, IntPtr lParam, out bool handled)
    {
    var modified = this._ModifyStyle(WS.VISIBLE, 0);

            // Setting the caption text and icon cause Windows to redraw the caption.
            // Letting the default WndProc handle the message without the WS_VISIBLE
            // style applied bypasses the redraw.
            var lRet = NativeMethods.DefWindowProc(this.windowHandle, uMsg, wParam, lParam);
    
            // Put back the style we removed.
            if (modified)
            {
                this._ModifyStyle(0, WS.VISIBLE);
            }
            handled = true;
            return lRet;
        }
    

    `

  11. batzen commented on Oct 29, 2021

    @batzen
    Contributor

    @ClosetBugSlayer FYI: The window chrome from ControlzEx no longer toggles the visible style.
    You can try it out by using the changes from ControlzEx/ControlzEx#151

  12. ClosetBugSlayer commented on Oct 30, 2021

    @ClosetBugSlayer
    Author

    @ClosetBugSlayer FYI: The window chrome from ControlzEx no longer toggles the visible style. You can try it out by using the changes from ControlzEx/ControlzEx#151

    Good deal. Mind summarizing the fix here so the WPF devs can get on it quickly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions