You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Set WindowChrome.NonClientFrameEdges will cause UI layout problem #11204
Switch Windows theme or color mode. The UI elements layout will be misaligned.
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.
~~ 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.
~~ 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.
[@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?
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.
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
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).
publicMainWindow(){InitializeComponent();SourceInitialized+=OnSourceInitialized;}privatevoidOnSourceInitialized(object?sender,EventArgse){varwindowInteropHelper=newWindowInteropHelper(this);varhwnd=windowInteropHelper.Handle;HwndSourcesource=HwndSource.FromHwnd(hwnd)!;source.AddHook(Hook);}privateIntPtrHook(IntPtrhwnd,intmsg,IntPtrwparam,IntPtrlparam,refboolhandled){constintWM_NCPAINT=0x0085;constintWM_SETTINGCHANGE=0x001A;if(msgisWM_NCPAINT or WM_SETTINGCHANGE){Windowwindow=this;varrect=newInt32Rect(0,0,(int)Math.Ceiling(window.ActualWidth),(int)Math.Ceiling(window.ActualHeight));unsafe{InvalidateRect(hwnd,&rect,true);}}returnIntPtr.Zero;}[DllImport("User32")]privatestaticexternunsafeboolInvalidateRect(IntPtrhwnd,Int32Rect*lpRect,boolbErase);
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
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?
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.
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.
Activity
lindexi commentedon Oct 23, 2025
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:
MainWindow.xaml.cs:
And sorry I can not switch my system theme mode, because some of my application will crash when switch the theme.
YerongAI commentedon Oct 23, 2025
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
lindexi commentedon Oct 28, 2025
~~ 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.
@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 项目,然后进行构建即可。构建方法是执行以下命令行:
YerongAI commentedon Oct 28, 2025
Set window foreground and background to repro the issue.
For example:
I discovered this issue during development, so the relevant version has not been released.
lindexi commentedon Oct 28, 2025
@YerongAI I add the
Foreground="Black" Background="White"to my demo window. But I can not repro this issues. Did I miss anything?YerongAI commentedon Oct 28, 2025
Full code:
Nothing else has changed.
lindexi commentedon Oct 28, 2025
@YerongAI Can you help me to test: add the
ThemeMode = ThemeMode.Light;in .ctor ?Build with .NET 9/10
YerongAI commentedon Oct 28, 2025
Adding the
ThemeMode = ThemeMode.Lightcan avoid the problem, but the UI elements still shaking.lindexi commentedon Oct 28, 2025
@YerongAI Thank you. And how about this code:
After switch the system theme color, you can click the UpdateButton to re-render the window.
lindexi commentedon Oct 28, 2025
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 效果的原因
但为什么在这样的窗口状态下,系统底层渲染就要将窗口界面内容向左移动,这点我还没调查到本质原因。其中的猜测是原本这是阴影部分的内容,合成器之类的模块以为要去填补阴影部分,于是移动窗口内容
lindexi commentedon Oct 28, 2025
YerongAI commentedon Oct 28, 2025
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.
lindexi commentedon Oct 29, 2025
I'm worried that this is the bug as design in Windows.
MichaeIDietrich commentedon Oct 31, 2025
The root problem comes from using
WindowChromeas it comes with several known flaws.The displacement we see here seems to originate from
WindowChromeoverridingNC_CALCSIZEand it looks like the issue can be avoided when returningWVR_VALIDRECTSfor this message, but instead it currently returnsWVR_REDRAWwhich 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
WindowChromeBehaviorSee implementations for comparison:
wpf/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Shell/WindowChromeWorker.cs
Line 445 in 808031c
https://github.com/ControlzEx/ControlzEx/blob/5f7db23b818e1aad8abfd7142f2d7af2c597e3d7/src/ControlzEx/Behaviors/WindowChrome/WindowChromeBehavior.MessageHandling.cs#L400
If you still want to stick with
WindowChromeyou should be able to work around this by forcing a redraw by handlingWM_SETTINGCHANGEandWM_NCPAINTand directly invokingInvalidateRect(user32.dll).lindexi commentedon Nov 1, 2025
@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:You can find all my demo code in : https://github.com/lindexi/lindexi_gd/tree/9bc7db74f69823d0c0d762492dea640c8048d67c/WPFDemo/LellaibeayeelaRakearjemfal
What is
WindowChromecomes with several known flaws?Such as:
YerongAI commentedon Nov 2, 2025
If
WindowChromecomes with several known issues, why not fix them on .NET repo?lindexi commentedon Nov 3, 2025
@YerongAI Good question. The answer is from Bastian Schmidt in #3193 (comment)
YerongAI commentedon Nov 5, 2025
I'm changging
WindowChrome.NonClientFrameEdges="Left,Right,Bottom"toWindowChrome.NonClientFrameEdges="Right,Bottom", it looks good now.YerongAI commentedon Nov 5, 2025
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.NonClientFrameEdgeswill 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:
Just have a issue with Aero button.
lindexi commentedon Nov 6, 2025
@YerongAI Sorry to hear that.