更喜欢使用 WPF 的 C#、.Net 3.5 中的答案(Windows 窗体也可以)
我有一个本质上是工具栏窗口或托盘图标的应用程序。它需要检测用户是否锁定了他/她的工作站并走开了,以便在集中式系统中更新此人的状态。
使用 SystemEvents,我可以很容易地检测到 session 切换或注销,但我终究无法弄清楚如何检测或接收 Lock 事件。
感谢您的帮助。
最佳答案
当你处理 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;
// ...
}
}
关于c# - 如何检测来自 WPF 应用程序的“锁定此计算机”命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/652550/