@@ -33,10 +33,17 @@ public partial class MainWindow
3333 {
3434 #region Private Fields
3535
36- // Win32 상수 및 구조체 정의
36+ //For restore window Freeze
3737 private const int WM_WTSSESSION_CHANGE = 0x02B1 ;
38- private const int WTS_SESSION_LOCK = 0x7 ;
3938 private const int WTS_SESSION_UNLOCK = 0x8 ;
39+ private const int NOTIFY_FOR_ALL_SESSIONS = 1 ;
40+ private const int NOTIFY_FOR_THIS_SESSION = 0 ;
41+
42+ [ DllImport ( "wtsapi32.dll" ) ]
43+ private static extern bool WTSRegisterSessionNotification ( IntPtr hWnd , int dwFlags ) ;
44+
45+ [ DllImport ( "wtsapi32.dll" ) ]
46+ private static extern bool WTSUnRegisterSessionNotification ( IntPtr hWnd ) ;
4047
4148 // Dependency Injection
4249 private readonly Settings _settings ;
@@ -82,16 +89,22 @@ public MainWindow()
8289 InitSoundEffects ( ) ;
8390 DataObject . AddPastingHandler ( QueryTextBox , QueryTextBox_OnPaste ) ;
8491
85- SystemEvents . PowerModeChanged += SystemEvents_PowerModeChanged ;
86- SystemEvents . SessionEnding += SystemEvents_SessionEnding ;
92+
8793 }
8894 private void SystemEvents_SessionEnding ( object sender , SessionEndingEventArgs e )
8995 {
90- _viewModel . Show ( ) ;
96+ _viewModel . SystemWakeUpShow ( ) ;
9197 }
9298 private void SystemEvents_PowerModeChanged ( object sender , PowerModeChangedEventArgs e )
9399 {
94- _viewModel . Show ( ) ;
100+ _viewModel . SystemWakeUpShow ( ) ;
101+ }
102+ private void SystemEvents_SessionSwitch ( object sender , Microsoft . Win32 . SessionSwitchEventArgs e )
103+ {
104+ if ( e . Reason == Microsoft . Win32 . SessionSwitchReason . SessionUnlock )
105+ {
106+ _viewModel . SystemWakeUpShow ( ) ;
107+ }
95108 }
96109 #endregion
97110
@@ -101,13 +114,22 @@ private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventA
101114
102115 private void OnSourceInitialized ( object sender , EventArgs e )
103116 {
104- var handle = Win32Helper . GetWindowHandle ( this , true ) ;
105- var win = HwndSource . FromHwnd ( handle ) ;
106- win . AddHook ( WndProc ) ;
107- Win32Helper . HideFromAltTab ( this ) ;
108- Win32Helper . DisableControlBox ( this ) ;
109- // 세션 변경 알림 등록 (Windows 잠금 감지)
110- WTSRegisterSessionNotification ( handle , 0 ) ;
117+ IntPtr handle = new WindowInteropHelper ( this ) . Handle ;
118+ var result = WTSRegisterSessionNotification ( handle , NOTIFY_FOR_THIS_SESSION ) ;
119+
120+ if ( ! result )
121+ {
122+ //Log.Error($"|MainWindow.OnSourceInitialized|WTSRegisterSessionNotification Failed: {Marshal.GetLastWin32Error()}");
123+ //Debug.WriteLine("Failed");
124+ }
125+ else
126+ {
127+ //Log.Info("|MainWindow.OnSourceInitialized|WTSRegisterSessionNotification Sucesss");
128+ //Debug.WriteLine("Sucesss");
129+ }
130+
131+ HwndSource source = PresentationSource . FromVisual ( this ) as HwndSource ;
132+ source ? . AddHook ( WndProc ) ;
111133 }
112134
113135 private async void OnLoaded ( object sender , RoutedEventArgs _ )
@@ -248,13 +270,12 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
248270
249271 private async void OnClosing ( object sender , CancelEventArgs e )
250272 {
251- // 세션 변경 알림 등록 해제
273+ // Unregister session notification
252274 var handle = Win32Helper . GetWindowHandle ( this , false ) ;
253275 WTSUnRegisterSessionNotification ( handle ) ;
254-
255- // 기존 이벤트 구독 해제
256276 SystemEvents . PowerModeChanged -= SystemEvents_PowerModeChanged ;
257277 SystemEvents . SessionEnding -= SystemEvents_SessionEnding ;
278+ SystemEvents . SessionSwitch -= SystemEvents_SessionSwitch ;
258279
259280 _notifyIcon . Visible = false ;
260281 App . API . SaveAppAllSettings ( ) ;
@@ -263,13 +284,13 @@ private async void OnClosing(object sender, CancelEventArgs e)
263284 Notification . Uninstall ( ) ;
264285 Environment . Exit ( 0 ) ;
265286 }
266-
267- [ DllImport ( "wtsapi32.dll" , SetLastError = true ) ]
268- private static extern bool WTSRegisterSessionNotification ( IntPtr hWnd , int dwFlags ) ;
269-
270- [ DllImport ( "wtsapi32.dll" , SetLastError = true ) ]
271- private static extern bool WTSUnRegisterSessionNotification ( IntPtr hWnd ) ;
272-
287+ protected override void OnClosed ( EventArgs e )
288+ {
289+ IntPtr handle = new WindowInteropHelper ( this ) . Handle ;
290+ WTSUnRegisterSessionNotification ( handle ) ;
291+
292+ base . OnClosed ( e ) ;
293+ }
273294 private void OnLocationChanged ( object sender , EventArgs e )
274295 {
275296 if ( _animating )
@@ -429,64 +450,18 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
429450 }
430451 else if ( msg == Win32Helper . WM_EXITSIZEMOVE )
431452 {
432- if ( _initialHeight != ( int ) Height )
433- {
434- var shadowMargin = 0 ;
435- var ( _, useDropShadowEffect ) = _theme . GetActualValue ( ) ;
436- if ( useDropShadowEffect )
437- {
438- shadowMargin = 32 ;
439- }
440-
441- if ( ! _settings . KeepMaxResults )
442- {
443- var itemCount = ( Height - ( _settings . WindowHeightSize + 14 ) - shadowMargin ) / _settings . ItemHeightSize ;
444-
445- if ( itemCount < 2 )
446- {
447- _settings . MaxResultsToShow = 2 ;
448- }
449- else
450- {
451- _settings . MaxResultsToShow = Convert . ToInt32 ( Math . Truncate ( itemCount ) ) ;
452- }
453- }
454-
455- SizeToContent = SizeToContent . Height ;
456- _viewModel . MainWindowWidth = Width ;
457- }
458-
459- if ( _initialWidth != ( int ) Width )
460- {
461- SizeToContent = SizeToContent . Height ;
462- }
463-
453+ // 기존 코드
464454 handled = true ;
465455 }
456+
466457 // Windows (Win+L) Event
467- else if ( msg == WM_WTSSESSION_CHANGE )
458+ if ( msg == WM_WTSSESSION_CHANGE && wParam . ToInt32 ( ) == WTS_SESSION_UNLOCK )
468459 {
469- int reason = wParam . ToInt32 ( ) ;
470- if ( reason == WTS_SESSION_LOCK )
471- {
472- Application . Current . Dispatcher . Invoke ( ( ) =>
473- {
474- _viewModel . SystemWakeUpShow ( ) ;
475- } ) ;
476-
477- handled = true ;
478- }
479- else if ( reason == WTS_SESSION_UNLOCK )
480- {
481- Application . Current . Dispatcher . Invoke ( ( ) =>
482- {
483- _viewModel . SystemWakeUpShow ( ) ;
484- } ) ;
485-
486- handled = true ;
487- }
460+ // 기존 코드
461+ handled = true ;
488462 }
489-
463+
464+ // 여기에 반환문 추가
490465 return IntPtr . Zero ;
491466 }
492467
0 commit comments