如何从WPF应用程序中检测“锁定计算机”命令?

21

希望以 C#、.Net 3.5 使用 WPF(Windows Forms 也可以)的形式给出答案。

我有一个基本上是工具栏窗口或托盘图标的应用程序。它需要检测用户是否锁定了他/她的工作站并离开,以便在集中系统中更新此人的状态。

我可以使用 SystemEvents 轻松检测会话切换或注销,但是我无论如何都无法找到如何检测或接收锁定事件的方法。

感谢任何帮助。


请参考以下链接:https://dev59.com/g3VD5IYBdhLWcg3wO5AD。 - Abhijit
3个回答

46

当你处理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;
         // ...
     }
 }

2

2
有没有理由这样做,而不是处理SystemEvents.SessionSwitch? - Daniel LeCheminant
4
只有你不知道SessionSwitch,我以前也不知道。 - BC.

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接