【问题标题】:To catch the event of screen lock in Windows 10 (WPF)在 Windows 10 (WPF) 中捕获屏幕锁定事件
【发布时间】:2016-06-08 17:02:09
【问题描述】:

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

我有一个使用 AForge 与平板电脑相机配合使用的 WPF 应用程序。当用户通过键盘快捷键“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;
        }
    }

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

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

【问题讨论】:

  • 您是否尝试在调试窗口中打印 e.Reason 以查看是否有其他值用于锁定屏幕。 Debug.WriteLine(e.Reason)

标签: c# wpf winapi


【解决方案1】:

也许我有一个解决方案。此代码适用于 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;
    }

}

【讨论】:

  • 我在平板上查了一下,相机没有关闭。由前面板上的运行指示灯决定。
猜你喜欢
  • 2018-11-18
  • 2018-11-07
  • 1970-01-01
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多