From 518d102c31a7e11cda5d7c3b49ea79d8b3311070 Mon Sep 17 00:00:00 2001 From: ema Date: Thu, 23 Apr 2026 00:34:37 +0800 Subject: [PATCH] Add EnsureHandleSafe extension and use it --- .../WindowInteropHelperExtension.cs | 40 +++++++++++++++++++ .../Helpers/DisplayDeviceHelper.cs | 5 ++- QuickLook.Common/Helpers/WindowHelper.cs | 13 +++--- 3 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 QuickLook.Common/ExtensionMethods/WindowInteropHelperExtension.cs diff --git a/QuickLook.Common/ExtensionMethods/WindowInteropHelperExtension.cs b/QuickLook.Common/ExtensionMethods/WindowInteropHelperExtension.cs new file mode 100644 index 0000000..ad42745 --- /dev/null +++ b/QuickLook.Common/ExtensionMethods/WindowInteropHelperExtension.cs @@ -0,0 +1,40 @@ +// Copyright © 2017-2026 QL-Win Contributors +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +using System.Windows.Interop; + +namespace QuickLook.Common.ExtensionMethods; + +public static class WindowInteropHelperExtension +{ + extension(WindowInteropHelper windowInteropHelper) + { + public nint EnsureHandleSafe() + { + try + { + return windowInteropHelper?.EnsureHandle() ?? IntPtr.Zero; + } + catch + { + // Returning 0 is fine, since this error usually only occurs when the window is already closed or being disposed. + return IntPtr.Zero; + } + } + } +} diff --git a/QuickLook.Common/Helpers/DisplayDeviceHelper.cs b/QuickLook.Common/Helpers/DisplayDeviceHelper.cs index 08b61b3..ede4181 100644 --- a/QuickLook.Common/Helpers/DisplayDeviceHelper.cs +++ b/QuickLook.Common/Helpers/DisplayDeviceHelper.cs @@ -15,6 +15,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +using QuickLook.Common.ExtensionMethods; using System; using System.Drawing; using System.Runtime.InteropServices; @@ -32,7 +33,7 @@ public static class DisplayDeviceHelper public static ScaleFactor GetScaleFactorFromWindow(Window window) { - return GetScaleFactorFromWindow(new WindowInteropHelper(window).EnsureHandle()); + return GetScaleFactorFromWindow(new WindowInteropHelper(window).EnsureHandleSafe()); } public static ScaleFactor GetCurrentScaleFactor() @@ -79,7 +80,7 @@ public static class DisplayDeviceHelper { try { - var hMonitor = MonitorFromWindow(new WindowInteropHelper(window).EnsureHandle(), MonitorDefaults.TONEAREST); + var hMonitor = MonitorFromWindow(new WindowInteropHelper(window).EnsureHandleSafe(), MonitorDefaults.TONEAREST); return GetMonitorColorProfile(hMonitor); } catch (COMException ex) when (ex.HResult == unchecked((int)0x80263001)) diff --git a/QuickLook.Common/Helpers/WindowHelper.cs b/QuickLook.Common/Helpers/WindowHelper.cs index c428170..cc01168 100644 --- a/QuickLook.Common/Helpers/WindowHelper.cs +++ b/QuickLook.Common/Helpers/WindowHelper.cs @@ -15,6 +15,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +using QuickLook.Common.ExtensionMethods; using QuickLook.Common.NativeMethods; using System; using System.Diagnostics; @@ -80,7 +81,7 @@ public static class WindowHelper double width, double height) { - var handle = new WindowInteropHelper(window).EnsureHandle(); + var handle = new WindowInteropHelper(window).EnsureHandleSafe(); // scale the size to the primary display TransformToPixels(window, width, height, @@ -92,7 +93,7 @@ public static class WindowHelper public static Rect GetWindowRectInPixel(this Window window) { - var handle = new WindowInteropHelper(window).EnsureHandle(); + var handle = new WindowInteropHelper(window).EnsureHandleSafe(); User32.GetWindowRect(handle, out User32.RECT nRect); @@ -172,7 +173,7 @@ public static class WindowHelper { window.Background = Brushes.Transparent; - var hwnd = new WindowInteropHelper(window).EnsureHandle(); + var hwnd = new WindowInteropHelper(window).EnsureHandleSafe(); if (!window.AllowsTransparency && HwndSource.FromHwnd(hwnd) is HwndSource hwndSource) { @@ -226,7 +227,7 @@ public static class WindowHelper Data = accentPtr }; - var hwnd = new WindowInteropHelper(window).EnsureHandle(); + var hwnd = new WindowInteropHelper(window).EnsureHandleSafe(); User32.SetWindowCompositionAttribute(hwnd, ref data); Marshal.FreeHGlobal(accentPtr); @@ -280,7 +281,7 @@ public static class WindowHelper // Mica will handle the color window.Background = Brushes.Transparent; - var hwnd = new WindowInteropHelper(window).EnsureHandle(); + var hwnd = new WindowInteropHelper(window).EnsureHandleSafe(); if (!window.AllowsTransparency && HwndSource.FromHwnd(hwnd) is HwndSource hwndSource) { @@ -357,4 +358,4 @@ public static class WindowHelper public uint GradientColor; public readonly int AnimationId; } -} \ No newline at end of file +}