From 74306f0a7616d6807ee992f3b4e2ee78c617f84c Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 23 Jul 2025 14:15:20 +0800 Subject: [PATCH] Use PInvoke to register SystemEvents --- .../NativeMethods.txt | 5 +++- Flow.Launcher.Infrastructure/Win32Helper.cs | 3 +++ Flow.Launcher/MainWindow.xaml.cs | 23 ++++++++----------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Flow.Launcher.Infrastructure/NativeMethods.txt b/Flow.Launcher.Infrastructure/NativeMethods.txt index e87004251..eb844dd7c 100644 --- a/Flow.Launcher.Infrastructure/NativeMethods.txt +++ b/Flow.Launcher.Infrastructure/NativeMethods.txt @@ -83,4 +83,7 @@ WM_GETTEXT OpenProcess QueryFullProcessImageName EVENT_OBJECT_HIDE -EVENT_SYSTEM_DIALOGEND \ No newline at end of file +EVENT_SYSTEM_DIALOGEND + +WM_POWERBROADCAST +PBT_APMRESUMEAUTOMATIC \ No newline at end of file diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 68d382377..7cc644eaa 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -338,6 +338,9 @@ namespace Flow.Launcher.Infrastructure public const int SC_MAXIMIZE = (int)PInvoke.SC_MAXIMIZE; public const int SC_MINIMIZE = (int)PInvoke.SC_MINIMIZE; + public const int WM_POWERBROADCAST = (int)PInvoke.WM_POWERBROADCAST; + public const int PBT_APMRESUMEAUTOMATIC = (int)PInvoke.PBT_APMRESUMEAUTOMATIC; + #endregion #region Window Handle diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index e69de67cf..8eb41e032 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -25,7 +25,6 @@ using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Plugin.SharedModels; using Flow.Launcher.ViewModel; -using Microsoft.Win32; using ModernWpf.Controls; using DataObject = System.Windows.DataObject; using Key = System.Windows.Input.Key; @@ -96,7 +95,6 @@ namespace Flow.Launcher InitSoundEffects(); DataObject.AddPastingHandler(QueryTextBox, QueryTextBox_OnPaste); - SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged; _viewModel.ActualApplicationThemeChanged += ViewModel_ActualApplicationThemeChanged; } @@ -670,6 +668,16 @@ namespace Flow.Launcher handled = true; } break; + case Win32Helper.WM_POWERBROADCAST: // Handle power broadcast messages + // https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast + if (wParam.ToInt32() == Win32Helper.PBT_APMRESUMEAUTOMATIC) + { + // Fix for sound not playing after sleep / hibernate + // https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps + InitSoundEffects(); + } + handled = true; + break; } return IntPtr.Zero; @@ -679,16 +687,6 @@ namespace Flow.Launcher #region Window Sound Effects - private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e) - { - // Fix for sound not playing after sleep / hibernate - // https://stackoverflow.com/questions/64805186/mediaplayer-doesnt-play-after-computer-sleeps - if (e.Mode == PowerModes.Resume) - { - InitSoundEffects(); - } - } - private void InitSoundEffects() { if (_settings.WMPInstalled) @@ -1443,7 +1441,6 @@ namespace Flow.Launcher animationSoundWMP?.Close(); animationSoundWPF?.Dispose(); _viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged; - SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; } _disposed = true;