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.