Mouse.GetPosition throws 'Invalid window handle' when called on UI element loading right after a child window was closed
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
* .NET Core Version: **.NET SDK, Version: 7.0.101, Commit: bb24aafa11**
* Windows version: **Version 22h2 (OS Build 22621.963)**
* Does the bug reproduce also in WPF for .NET Framework 4.8?: **Yes**
* Is this bug related specifically to tooling in Visual Studio (e.g. XAML Designer, Code editing, etc...)?: **No**
---
**Problem description:**
An attempt to call the `Mouse.GetPosition` method for an UI element in its `Loaded` event handler right after a child window was closed lead to the "System.ComponentModel.Win32Exception: 'Invalid window handle.'" error. This issue occurs only if the mouse pointer is over the UI element that is loaded.
NOTE: You need to enable `Common Language Runtime Exceptions` to see this error.
Please refer to the **Minimal repro** and **Screencast** sections that illustrate the problematic behavior.
---
**Actual behavior:**
```callstack
System.ComponentModel.Win32Exception: 'Invalid window handle.
WindowsBase.dll!MS.Win32.UnsafeNativeMethods.ClientToScreen(System.Runtime.InteropServices.HandleRef hWnd, ref MS.Win32.NativeMethods.POINT pt) Unknown
PresentationCore.dll!MS.Internal.PointUtil.ClientToScreen(System.Windows.Point pointClient, System.Windows.PresentationSource presentationSource) Unknown
PresentationCore.dll!System.Windows.Input.MouseDevice.GetScreenPositionFromSystem() Unknown
PresentationCore.dll!System.Windows.Input.MouseDevice.GetScreenPosition() Unknown
PresentationCore.dll!System.Windows.Input.MouseDevice.GetPosition(System.Windows.IInputElement relativeTo) Unknown
PresentationCore.dll!System.Windows.Input.Mouse.GetPosition(System.Windows.IInputElement relativeTo) Unknown
> WpfCore.dll!WpfCore.MainWindow.Border_Loaded(object sender, System.Windows.RoutedEventArgs e) Line 50 C#
PresentationCore.dll!System.Windows.RoutedEventHandlerInfo.InvokeHandler(object target, System.Windows.RoutedEventArgs routedEventArgs) Unknown
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender, System.Windows.RoutedEventArgs args) Unknown
PresentationCore.dll!System.Windows.UIElement.RaiseEvent(System.Windows.RoutedEventArgs e) Unknown
PresentationFramework.dll!System.Windows.BroadcastEventHelper.BroadcastEvent(System.Windows.DependencyObject root, System.Windows.RoutedEvent routedEvent) Unknown
PresentationFramework.dll!System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(object root) Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.FireLoadedPendingCallbacks() Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget) Unknown
PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object resizedCompositionTarget) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object state) Unknown
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(object obj) Unknown
System.Private.CoreLib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
System.Private.CoreLib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) Unknown
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object source, System.Delegate callback, object args, int numArgs, System.Delegate catchHandler) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) Unknown
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) Unknown
[Native to Managed Transition]
[Managed to Native Transition]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame frame) Unknown
WindowsBase.dll!System.Windows.Threading.Dispatcher.Run() Unknown
PresentationFramework.dll!System.Windows.Application.RunDispatcher(object ignore) Unknown
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) Unknown
PresentationFramework.dll!System.Windows.Application.Run() Unknown
WpfCore.dll!WpfCore.App.Main() Unknown
```
---
**Expected behavior:**
The `Mouse.GetPosition` method should return a correct Point value.
---
**Minimal repro:** [dxSample.zip](https://github.com/dotnet/wpf/files/10286316/dxSample.zip)
**Screencast:** [screencast.mp4](https://user-images.githubusercontent.com/31204429/209119622-62c5a6d2-d52d-4206-b301-ef0c94c2d0f2.mp4)
---
**Steps to reproduce:**
1. Enable showing `Common Language Runtime Exceptions` in your Visual Studio;
2. Run any of projects in the attached project;
3. Click the `Test` button in the MainWindow and **do not move** the mouse pointer anywhere;
4. Click in the button from a child window;
At this moment, an error should occur in the Loaded event handler of the Border element. Please refer to the screencast to see the issue.
Contributor guide
Assessment
This issue has not been assessed yet.