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

Gra*_*ner 19 c# wpf desktop locking

更喜欢使用WPF的C#,.Net 3.5中的答案(Windows Forms也没关系)

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

我可以使用SystemEvents轻松地检测会话切换或注销,但我不能在我的生活中弄清楚如何在Lock上检测或接收事件.

谢谢你的帮助.

Dan*_*ant 44

当您处理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;
         // ...
     }
 }
Run Code Online (Sandbox Code Playgroud)