New*_*bie 4 visual-studio winforms visual-studio-2012
我正在开发一个应用程序,我试图检测工作站何时被锁定,例如用户按下 Windows + L 键。
我知道锁定事件具有价值
WTS_SESSION_LOCK 0x7
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用它。我在网上搜索过,但一无所获。
您应该使用命名空间中的SystemEvents类Microsoft.Win32,尤其是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
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在 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
}
}
Run Code Online (Sandbox Code Playgroud)
终于在 VB 上成功了:D
首先,您需要导入库:
Imports System
Imports Microsoft.Win32
Imports System.Windows.Forms
Run Code Online (Sandbox Code Playgroud)
然后添加处理程序:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler SystemEvents.SessionSwitch, AddressOf SessionSwitch_Event
End Sub
Run Code Online (Sandbox Code Playgroud)
最后创建捕获它的子程序:
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
Run Code Online (Sandbox Code Playgroud)
最后删除处理程序:
Private Sub closing_event(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
RemoveHandler SystemEvents.SessionSwitch, AddressOf SessionSwitch_Event
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2816 次 |
| 最近记录: |