The Windows Messaging system consists of 3 parts:
1. Message Queuing. Messages for Windows applications are managed by Windows Unified in a message queue. 2. Message loops. The application obtains its own message from the Windows message queue and assigns it to the window procedure for processing. 3. Window procedure. is responsible for processing the received messages, each window has a corresponding window procedure, is responsible for intercepting the message and responding. WndProc is a window procedure function that handles received messages, and when we write code, we don't notice this function because the development environment is automatically generated for us. WndProc function through Switch...case ... The Defwndproc function is called to handle messages that are not encountered in the case, so the user-defined message in C # can be overloaded with the Defwndproc function using Switch...case ... Judgment and processing. the events in C # are the encapsulation of Windows messages, which define the events of the form during the window, to the extent that the programmer avoids too much consideration of the underlying things such as messages, so it is best to avoid the problems that the message mechanism solves.
In general, WinForm's message processing mechanism is mostly done through event handlers, but when there is no corresponding event, the usual practice is to declare defwndproc or WndProc or imessagefilter, and often see on the Internet that there are articles that are tied to three, So what's the difference between them? This article makes a simple analysis of the following:
Both Defwndproc and WndProc are virtual methods that inherit from the control class and are prototyped as follows:
protected Override void Defwndproc (ref Message m) {.... Base . Defwndproc (m);} protected Override void WndProc (ref Message m); {... Base . WndProc (m);}
All controls that have a user interface inherit from control, which requires the creation of a derived class of the corresponding control, which does not uniformly intercept the messages of each window, because they are essentially Windows window procedures and only receive messages for the window itself.
By reviewing the Windows message processing mechanism, the relationship between the three can be better understood. The application's message is from the system message queue and is handled by the message loop in the application's main program. This message loop takes the message out of the application's message queue, is preprocessed, and then distributes it to the window procedure that corresponds to the message, and the window procedure is processed according to the type of message after it is called, and some messages that can be processed by Windows by default call DefWindowProc of Windows.
The WndProc here is the window procedure for the corresponding control window, and Defwndproc is called by WndProc to handle messages that are not processed in WndProc (including WndProc), so defwndproc receives less messages than WndProc.
the invocation of imessagefilter occurs in the message loop of the application and is part of the message preprocessing, so the message it receives is more complete (except for those messages that are sent directly to the window procedure that do not enter the message queue). use the following methods:
Public classmessagefilter:imessagefilter{ Public BOOLPrefiltermessage (refMessage msg) { //identify messages and process//return true;//Swallow News, do not distribute return false;//go to next to distribute to the corresponding window process }} //Adding a message filter to the application message loopMessageFilter f =NewMessageFilter ( This. lbmsg); Application.addmessagefilter (f);
The difference between Defwndproc, WndProc and IMessageFilter in WinForm