2
2
using System . ComponentModel ;
3
3
using System . Linq ;
4
4
using System . Media ;
5
+ using System . Runtime . InteropServices ;
5
6
using System . Threading . Tasks ;
6
7
using System . Windows ;
7
8
using System . Windows . Controls ;
20
21
using Flow . Launcher . Infrastructure . UserSettings ;
21
22
using Flow . Launcher . Plugin . SharedCommands ;
22
23
using Flow . Launcher . ViewModel ;
24
+ using Microsoft . Win32 ;
23
25
using ModernWpf . Controls ;
24
26
using MouseButtons = System . Windows . Forms . MouseButtons ;
25
27
using NotifyIcon = System . Windows . Forms . NotifyIcon ;
@@ -31,6 +33,11 @@ public partial class MainWindow
31
33
{
32
34
#region Private Fields
33
35
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
+
34
41
// Dependency Injection
35
42
private readonly Settings _settings ;
36
43
private readonly Theme _theme ;
@@ -74,8 +81,18 @@ public MainWindow()
74
81
75
82
InitSoundEffects ( ) ;
76
83
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 ( ) ;
77
95
}
78
-
79
96
#endregion
80
97
81
98
#region Window Event
@@ -89,8 +106,10 @@ private void OnSourceInitialized(object sender, EventArgs e)
89
106
win . AddHook ( WndProc ) ;
90
107
Win32Helper . HideFromAltTab ( this ) ;
91
108
Win32Helper . DisableControlBox ( this ) ;
109
+ // 세션 변경 알림 등록 (Windows 잠금 감지)
110
+ WTSRegisterSessionNotification ( handle , 0 ) ;
92
111
}
93
-
112
+
94
113
private async void OnLoaded ( object sender , RoutedEventArgs _ )
95
114
{
96
115
// Check first launch
@@ -234,13 +253,28 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
234
253
235
254
private async void OnClosing ( object sender , CancelEventArgs e )
236
255
{
256
+ // 세션 변경 알림 등록 해제
257
+ var handle = Win32Helper . GetWindowHandle ( this , false ) ;
258
+ WTSUnRegisterSessionNotification ( handle ) ;
259
+
260
+ // 기존 이벤트 구독 해제
261
+ SystemEvents . PowerModeChanged -= SystemEvents_PowerModeChanged ;
262
+ SystemEvents . SessionEnding -= SystemEvents_SessionEnding ;
263
+
237
264
_notifyIcon . Visible = false ;
238
265
App . API . SaveAppAllSettings ( ) ;
239
266
e . Cancel = true ;
240
267
await PluginManager . DisposePluginsAsync ( ) ;
241
268
Notification . Uninstall ( ) ;
242
269
Environment . Exit ( 0 ) ;
243
270
}
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 ) ;
244
278
245
279
private void OnLocationChanged ( object sender , EventArgs e )
246
280
{
@@ -435,6 +469,31 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
435
469
436
470
handled = true ;
437
471
}
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
+ }
438
497
439
498
return IntPtr . Zero ;
440
499
}
0 commit comments