[英]Checking for workstation lock/unlock change with c#
DUPLICATE: 如何以編程方式確定我的工作站是否已鎖定?
如何檢測(在運行時)Windows用戶鎖定其屏幕(Windows + L)並再次解鎖時。 我知道我可以全局跟蹤鍵盤輸入,但是可以用環境變量來檢查這樣的事情嗎?
SessionSwitch事件可能是您最好的選擇。 檢查通過SessionSwitchEventArgs傳遞的SessionSwitchReason ,找出它是什么樣的開關並做出適當的反應。
您可以通過WM_WTSSESSION_CHANGE消息獲取此通知。 您必須通過WTSRegisterSessionNotification通知Windows您要接收這些消息,並使用WTSUnRegisterSessionNotification取消注冊。
這些帖子應該對C#實現有所幫助。
http://pinvoke.net/default.aspx/wtsapi32.WTSRegisterSessionNotification
http://blogs.msdn.com/shawnfa/archive/2005/05/17/418891.aspx
http://bytes.com/groups/net-c/276963-trapping-when-workstation-locked
您可以使用ComponentDispatcher
作為獲取這些事件的替代方法。
這是一個包裝它的示例類。
public class Win32Session
{
private const int NOTIFY_FOR_THIS_SESSION = 0;
private const int WM_WTSSESSION_CHANGE = 0x2b1;
private const int WTS_SESSION_LOCK = 0x7;
private const int WTS_SESSION_UNLOCK = 0x8;
public event EventHandler MachineLocked;
public event EventHandler MachineUnlocked;
public Win32Session()
{
ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;
}
void ComponentDispatcher_ThreadFilterMessage(ref MSG msg, ref bool handled)
{
if (msg.message == WM_WTSSESSION_CHANGE)
{
int value = msg.wParam.ToInt32();
if (value == WTS_SESSION_LOCK)
{
OnMachineLocked(EventArgs.Empty);
}
else if (value == WTS_SESSION_UNLOCK)
{
OnMachineUnlocked(EventArgs.Empty);
}
}
}
protected virtual void OnMachineLocked(EventArgs e)
{
EventHandler temp = MachineLocked;
if (temp != null)
{
temp(this, e);
}
}
protected virtual void OnMachineUnlocked(EventArgs e)
{
EventHandler temp = MachineUnlocked;
if (temp != null)
{
temp(this, e);
}
}
}
你絕對不需要WM_WTSSESSION_CHANGE只需使用內部WTTS apis。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.