问题
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