Skip to content

Commit

Permalink
Wpf: Fix crash with certain mouse drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Apr 24, 2024
1 parent 008fb16 commit d2339ee
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Eto.Wpf/Forms/WpfWindow.cs
Original file line number Diff line number Diff line change
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 d2339ee

Please sign in to comment.