MahApps / MahApps.Metro

A framework that allows developers to cobble together a better UI for their own WPF applications with minimal effort.
https://mahapps.com
MIT License
9.29k stars 2.45k forks source link

Crash when changing screen resolution #3409

Closed rhinterndorfer closed 4 years ago

rhinterndorfer commented 5 years ago

WPF application using MahApp MetroWindow crashes with unhandled exception when reducing screen resolution and the window width and height properties are defined.

Problem occurs on remote desktop environments when connecting with different clients (screen resolutions) to terminal servers.

Steps to reproduce the behavior:

Following simple xaml has been used to reproduce the behavior:

<Metro:MetroWindow 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:Metro="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Background="WhiteSmoke"
        Title="MainWindow" 
        Height="768" 
        Width="1024">
    <Grid>
    </Grid>
</Metro:MetroWindow>
  1. Open the app on a high resolution (3840x2160 and 150% text size) screen and maximize the window.
  2. Reduce the resolution of the screen while app is maximized to 1024x768
  3. Minimize the application and the application will crash with an unhandled exception

Detailed windbg exception information:

0:000> !pe
Exception object: 0248cc60
Exception type:   System.ArgumentException
Message:          Width and Height must be non-negative.
InnerException:   <none>
StackTrace (generated):
<none>
StackTraceString: <none>
HResult: 80070057

0:000> !CLRStack
OS Thread Id: 0x12d4 (0)
Child SP       IP Call Site
004ddc10 748a5ef8 [HelperMethodFrame: 004ddc10] 
004ddcc0 6c8a204e System.Windows.Rect..ctor(Double, Double, Double, Double)
004ddcec 69696cd4 System.Windows.Window.GetNormalRectDeviceUnits(IntPtr)
004ddd68 69696d22 System.Windows.Window.GetNormalRectLogicalUnits(IntPtr)
004ddde0 696964aa System.Windows.Window.get_RestoreBounds()
004dddf0 69696f31 System.Windows.Window.UpdateDimensionsToRestoreBounds()
004dde2c 6a3dee8c System.Windows.Window.WmSizeChanged(IntPtr)
004dde90 6970c4dc System.Windows.Window.WindowFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
004ddeb4 6a8e9703 System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
004ddee8 6c60c28b MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
004ddf24 6c60c1db MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
004ddf34 6c60c062 System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
004ddf54 6c60bfa4 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
004ddf98 6c60a581 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
004ddff4 6c60baae MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
004de4b8 005ed0b9 [InlinedCallFrame: 004de4b8] MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004de4b4 6c60c380 MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
004de628 005ed0b9 [InlinedCallFrame: 004de628] 
004de624 6c63939e DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004de628 6c60bae9 [InlinedCallFrame: 004de628] MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004de664 6c60bae9 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
004debe0 005ed0b9 [InlinedCallFrame: 004debe0] MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004debdc 6c60c380 MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
004ded50 005ed0b9 [InlinedCallFrame: 004ded50] 
004ded4c 6c63939e DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004ded50 6c60bae9 [InlinedCallFrame: 004ded50] MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
004ded8c 6c60bae9 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
004def34 005ed0b9 [InlinedCallFrame: 004def34] 
004def30 6c639678 DomainBoundILStubClass.IL_STUB_PInvoke(System.Windows.Interop.MSG ByRef)
004def34 6c60857d [InlinedCallFrame: 004def34] MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
004def68 6c60857d System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
004defb4 6c60816e System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
004defc0 696a4eca System.Windows.Application.RunDispatcher(System.Object)
004defd0 696a4d60 System.Windows.Application.RunInternal(System.Windows.Window)
004deff0 696a4b07 System.Windows.Application.Run(System.Windows.Window)
004df000 696a46e4 System.Windows.Application.Run()
004df00c 0065049c WpfApp1.App.Main()
004df188 7093ebb6 [GCFrame: 004df188] 

Screenshots

crash_highres_maximized crash_lowres_maximized crash_lowres_minimized crash_dpi_settings

Environment:

punker76 commented 5 years ago

@rhinterndorfer Is it possible for you to test the latest alpha version 2.0.0-alpha0170 (on Nuget)?

rhinterndorfer commented 5 years ago

Same problem with 2.0.0-alpha0170. crash_alpha0170_settings crash_alpha0170_lowres

rhinterndorfer commented 5 years ago

We currently use a workaround that detects the screen resolution change and sets window state to normal and immediately to maximized afterwards if the app is maximized in code behind file "MainWindow.xaml.cs".

        public MainWindow()
        {
            InitializeComponent();
            SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
        }
        private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowState = WindowState.Normal;
                this.WindowState = WindowState.Maximized;
            }
        }
AKruimink commented 4 years ago

I asume the resolution size is very specific? beccause im unable to recreate it with my 2560 x 1440 monitor on the latest developer's version, even when throwing the zoom on 225% en changes resolution to 800x600.

punker76 commented 4 years ago

I’m closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on for so long, then it seems no one has the time to implement this.

Please reopen if you still encounter this issue with the latest stable version and then please use the issue template.

If you have enough time then it would be nice to test this issue against the latest alpha version (availabe via NuGet and appveyor).

Thank you! 🙏