22using System . ComponentModel ;
33using System . Linq ;
44using System . Media ;
5+ using System . Runtime . InteropServices ;
56using System . Threading . Tasks ;
67using System . Windows ;
78using System . Windows . Controls ;
2021using Flow . Launcher . Infrastructure . UserSettings ;
2122using Flow . Launcher . Plugin . SharedCommands ;
2223using Flow . Launcher . ViewModel ;
24+ using Microsoft . Win32 ;
2325using ModernWpf . Controls ;
2426using MouseButtons = System . Windows . Forms . MouseButtons ;
2527using NotifyIcon = System . Windows . Forms . NotifyIcon ;
@@ -31,6 +33,11 @@ public partial class MainWindow
3133 {
3234 #region Private Fields
3335
36+ // Win32 상수 및 구조체 정의
37+ private const int WM_WTSSESSION_CHANGE = 0x02B1 ;
38+ private const int WTS_SESSION_LOCK = 0x7 ;
39+ private const int WTS_SESSION_UNLOCK = 0x8 ;
40+
3441 // Dependency Injection
3542 private readonly Settings _settings ;
3643 private readonly Theme _theme ;
@@ -74,8 +81,18 @@ public MainWindow()
7481
7582 InitSoundEffects ( ) ;
7683 DataObject . AddPastingHandler ( QueryTextBox , QueryTextBox_OnPaste ) ;
84+
85+ SystemEvents . PowerModeChanged += SystemEvents_PowerModeChanged ;
86+ SystemEvents . SessionEnding += SystemEvents_SessionEnding ;
87+ }
88+ private void SystemEvents_SessionEnding ( object sender , SessionEndingEventArgs e )
89+ {
90+ _viewModel . Show ( ) ;
91+ }
92+ private void SystemEvents_PowerModeChanged ( object sender , PowerModeChangedEventArgs e )
93+ {
94+ _viewModel . Show ( ) ;
7795 }
78-
7996 #endregion
8097
8198 #region Window Event
@@ -89,8 +106,10 @@ private void OnSourceInitialized(object sender, EventArgs e)
89106 win . AddHook ( WndProc ) ;
90107 Win32Helper . HideFromAltTab ( this ) ;
91108 Win32Helper . DisableControlBox ( this ) ;
109+ // 세션 변경 알림 등록 (Windows 잠금 감지)
110+ WTSRegisterSessionNotification ( handle , 0 ) ;
92111 }
93-
112+
94113 private async void OnLoaded ( object sender , RoutedEventArgs _ )
95114 {
96115 // Check first launch
@@ -234,13 +253,28 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
234253
235254 private async void OnClosing ( object sender , CancelEventArgs e )
236255 {
256+ // 세션 변경 알림 등록 해제
257+ var handle = Win32Helper . GetWindowHandle ( this , false ) ;
258+ WTSUnRegisterSessionNotification ( handle ) ;
259+
260+ // 기존 이벤트 구독 해제
261+ SystemEvents . PowerModeChanged -= SystemEvents_PowerModeChanged ;
262+ SystemEvents . SessionEnding -= SystemEvents_SessionEnding ;
263+
237264 _notifyIcon . Visible = false ;
238265 App . API . SaveAppAllSettings ( ) ;
239266 e . Cancel = true ;
240267 await PluginManager . DisposePluginsAsync ( ) ;
241268 Notification . Uninstall ( ) ;
242269 Environment . Exit ( 0 ) ;
243270 }
271+
272+ // Win32 API 함수 정의
273+ [ DllImport ( "wtsapi32.dll" , SetLastError = true ) ]
274+ private static extern bool WTSRegisterSessionNotification ( IntPtr hWnd , int dwFlags ) ;
275+
276+ [ DllImport ( "wtsapi32.dll" , SetLastError = true ) ]
277+ private static extern bool WTSUnRegisterSessionNotification ( IntPtr hWnd ) ;
244278
245279 private void OnLocationChanged ( object sender , EventArgs e )
246280 {
@@ -435,6 +469,31 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
435469
436470 handled = true ;
437471 }
472+ // Windows 잠금(Win+L) 이벤트 처리
473+ else if ( msg == WM_WTSSESSION_CHANGE )
474+ {
475+ int reason = wParam . ToInt32 ( ) ;
476+ if ( reason == WTS_SESSION_LOCK )
477+ {
478+ // Windows 잠금 발생 시 메시지 박스 표시
479+ Application . Current . Dispatcher . Invoke ( ( ) =>
480+ {
481+ _viewModel . Show ( ) ;
482+ } ) ;
483+
484+ handled = true ;
485+ }
486+ else if ( reason == WTS_SESSION_UNLOCK )
487+ {
488+ // Windows 잠금 해제 시 메시지 박스 표시 (선택적)
489+ Application . Current . Dispatcher . Invoke ( ( ) =>
490+ {
491+ _viewModel . Show ( ) ;
492+ } ) ;
493+
494+ handled = true ;
495+ }
496+ }
438497
439498 return IntPtr . Zero ;
440499 }
0 commit comments