diff --git a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs index 6eedadb4a7..00517fc261 100644 --- a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs +++ b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs @@ -2,6 +2,7 @@ // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using ColorPicker.Helpers; using System; using System.ComponentModel; using System.Diagnostics; @@ -71,7 +72,7 @@ namespace ColorPicker.Mouse if (!result) { int errorCode = Marshal.GetLastWin32Error(); - throw new Win32Exception(errorCode); + Logger.LogError("Failed to unsubscribe mouse hook with the error code" + errorCode); } } } @@ -90,7 +91,7 @@ namespace ColorPicker.Mouse if (_mouseHookHandle == IntPtr.Zero) { int errorCode = Marshal.GetLastWin32Error(); - throw new Win32Exception(errorCode); + Logger.LogError("Failed to subscribe mouse hook with the error code" + errorCode); } } } diff --git a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs index a2ba4952ef..27168b6120 100644 --- a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs +++ b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs @@ -94,15 +94,7 @@ namespace ColorPicker.Mouse private void AppStateMonitor_AppClosed(object sender, EventArgs e) { - _timer.Stop(); - _previousMousePosition = new System.Windows.Point(-1, 1); - _mouseHook.OnMouseDown -= MouseHook_OnMouseDown; - _mouseHook.OnMouseWheel -= MouseHook_OnMouseWheel; - - if (_userSettings.ChangeCursor.Value) - { - CursorManager.RestoreOriginalCursors(); - } + DisposeHook(); } private void AppStateMonitor_AppShown(object sender, EventArgs e) @@ -135,7 +127,25 @@ namespace ColorPicker.Mouse private void MouseHook_OnMouseDown(object sender, Point p) { + DisposeHook(); OnMouseDown?.Invoke(this, p); } + + private void DisposeHook() + { + if (_timer.IsEnabled) + { + _timer.Stop(); + } + + _previousMousePosition = new System.Windows.Point(-1, 1); + _mouseHook.OnMouseDown -= MouseHook_OnMouseDown; + _mouseHook.OnMouseWheel -= MouseHook_OnMouseWheel; + + if (_userSettings.ChangeCursor.Value) + { + CursorManager.RestoreOriginalCursors(); + } + } } }