In C #, you can use the imessagefilter interface to intercept the message I messagefilter interface of the application, allowing the application to capture the message before it is scheduled to the control or form. You can add classes that implement the imessagefilter interface to the Message Pump of an application to filter messages or perform other operations before they are scheduled to the control or form. To add the message filter to the Message pump of the application, use the addmessagefilter method in the application class.
The application. addmessagefilter method adds a message filter to monitor Windows messages sent to the target.
[Visual Basic] public shared sub addmessagefilter (byval value as imessagefilter)
Parameter Value: the implementation of the imessagefilter interface to be installed.
Note: Use message filters to prevent a specific event from being triggered, or use message filters to perform special operations on an event before it is passed to the event handler. The message filter is unique to a specific thread.
To prevent messages from being scheduled, the value parameter instance passed to the method must overwrite the prefiltermessage method with the code that processes the message. This method must return false.
Warning adding a message filter to the Message Pump of an application reduces performance.
The following is an example of how to use the imessagefilter interface to intercept messages from the keyboard or mouse:
Internal class mymessager: imessagefilter
{
Public bool prefiltermessage (ref message m)
{
// If a message with a mouse or a keyboard is detected, the count is 0 .....
If (M. MSG = 0x0200 | M. MSG = 0x0216 | M. MSG = 0x0201 | M. MSG = 0x0204 | M. MSG = 0x0207)
{
Winedetail. iopercount = 0;
}
Return false;
}
}
Add the following code to the constructor of the screen to capture the form:
// Intercept messages sent by the program to the System
Mymessager MSG = new mymessager ();
Application. addmessagefilter (MSG );
Set a timer on the screen to identify and execute events in the timer method:
Private void timereffectick (Object sender, eventargs E)
{
Iopercount ++;
If (iopercount> 10)
{
// Do something ....
}
}
The above is the logic of a simple demo program.