C# winform BlockInput之后,为什么按任意键都可以唤醒键盘

发布于 2022-09-12 03:14:22 字数 1665 浏览 16 评论 0

本项目是一个winform程序,code如下

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace csharp_win_tool_turnoff_screen
{
    public partial class Form1 : Form
    {
        private const uint WM_SYSCOMMAND = 0x0112;
        private const uint SC_MONITORPOWER = 0xF170;
        public Form1()
        {
            InitializeComponent();
        }

        //关屏
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
        //禁止鼠标键盘动作
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern bool BlockInput([In, MarshalAs(UnmanagedType.Bool)] bool fBlockIt);
        //锁屏切换到锁屏界面
        [DllImport("user32.dll")]
        public static extern bool LockWorkStation();

        private void button1_Click(object sender, EventArgs e)
        {
            BlockInput(true);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //关闭屏幕
            SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BlockInput(true);
            SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
        }
    }
}

我执行的方法是button3_Click
我需要得到的效果是button3单击后锁定键盘和鼠标,关闭屏幕,
但是我实际测试的效果是,可以锁定键盘和鼠标,关闭屏幕,但是按任意个键屏幕就会亮

BlockInput(true) 阻塞键盘及鼠标事件到达应用程序

我已经使用BlockInput(true)阻止了,为什么屏幕还会亮呢?是哪个进程接受到之后打开的屏幕

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2022-09-19 03:14:22

这是 Windows 2000 时代的 Win32API。

从 Windows Vista 开始不支持 64 位系统。

从 Windows 8 开始不支持带触屏设备的 32 位系统。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文