User Input in a Windows Forms Application
In Windows Forms, user input is sent to applications in the form of Windows messages. A series of overridable methods process these messages at the application, form, and control level. When these methods receive mouse and keyboard messages, they raise events that can be handled to get information about the mouse or keyboard input. In many cases, Windows Forms applications will be able to process all user input simply by handling these events. In other cases, an application may need to override one of the methods that process messages in order to intercept a particular message before it is received by the application, form, or control.
Mouse and Keyboard Events
All Windows Forms controls inherit a set of events related to mouse and keyboard input. For example, a control can handle the KeyPress event to determine the character code of a key that was pressed, or a control can handle the MouseClick event to determine the location of a mouse click. For more information on the mouse and keyboard events, see Using Keyboard Events and Mouse Events in Windows Forms.
Methods that Process User Input Messages
Forms and controls have access to the IMessageFilter interface and a set of overridable methods that process Windows messages at different points in the message queue. These methods all have a Message parameter, which encapsulates the low-level details of Windows messages. You can implement or override these methods to examine the message and then either consume the message or pass it on to the next consumer in the message queue. The following table presents the methods that process all Windows messages in Windows Forms.
Method |
Notes |
---|---|
This method intercepts queued (also known as posted) Windows messages at the application level. |
|
This method intercepts Windows messages at the form and control level before they have been processed. |
|
This method processes Windows messages at the form and control level. |
|
This method performs the default processing of Windows messages at the form and control level. This provides the minimal functionality of a window. |
|
This method intercepts messages at the form and control level, after they have been processed. The EnableNotifyMessage style bit must be set for this method to be called. |
Keyboard and mouse messages are also processed by an additional set of overridable methods that are specific to those types of messages. For more information, see How Keyboard Input Works and How Mouse Input Works in Windows Forms.