Skip to content

Commit

Permalink
Merge pull request #2648 from cwensley/curtis/wpf-fix-mouse-driver-crash
Browse files Browse the repository at this point in the history
Wpf: Fix crash with certain mouse drivers
  • Loading branch information
cwensley committed Apr 24, 2024
2 parents 008fb16 + d2339ee commit 376489d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Eto.Wpf/Forms/WpfWindow.cs
Expand Up @@ -152,6 +152,25 @@ private void Control_SourceInitialized(object sender, EventArgs e)
}
}

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)
{
// NOTE: If the window size is set, it will be made visible BEFORE this is called.
Expand All @@ -166,6 +185,8 @@ private void Control_Loaded(object sender, sw.RoutedEventArgs e)
SetSizeToContent();
if (Control.ShowActivated)
Control.MoveFocus(new swi.TraversalRequest(swi.FocusNavigationDirection.Next));

((swin.HwndSource)sw.PresentationSource.FromVisual(Control))?.AddHook(HookProc);
}

private void Control_SizeChanged(object sender, sw.SizeChangedEventArgs e)
Expand Down

0 comments on commit 376489d

Please sign in to comment.