我正在开发一个应用程序,我正在尝试检测工作站何时被锁定,例如用户按下Windows + L键。
我知道锁定事件的值为
WTS_SESSION_LOCK 0x7
但我不知道如何使用它。我在网上搜索但一无所获。
答案 0 :(得分:3)
您应该使用Microsoft.Win32
命名空间中的SystemEvents
类,尤其是SystemEvents.SessionSwitch
事件。
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; // Subscribe to the SessionSwitch event
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
// Add your session lock "handling" code here
}
如果您需要在Winforms应用程序中从程序启动中激活此事件:
static class Program
{
[STAThread]
private static void Main()
{
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch; // Subscribe to the SessionSwitch event
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
// Add your session lock "handling" code here
}
}
答案 1 :(得分:0)
Finnallly设法在VB上做到了:D
首先你需要导入libs:
Imports System
Imports Microsoft.Win32
Imports System.Windows.Forms
然后添加处理程序:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler SystemEvents.SessionSwitch, AddressOf SessionSwitch_Event
End Sub
最后你创建了捕获它的子:
Private Sub SessionSwitch_Event(ByVal sender As Object, ByVal e As SessionSwitchEventArgs)
If e.Reason = SessionSwitchReason.SessionLock Then
MsgBox("Locked")
End If
If e.Reason = SessionSwitchReason.SessionUnlock Then
MsgBox("Unlocked")
End If
End Sub
最后删除处理程序:
Private Sub closing_event(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
RemoveHandler SystemEvents.SessionSwitch, AddressOf SessionSwitch_Event
End Sub