7

重复: 如何以编程方式确定我的工作站是否被锁定?

当 Windows 用户锁定屏幕 (Windows+L) 并再次解锁时,如何检测(在运行时)。我知道我可以全局跟踪键盘输入,但是可以用环境变量检查这样的事情吗?

4

4 回答 4

16

SessionSwitch事件可能是您最好的选择检查通过SessionSwitchEventArgs传递的SessionSwitchReason以找出它是什么类型的开关并做出适当的反应。

于 2009-03-02T18:55:12.117 回答
3

您可以通过 WM_WTSSESSION_CHANGE 消息获得此通知。您必须通知 Windows 您希望通过 WTSRegisterSessionNotification 接收这些消息并取消注册 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

于 2009-03-02T18:58:14.807 回答
2

您可以使用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);
        }
    }
}
于 2013-12-19T22:21:26.080 回答
-3

您绝对不需要 WM_WTSSESSION_CHANGE 只需使用内部 WTTS api。

于 2009-03-02T19:54:48.900 回答