Two Methods for intercepting messages on form in winform

Source: Internet
Author: User

For the original article, see:Two Methods for intercepting messages on form in winform

The defwndproc Method for reload form is commonly used, for example, intercepting the message pressed by the mouse:

Protected override void defwndproc (ref message m)
{
If (M. MSG = 0x0201)
{
MessageBox. Show (M. msg. tostring ());
}
Else
{
Base. defwndproc (ref m );
}
}

You can also use the imessagefilter interface: public class messagefilter: imessagefilter
{
Public bool prefiltermessage (ref message m)
{
If (M. MSG = 0x0201)
{
MessageBox. Show ("wm_lbuttondown is:" + M. msg );
Return true;
}
Return false;
}

}

Use the application. addmessagefilter method, for example:
Private Static messagefilter msgfliter = new messagefilter ();
Register the message filter in the main method:
Application. addmessagefilter (msgfliter );
To cancel registration, you can call the application. removemessagefilter method.

Here is a Windows messageid enumeration, which is very useful and has forgotten the address several times. Please save it here this time.
Windows Message ID Constants

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.