From fb78b41211e58e4f361e0232b3a41f57aa1a9e84 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Wed, 24 Apr 2024 15:57:29 -0700 Subject: [PATCH] Wpf: Fix crash with certain mouse drivers --- src/Eto.Wpf/Forms/WpfWindow.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Eto.Wpf/Forms/WpfWindow.cs b/src/Eto.Wpf/Forms/WpfWindow.cs index ed03b78d2..0e4bb7c3b 100755 --- a/src/Eto.Wpf/Forms/WpfWindow.cs +++ b/src/Eto.Wpf/Forms/WpfWindow.cs @@ -150,6 +150,27 @@ private void Control_SourceInitialized(object sender, EventArgs e) SetLocation(initialLocation.Value); initialLocation = null; } + + ((swin.HwndSource)sw.PresentationSource.FromVisual(Control)).AddHook(HookProc); + } + + private IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + if (msg == 0x0084 /*WM_NCHITTEST*/ ) + { + // This prevents a crash in WindowChromeWorker._HandleNCHitTest with some mouse drivers + // See https://github.com/dotnet/wpf/issues/6777 for more information + // This was fixed in .NET 8 but this is here to avoid the crash in .NET Framework 4.8 and .NET 7. + try + { + lParam.ToInt32(); + } + catch (OverflowException) + { + handled = true; + } + } + return IntPtr.Zero; } private void Control_Loaded(object sender, sw.RoutedEventArgs e)