繁体   English   中英

捕获Windows 10(WPF)中的屏幕锁定事件

[英]To catch the event of screen lock in Windows 10 (WPF)

告诉我该怎么办,我不知道在哪里寻找答案。

我有一个WPF应用程序,可使用AForge与平板电脑的相机配合使用。 当用户通过键盘快捷键“ Win + L”锁定系统时-不再使用摄像头(指示灯已关闭),因为通过下面的事件我可以控制该过程。

 private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        switch (e.Reason)
        {

            case SessionSwitchReason.SessionLock: StopCam(); break;
            case SessionSwitchReason.SessionUnlock:
                if (this.Window.WindowState != WindowState.Minimized)
                {
                    StartCam();
                }
                break;
        }
    }

如果用户按屏幕锁定按钮(通常在平板电脑顶部),则我的相机不会关闭(指示灯点亮)。 如何跟踪此事件?

ps我的WPF应用程序将在Windows 10平板电脑上运行。

也许我有解决方案。 这段代码在PC上有效,但我没有在平板电脑上测试过(因为我没有一个)。 您需要使用System.Windows.Interop添加;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
        source.AddHook(WndProc);
    }

    private const int WM_SYSCOMMAND = 0x0112;
    private const int SC_MONITORPOWER = 0xf170;

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == WM_SYSCOMMAND)
        {
            if (wParam.ToInt32() == SC_MONITORPOWER)
            {
                switch (lParam.ToInt32())
                {
                    case -1:
                        this.listBox1.Items.Add("display is powering on");
                        break;

                    case 2:
                        this.listBox1.Items.Add("display is being shut off");
                        break;
                }
            }
        }

        return IntPtr.Zero;
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM