[C # FAQ] preprocessing Win32 messages through Windows Forms

Source: Internet
Author: User
Tip from Andrew troelsen | posted by: Duncan macenzie, msdn | translated by findekano
Note: Article Source: C # FAQ. Translate this article for personal hobbies only. If you have any mistakes, please advise.

In the unmanaged world, it is common to take Win32 messages for processing. Win32 messages are applied by users.ProgramIntercept is like being popped up in the message queue. However, this is rare in hosting windows form applications. Anyway, you can take the following steps to intercept and process Windows messages.
First, create a helper class that implements the imessagefilter interface. You can use its unique method prefiltermessage () to obtain the original wparam and lparam data, that is, the actual message id. The following is a simple example:

Public   Class Mymessagefilter: imessagefilter
{
Public   Bool Prefiltermessage ( Ref Message m)
{
// Intercept the left mouse button down message.
If (M. msg =   513 )
{
MessageBox. Show ("Wm_lbuttondown is:" +M. msg );
Return True;
}
Return   False ;
}
}

Register the helper class with the application type in the main program:

Public   Class Mainform: system. Windows. Forms. Form
{
Private Mymessagefilter msgfliter =   New Mymessagefilter ();

Public Mainform ()
{
//Register message filter.
Application. addmessagefilter (msgfliter );
}
...
}

At this time, the message will be pre-processed by the custom message filter before it is sent to the registered event processor. You can use application. removemessagefilter () to delete the originally registered message filter.

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.