In C # How to disable the use of the left mouse button, in fact, we can through the IMessageFilter interface under the Prefiltermessage method, Application class Addmessagefilter method, Removemessagefilter the Msg property of the method and message structure to disable the left mouse button. The message structure wraps messages sent by Windows, uses the structure to wrap the message, and assigns it to a window procedure for scheduling, and it can also be used to get information about a message that the system sends to an application or control.
Use the Prefiltermessage method to filter out messages before they are dispatched. The syntax format is as follows:
Bool prefiltermessage (refmessage m)
Parameter description:
M: This message cannot be modified to dispatch a message.
Return value: True if the message is filtered and the message is prevented from being dispatched, or false if the message is allowed to continue to reach the next filter or control. Use the Addmessagefilter method to add a message filter to monitor these messages when a Windows message is delivered to the destination. Causes Removemessagefilter to remove a message filter from the application's message pump.
Here's a look at the main code:
public partial class Form1:form,imessagefilter
{
Public Form1 ()
{
InitializeComponent ();
}
public bool Prefiltermessage (ref System.Windows.Forms.Message Mymessage)
{
Do not respond to left mouse button messages
If (mymessage.msg>=513 && mymessage.msg<=515)
{
Return true;
}
Return false;
}
Private voi Button1_Click (Object Sender,eventargs e)
{
Application.addmessagefilter (this);
MessageBox.Show ("The left mouse button has been disabled, please use the TAB key to perform the action", "Information tip",
Messageboxbuttons.ok,messageboxicn.information);
}
Private void button2_click (Object Sender,eventargs e)
{
Application.removemessagefilter (this);
MessageBox.Show ("The left mouse button has been lifted, you can perform the operation!") "," info tip ",
Messageboxbuttons.ok,messageboxicon.information)
}
}
Mouse movement: 512
Left mouse button:
down:513
up:514
Double click:515
right mouse button:
down:516
up:517
Mouse wheel: 522
C # capture mouse messages via IMessageFilter (reprint)