希望以 C#、.Net 3.5 使用 WPF(Windows Forms 也可以)的形式给出答案。
我有一个基本上是工具栏窗口或托盘图标的应用程序。它需要检测用户是否锁定了他/她的工作站并离开,以便在集中系统中更新此人的状态。
我可以使用 SystemEvents 轻松检测会话切换或注销,但是我无论如何都无法找到如何检测或接收锁定事件的方法。
感谢任何帮助。
当你处理Microsoft.Win32.SystemEvents.SessionSwitch
事件时(看起来你已经这样做以检测注销),请检查Reason
是否为SessionSwitchReason
.SessionLock
:
using Microsoft.Win32;
// ...
// Somewhere in your startup, add your event handler:
SystemEvents.SessionSwitch +=
new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
// ...
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
switch(e.Reason)
{
// ...
case SessionSwitchReason.SessionLock:
// Do whatever you need to do for a lock
// ...
break;
case SessionSwitchReason.SessionUnlock:
// Do whatever you need to do for an unlock
// ...
break;
// ...
}
}
WTSRegisterSessionNotification
。