■ WTSRegisterSessionNotification/WTSRegisterSessionNotification API 함수를 사용해 잠금 화면 전환시 처리하는 방법을 보여준다.
▶ SessionNotification.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
namespace TestProject { /// <summary> /// 세션 통지 /// </summary> public enum SessionNotification { /// <summary> /// 잠금 /// </summary> Lock = 0, /// <summary> /// 잠금 취소 /// </summary> Unlock } } |
▶ SessionNotificationEventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
using System; namespace TestProject { /// <summary> /// 세션 통지 이벤트 인자 /// </summary> public class SessionNotificationEventArgs : EventArgs { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 세션 통지 /// </summary> private SessionNotification notification; /// <summary> /// 타임 스탬프 /// </summary> private DateTime timeStamp; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 세션 통지 - Notification /// <summary> /// 세션 통지 /// </summary> public SessionNotification Notification { get { return this.notification; } } #endregion #region 타임 스탬프 - TimeStamp /// <summary> /// 타임 스탬프 /// </summary> public DateTime TimeStamp { get { return this.timeStamp; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SessionNotificationEventArgs(notification) /// <summary> /// 생성자 /// </summary> /// <param name="notification">세션 통지</param> public SessionNotificationEventArgs(SessionNotification notification) { this.notification = notification; this.timeStamp = DateTime.Now; } #endregion } } |
▶ SessionNotificationHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
using System; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; namespace TestProject { /// <summary> /// 세션 통지 헬퍼 /// </summary> public class SessionNotificationHelper : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region WTS 세션 통지 등록하기 - WTSRegisterSessionNotification(windowHandle, flag) /// <summary> /// WTS 세션 통지 등록하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("wtsapi32")] private static extern bool WTSRegisterSessionNotification(IntPtr windowHandle, int flag); #endregion #region WTS 세션 통지 등록 취소하기 - WTSUnRegisterSessionNotification(windowHandle) /// <summary> /// WTS 세션 통지 등록 취소하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <returns>처리 결과</returns> [DllImport("wtsapi32")] private static extern bool WTSUnRegisterSessionNotification(IntPtr windowHandle); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 세션 변경시 이벤트 - SessionChanged /// <summary> /// 세션 변경시 이벤트 /// </summary> public event EventHandler<SessionNotificationEventArgs> SessionChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// NOTIFY_FOR_THIS_SESSION /// </summary> private const int NOTIFY_FOR_THIS_SESSION = 0; /// <summary> /// SESSION_CHANGED /// </summary> private const int SESSION_CHANGED = 0x02B1; /// <summary> /// SESSION_LOCK /// </summary> private const int SESSION_LOCK = 0x7; /// <summary> /// SESSION_UNLOCK /// </summary> private const int SESSION_UNLOCK = 0x8; /// <summary> /// 등록 여부 /// </summary> private bool registered = false; /// <summary> /// 윈도우 인터롭 헬퍼 /// </summary> private WindowInteropHelper windowInteropHelper; /// <summary> /// 세션 잠금 마지막 시간 /// </summary> private DateTime lastTimeSessionLocked = DateTime.MinValue; /// <summary> /// 세션 잠금 취소 마지막 시간 /// </summary> private DateTime lastTimeSessionUnlocked = DateTime.MinValue; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 이벤트 발생 가능 여부 - EnableRaisingEvents /// <summary> /// 이벤트 발생 가능 여부 /// </summary> protected bool EnableRaisingEvents { get { return this.registered; } set { bool haveXp = Environment.OSVersion.Platform == PlatformID.Win32NT && (Environment.OSVersion.Version.Major > 5 || (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1)); if(!haveXp) { this.registered = false; return; } if(value == true && !registered) { WTSRegisterSessionNotification(this.windowInteropHelper.Handle, NOTIFY_FOR_THIS_SESSION); this.registered = true; } else if(value == false && registered) { WTSUnRegisterSessionNotification(this.windowInteropHelper.Handle); this.registered = false; } } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - SessionNotificationHelper(window) /// <summary> /// 생성자 /// </summary> /// <param name="window">윈도우</param> public SessionNotificationHelper(Window window) { this.windowInteropHelper = new WindowInteropHelper(window); window.Loaded += window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public //////////////////////////////////////////////////////////////////////////////// Function #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { if(this.registered) { EnableRaisingEvents = false; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void window_Loaded(object sender, RoutedEventArgs e) { HwndSource windowHandleSource = HwndSource.FromHwnd(this.windowInteropHelper.Handle); windowHandleSource.AddHook(new HwndSourceHook(WndProc)); EnableRaisingEvents = true; } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 세션 잠금 처리하기 - ProcessSessionLock() /// <summary> /// 세션 잠금 처리하기 /// </summary> private void ProcessSessionLock() { DateTime currentTime = DateTime.Now; if(this.lastTimeSessionLocked == DateTime.MinValue) { this.lastTimeSessionLocked = currentTime; } else { TimeSpan elapsed = currentTime - this.lastTimeSessionLocked; this.lastTimeSessionLocked = currentTime; if(elapsed.TotalMilliseconds < 100) { return; } } if(SessionChanged != null) { SessionChanged(this, new SessionNotificationEventArgs(SessionNotification.Lock)); } } #endregion #region 세션 잠금 취소 처리하기 - ProcessSessionUnLock() /// <summary> /// 세션 잠금 취소 처리하기 /// </summary> private void ProcessSessionUnLock() { DateTime currentTime = DateTime.Now; if(this.lastTimeSessionUnlocked == DateTime.MinValue) { this.lastTimeSessionUnlocked = currentTime; } else { TimeSpan elapsed = currentTime - this.lastTimeSessionUnlocked; this.lastTimeSessionUnlocked = currentTime; if(elapsed.TotalMilliseconds < 100) { return; } } if(SessionChanged != null) { SessionChanged(this, new SessionNotificationEventArgs(SessionNotification.Unlock)); } } #endregion #region 윈도우 프로시저 처리하기 - WndProc(windowHandle, message, wordParameter, longParameter, handled) /// <summary> /// 윈도우 프로시저 처리하기 /// </summary> /// <param name="windowHandle">윈도우 핸들</param> /// <param name="message">메시지</param> /// <param name="wordParameter">WORD 매개 변수</param> /// <param name="longParameter">LONG 매개 변수</param> /// <param name="handled">처리 여부</param> /// <returns>처리 결과</returns> private IntPtr WndProc(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter, ref bool handled) { if(message == SESSION_CHANGED) { if(wordParameter.ToInt32() == SESSION_LOCK) { ProcessSessionLock(); } else if(wordParameter.ToInt32() == SESSION_UNLOCK) { ProcessSessionUnLock(); } } return IntPtr.Zero; } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="WTSRegisterSessionNotification/WTSRegisterSessionNotification API 함수 : 잠금 화면 전환시 처리하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <TextBlock Name="textBlock" /> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); SessionNotificationHelper helper = new SessionNotificationHelper(this); helper.SessionChanged += helper_SessionChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 헬퍼 세션 변경시 처리하기 - helper_SessionChanged(sender, e) /// <summary> /// 헬퍼 세션 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void helper_SessionChanged(object sender, SessionNotificationEventArgs e) { this.textBlock.Text += string.Format("Recieved {0} notification at {1}\n", e.Notification, e.TimeStamp); } #endregion } } |