How to suppress global mouse click events from windows?

守給你的承諾、 提交于 2019-12-01 13:04:12

问题



I am developing a windows based application in which i want that whenever my application get started it should disable mouse click events outside the my application window form.

Can anyone please tell me, how can i achieve that?

Thanks in advance.

Edit :
Catching the mouse click event within the form and suppressing the click action is easy, for that we just use this :

    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);
    }

but how to catch the mouse click event outside of the my app form?


回答1:


This way it can be done. It uses the win API function BlockInput.

NOTE: CTRL + ALT + DELETE enables the input again. But other mouse and keyboard input is blocked.

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); 
        }
    }
}


来源:https://stackoverflow.com/questions/19115578/how-to-suppress-global-mouse-click-events-from-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!