如何禁止显示来自窗口的全局鼠标单击事件

本文关键字:全局 鼠标 单击 事件 窗口 禁止显示 | 更新日期: 2023-09-27 18:33:31


我正在开发一个基于 Windows 的应用程序,我希望每当我的应用程序启动时,它都应该禁用我的应用程序窗口窗体之外的鼠标单击事件。

谁能告诉我,我怎样才能做到这一点?

提前谢谢。

编辑:
在表单中捕获鼠标单击事件并抑制单击操作很容易,为此我们只使用它:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == (int)MouseMessages.WM_LBUTTONDOWN || m.Msg == (int)MouseMessages.WM_LBUTTONUP)
            MessageBox.Show("Click event caught!");  //return; --for suppress the click event action.
        else
            base.WndProc(ref m);
    }

但是,如何在"我的应用"窗体之外捕获鼠标单击事件?

如何禁止显示来自窗口的全局鼠标单击事件

这样

就可以做到了。它使用 win API 函数 BlockInput。

注意:CTRL + ALT + DELETE再次启用输入。但其他鼠标和键盘输入被阻止。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void BlockInput([In, MarshalAs(UnmanagedType.Bool)]bool fBlockIt);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Show();
            //Blocks the input
            BlockInput(true);
            System.Threading.Thread.Sleep(5000);
            //Unblocks the input
            BlockInput(false); 
        }
    }
}