Skip to content

[Bug] Set WindowChrome.NonClientFrameEdges will cause UI layout problem #11204

Open

Description

@YerongAI

Description

When I set WindowChrome.NonClientFrameEdges="Left,Right,Bottom", the UI elements layout will be misaligned after switch Windows theme or color mode.

Reproduction Steps

Create a blank WPF project, set the WindowChrome like that:

    <WindowChrome.WindowChrome>
        <WindowChrome NonClientFrameEdges="Left,Right,Bottom"/>
    </WindowChrome.WindowChrome>

Put any visiable element on the window, like:

<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="123"/>

Run the project.

Image

Switch Windows theme or color mode. The UI elements layout will be misaligned.

Image

Expected behavior

There should be no issues with UI elements.

Actual behavior

UI elements are misaligned.

In another project of mine, frequently switching color modes caused UI elements to become constantly misaligned. In other words, as long as I kept switching, the elements would shift until they disappeared.

Regression?

No response

Known Workarounds

Set the WindowChrome.NonClientFrameEdges="None" can avoids this problem, but I need to use it. This isn't really a solution for me.

Impact

No response

Configuration

  • .NET 8.0 and .NET 9.0.
  • Windows 11 25H2 build 26200.6899.
  • x64

Other information

No response

Activity

  1. lindexi commented on Oct 23, 2025

    @lindexi
    Member

    Thank you for your report.

    I can not repro this issue by switch ThemeMode:

    You can find my demo project in: https://github.com/lindexi/lindexi_gd/tree/890bfffc2abe5c4595aa8da93a931e6c203dddb9/WPFDemo/LewerekeekalurcemFoqihairhilaw

    MainWindow.xaml:

    <Window x:Class="LewerekeekalurcemFoqihairhilaw.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:LewerekeekalurcemFoqihairhilaw"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <WindowChrome.WindowChrome>
            <WindowChrome NonClientFrameEdges="Left,Right,Bottom"/>
        </WindowChrome.WindowChrome>
    
        <Grid RowDefinitions="*,Auto">
            <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="123"/>
    
            <ToggleButton x:Name="SwitchThemeModeButton" Grid.Row="1" Margin="10,10,10,10" Click="SwitchThemeModeButton_OnClick">Switch Theme Mode</ToggleButton>
        </Grid>
    </Window>

    MainWindow.xaml.cs:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ThemeMode = ThemeMode.Light;
        }
    
        private void SwitchThemeModeButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (SwitchThemeModeButton.IsChecked is true)
            {
                ThemeMode = ThemeMode.Dark;
            }
            else
            {
                ThemeMode = ThemeMode.Light;
            }
        }
    }

    And sorry I can not switch my system theme mode, because some of my application will crash when switch the theme.

  2. YerongAI commented on Oct 23, 2025

    @YerongAI
    Author

    Switch system color mode to repro the issue.

    Screen.Recoding.20251023-9131.mp4

    Most of time, the UI is able to repaint and return to normal.

    But it's not working on my other projects. Like this:

    Screen.Recoding.20251023-91748.mp4
  3. added
    Investigate
    on Oct 23, 2025
  4. lindexi commented on Oct 28, 2025

    @lindexi
    Member

    ~~ It seems that skaking by Size frameSize = GetHwndNonClientAreaSizeInMeasureUnits();~~

    The Shaking looks like the Windows 11 render issues. (Update : it seems the windows as design issues, not only the windows 11). I can see the whole UI move to left before wpf do anything.

    Image

    @YerongAI I try your https://github.com/YerongAI/Office-Tool/releases/tag/v10.27.24.0 , but I can not repro this issues.

    Could you provide the version which can repro this issues as your attachment video? Or debugging with WPF debug version?

    我没法通过你的公开发布版本复现此问题,能否重新构建一个能复现问题的版本给我呢? 这个问题看起来很像是渲染的时候,系统 DWM 将整个窗口画面整体偏移,随后 WPF 重新布局渲染就能正常。但是你附加的视频里面,越来越偏的问题,我不能复现出来,也无法调试。 如果不方便提供上述视频的版本给我,你也好自己调试,只需拉取 WPF 项目,然后进行构建即可。构建方法是执行以下命令行:

    build.cmd -pack -ci -configuration Release -prepareMachine /p:Platform=x86

    Image

  5. YerongAI commented on Oct 28, 2025

    @YerongAI
    Author

    ~~ It seems that skaking by Size frameSize = GetHwndNonClientAreaSizeInMeasureUnits();~~

    The Shaking looks like the Windows 11 render issues. I can see the whole UI move to left before wpf do anything.

    Image [@YerongAI](https://github.com/YerongAI) I try your https://github.com/YerongAI/Office-Tool/releases/tag/v10.27.24.0 , but I can not repro this issues.

    Could you provide the version which can repro this issues as your attachment video? Or debugging with WPF debug version?

    我没法通过你的公开发布版本复现此问题,能否重新构建一个能复现问题的版本给我呢? 这个问题看起来很像是渲染的时候,系统 DWM 将整个窗口画面整体偏移,随后 WPF 重新布局渲染就能正常。但是你附加的视频里面,越来越偏的问题,我不能复现出来,也无法调试。 如果不方便提供上述视频的版本给我,你也好自己调试,只需拉取 WPF 项目,然后进行构建即可。构建方法是执行以下命令行:

    build.cmd -pack -ci -configuration Release -prepareMachine /p:Platform=x86
    Image Image

    Set window foreground and background to repro the issue.

    For example:

    Foreground="Black" Background="White"

    I discovered this issue during development, so the relevant version has not been released.

  6. lindexi commented on Oct 28, 2025

    @lindexi
    Member

    @YerongAI I add the Foreground="Black" Background="White" to my demo window. But I can not repro this issues. Did I miss anything?

  7. YerongAI commented on Oct 28, 2025

    @YerongAI
    Author

    @YerongAI I add the Foreground="Black" Background="White" to my demo window. But I can not repro this issues. Did I miss anything?

    • .NET 8.0.
    • Screen resolution: 3840 * 2160.
    • Scale: 200%.
    • Create normal WPF project.
    • Set window Foreground and Background.
    • Set WindowChrome.
    • Run app.
    • Change color mode.

    Full code:

    <Window x:Class="WpfApp1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Foreground="Black" Background="White"
            Title="MainWindow" Height="450" Width="800">
        <WindowChrome.WindowChrome>
            <WindowChrome NonClientFrameEdges="Left,Right,Bottom"/>
        </WindowChrome.WindowChrome>
        <Grid>
            <TextBlock Text="ABCD" FontSize="20"/>
        </Grid>
    </Window>

    Nothing else has changed.

  8. lindexi commented on Oct 28, 2025

    @lindexi
    Member

    @YerongAI Can you help me to test: add the ThemeMode = ThemeMode.Light; in .ctor ?

    #pragma warning disable WPF0001
        public MainWindow()
        {
            InitializeComponent();
            ThemeMode = ThemeMode.Light;
        }

    Build with .NET 9/10

  9. YerongAI commented on Oct 28, 2025

    @YerongAI
    Author

    @YerongAI Can you help me to test: add the ThemeMode = ThemeMode.Light; in .ctor ?

    #pragma warning disable WPF0001
    public MainWindow()
    {
    InitializeComponent();
    ThemeMode = ThemeMode.Light;
    }
    Build with .NET 9/10

    Adding the ThemeMode = ThemeMode.Light can avoid the problem, but the UI elements still shaking.

  10. lindexi commented on Oct 28, 2025

    @lindexi
    Member

    @YerongAI Thank you. And how about this code:

    <Window x:Class="LewerekeekalurcemFoqihairhilaw.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:LewerekeekalurcemFoqihairhilaw"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800"
            Foreground="Black" Background="White">
        <WindowChrome.WindowChrome>
            <WindowChrome NonClientFrameEdges="Left,Right,Bottom"/>
        </WindowChrome.WindowChrome>
    
        <Grid RowDefinitions="*,Auto">
            <TextBlock x:Name="TheTextBlock" HorizontalAlignment="Left" VerticalAlignment="Center" Text="123"/>
    
            <Button x:Name="UpdateButton" Margin="10,10,10,10" VerticalAlignment="Bottom" Click="UpdateButton_OnClick">Update</Button>
        </Grid>
    </Window>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void UpdateButton_OnClick(object sender, RoutedEventArgs e)
        {
            TheTextBlock.Text = $"abc{Random.Shared.Next(10)}";
            InvalidateVisual();
            UpdateLayout();
        }
    }

    After switch the system theme color, you can click the UpdateButton to re-render the window.

  11. lindexi commented on Oct 28, 2025

    @lindexi
    Member

    After my investigation, I believe the root cause is the system-level rendering. With the window styled as you described, changing the system color causes the rendered window content to be displaced by the rendering layer. Why does setting ThemeMode produce only a “shaking” effect? Because when ThemeMode is set, theme resources are injected into the window’s Resources. A system color change then triggers those theme resources to change, which forces a full window refresh. The refresh causes the window to be re-rendered, which updates and corrects the visual content. Visually, what happens is: on system color change the low-level renderer first shifts the window content (e.g., leftward), then WPF updates the theme resources and re-renders the UI, restoring the correct appearance—this sequence is perceived as the “shaking” effect.

    经过了我的调查,我认为当前的问题是系统渲染的问题。当窗口样式如当前描述的状态时,切换颜色时,将会从渲染层移动窗口的内容。那为什么设置了 ThemeMode 之后就只会看到 shaking 效果?那是因为当设置 ThemeMode 之后,将会在当前窗口的资源里面塞入主题资源,变更系统颜色时将会触发主题资源变更,进而导致全窗口刷新。窗口刷新时将会带来窗口重新渲染,窗口重新渲染将会更新渲染界面,从而修复界面渲染内容。人类可以看到的效果就是,先在变更系统颜色时,由系统底层渲染窗口内容向左偏移,随后 WPF 框架将变更主题资源,重新渲染界面,从而让界面再次正确地渲染出来,这就是 shaking 效果的原因

    但为什么在这样的窗口状态下,系统底层渲染就要将窗口界面内容向左移动,这点我还没调查到本质原因。其中的猜测是原本这是阴影部分的内容,合成器之类的模块以为要去填补阴影部分,于是移动窗口内容

  12. lindexi commented on Oct 28, 2025

    @lindexi
  13. YerongAI commented on Oct 28, 2025

    @YerongAI
    Author

    I think the problem should be fixed by .NET Team or Windows Team. The UI doesn't just shake when you change color, it can happen at any time.

    Also the UI is not 100% re-rendered and sometimes remains misaligned.

  14. lindexi commented on Oct 29, 2025

    @lindexi
    Member

    I'm worried that this is the bug as design in Windows.

  15. MichaeIDietrich commented on Oct 31, 2025

    @MichaeIDietrich
    Contributor

    The root problem comes from using WindowChrome as it comes with several known flaws.
    The displacement we see here seems to originate from WindowChrome overriding NC_CALCSIZE and it looks like the issue can be avoided when returning WVR_VALIDRECTS for this message, but instead it currently returns WVR_REDRAW which causes the issue when changing the theme.

    There are alternatives out there which seem not to suffer from this issue, so I suggest to simply switch to some of those, e.g. ControlzEx's WindowChromeBehavior

    See implementations for comparison:

    https://github.com/ControlzEx/ControlzEx/blob/5f7db23b818e1aad8abfd7142f2d7af2c597e3d7/src/ControlzEx/Behaviors/WindowChrome/WindowChromeBehavior.MessageHandling.cs#L400

    If you still want to stick with WindowChrome you should be able to work around this by forcing a redraw by handling WM_SETTINGCHANGE and WM_NCPAINT and directly invoking InvalidateRect (user32.dll).

  16. lindexi commented on Nov 1, 2025

    @lindexi
    Member

    @YerongAI Yeah, I agree with @MichaeIDietrich . And I think https://github.com/ControlzEx/ControlzEx is the better solution.

    As @MichaeIDietrich says, the better workaround is call the InvalidateRect :

        public MainWindow()
        {
            InitializeComponent();
    
            SourceInitialized += OnSourceInitialized;
        }
    
        private void OnSourceInitialized(object? sender, EventArgs e)
        {
            var windowInteropHelper = new WindowInteropHelper(this);
            var hwnd = windowInteropHelper.Handle;
    
            HwndSource source = HwndSource.FromHwnd(hwnd)!;
            source.AddHook(Hook);
        }
    
        private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled)
        {      
            const int WM_NCPAINT = 0x0085;
            const int WM_SETTINGCHANGE = 0x001A;
            if (msg is WM_NCPAINT or WM_SETTINGCHANGE)
            {
                Window window = this;
                var rect = new Int32Rect(0,0, (int)Math.Ceiling(window.ActualWidth), (int) Math.Ceiling(window.ActualHeight));
                unsafe
                {
                    InvalidateRect(hwnd, &rect, true);
                }
            }
    
            return IntPtr.Zero;
        }
    
        [DllImport("User32")]
        private static extern unsafe bool InvalidateRect(IntPtr hwnd, Int32Rect* lpRect, bool bErase);

    You can find all my demo code in : https://github.com/lindexi/lindexi_gd/tree/9bc7db74f69823d0c0d762492dea640c8048d67c/WPFDemo/LellaibeayeelaRakearjemfal


    What is WindowChrome comes with several known flaws?

    Such as:

  17. YerongAI commented on Nov 2, 2025

    @YerongAI
    Author

    The root problem comes from using WindowChrome as it comes with several known flaws. The displacement we see here seems to originate from WindowChrome overriding NC_CALCSIZE and it looks like the issue can be avoided when returning WVR_VALIDRECTS for this message, but instead it currently returns WVR_REDRAW which causes the issue when changing the theme.

    There are alternatives out there which seem not to suffer from this issue, so I suggest to simply switch to some of those, e.g. ControlzEx's WindowChromeBehavior

    See implementations for comparison:

    wpf/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChromeWorker.cs

    Line 445 in 808031c

    retVal = new IntPtr((int) (WVR.REDRAW));
    https://github.com/ControlzEx/ControlzEx/blob/5f7db23b818e1aad8abfd7142f2d7af2c597e3d7/src/ControlzEx/Behaviors/WindowChrome/WindowChromeBehavior.MessageHandling.cs#L400

    If you still want to stick with WindowChrome you should be able to work around this by forcing a redraw by handling WM_SETTINGCHANGE and WM_NCPAINT and directly invoking InvalidateRect (user32.dll).

    If WindowChrome comes with several known issues, why not fix them on .NET repo?

  18. lindexi commented on Nov 3, 2025

    @lindexi
    Member

    If WindowChrome comes with several known issues, why not fix them on .NET repo?

    @YerongAI Good question. The answer is from Bastian Schmidt in #3193 (comment)

  19. YerongAI commented on Nov 5, 2025

    @YerongAI
    Author

    I'm changging WindowChrome.NonClientFrameEdges="Left,Right,Bottom" to WindowChrome.NonClientFrameEdges="Right,Bottom", it looks good now.

  20. YerongAI commented on Nov 5, 2025

    @YerongAI
    Author

    I'm changging WindowChrome.NonClientFrameEdges="Left,Right,Bottom" to WindowChrome.NonClientFrameEdges="Right,Bottom", it looks good now.

    That's not a prefect solution, even tried ControlzEx. After using ControlzEx, my program window flickers during initialization and does not work with the existing theme components.

    Setting WindowChrome.NonClientFrameEdges will also cause issue about RTL layout.

    I'm about quitting all of this work, it's too hard for me, and not worth it.

    Edit:

    I'm now using the following settings:

    • WindowChrome.NonClientFrameEdges = "None".
    • Window BorderThickness = 1, and set to 0 when window maximized.

    Just have a issue with Aero button.

  21. lindexi commented on Nov 6, 2025

    @lindexi
    Member

    That's not a prefect solution, even tried ControlzEx. After using ControlzEx, my program window flickers during initialization and does not work with the existing theme components.

    @YerongAI Sorry to hear that.

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

    Investigate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions