C # capture mouse messages via IMessageFilter (reprint)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.