diff --git a/src/common/ManagedCommon/NativeMethods.cs b/src/common/ManagedCommon/NativeMethods.cs index a8d05b7a47..834a391b5f 100644 --- a/src/common/ManagedCommon/NativeMethods.cs +++ b/src/common/ManagedCommon/NativeMethods.cs @@ -112,5 +112,31 @@ namespace ManagedCommon public int cyTopHeight; public int cyBottomHeight; } + + /// + /// Safely calls DwmExtendFrameIntoClientArea to handle potential COMExceptions + /// that can occur after resuming from sleep state. + /// + /// Window handle + /// Margins + /// IntPtr.Zero on success, error code otherwise + internal static IntPtr SafeDwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset) + { + try + { + return DwmExtendFrameIntoClientArea(hWnd, ref pMarInset); + } + catch (COMException ex) when (ex.HResult == unchecked((int)0xD0000701)) + { + // Return a default value when this specific DWM error occurs (typically after sleep/resume) + // This is a non-critical styling error and can be safely ignored + return new IntPtr(-1); // Indicator for this specific error + } + catch (Exception) + { + // For other exceptions, rethrow to be handled by caller + throw; + } + } } } diff --git a/src/common/ManagedCommon/WindowHelpers.cs b/src/common/ManagedCommon/WindowHelpers.cs index c5ee13f69c..9e839ea996 100644 --- a/src/common/ManagedCommon/WindowHelpers.cs +++ b/src/common/ManagedCommon/WindowHelpers.cs @@ -47,7 +47,9 @@ namespace ManagedCommon if (OSVersionHelper.IsWindows10()) { var margins = new NativeMethods.MARGINS { cxLeftWidth = 0, cxRightWidth = 0, cyBottomHeight = 0, cyTopHeight = 2 }; - NativeMethods.DwmExtendFrameIntoClientArea(handle, ref margins); + + // Use the safe version of DwmExtendFrameIntoClientArea that handles resume-from-sleep issues + NativeMethods.SafeDwmExtendFrameIntoClientArea(handle, ref margins); } } } diff --git a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs index eaade8972f..9a24a690c1 100644 --- a/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs +++ b/src/modules/launcher/PowerLauncher/MainWindow.xaml.cs @@ -195,17 +195,37 @@ namespace PowerLauncher _viewModel.RegisterHotkey(_hwndSource.Handle); if (OSVersionHelper.IsGreaterThanWindows11_21H2()) { - // ResizeMode="NoResize" removes rounded corners. So force them to rounded. - IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle(); - DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE; - DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND; - DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint)); + try + { + // ResizeMode="NoResize" removes rounded corners. So force them to rounded. + IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle(); + DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE; + DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND; + DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint)); + } + catch (COMException ex) when (ex.HResult == unchecked((int)0xD0000701)) + { + // Ignore this specific DWM error which can occur after resuming from sleep + // This error is related to window styling and doesn't affect functionality + Log.Exception("DWM styling error occurred (typically after sleep resume). This is non-critical.", ex, GetType()); + + // Fallback to non-rounded corners + MainBorder.BorderThickness = new Thickness(0.5); + } + catch (Exception ex) + { + // Log other errors but continue execution + Log.Exception("Error setting DWM window attributes", ex, GetType()); + + // Fallback to non-rounded corners + MainBorder.BorderThickness = new Thickness(0.5); + } } else { // On Windows10 ResizeMode="NoResize" removes the border so we add a new one. // Also on 22000 it crashes due to DWMWA_WINDOW_CORNER_PREFERENCE https://github.com/microsoft/PowerToys/issues/36558 - MainBorder.BorderThickness = new System.Windows.Thickness(0.5); + MainBorder.BorderThickness = new Thickness(0.5); } }