Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Fix crash with certain mouse drivers #2648

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading