Add EnsureHandleSafe extension and use it

This commit is contained in:
ema
2026-04-23 00:34:37 +08:00
parent 6b0c3b8678
commit 518d102c31
3 changed files with 50 additions and 8 deletions
@@ -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 <http://www.gnu.org/licenses/>.
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;
}
}
}
}
@@ -15,6 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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))
+7 -6
View File
@@ -15,6 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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;
}
}
}