In C #, the driver used by the program is event-driven rather than the original message-driven, although the. NET Framework provides a rich range of events, but in the previous system has defined a rich message to the system programming provides a convenient way to implement, so in C # in the use of messages sometimes greatly improve the efficiency of programming.
1 Defining messages
In C #, messages need to be defined as the original 16 digits in a Windows system, such as
const int Wm_lbutton = 0x201; Defines the left-click message for the mouse
public const INT user = 0X0400//is a Windows system defined users message
2 message Sent
Message delivery is implemented through the API function SendMessage provided by Windows, and its prototype is defined as
[DllImport ("User32.dll", entrypoint= "SendMessage")] private static extern int SendMessage (int hWnd,//Handle to destination window int MSG,//message int WParam,//First message parameter int LParam Second message parameter);
3 Acceptance of the message
In C #, any window has a receive handler for the message, which is the Defproc function
You can overload the function in the form to process the message
protected override void Defwndproc (ref System.WinForms.Message m) {switch (m.msg) {case Wm_lbutton:///string with MFC Cstri Ng's Format function uses a different method than string message = string. Format ("Message received!" parameter: {0},{1} ", M.wparam,m.lparam); MessageBox.Show a message box break;default:base. Defwndproc (ref m);///Call the base class function to process a non-custom message. Break;}}
In fact, events in C # are also implemented by encapsulating system messages, if you do not process the Defwndproc function in the
Then, he will hand it over to the system to process the message, and the system will implement the mouse-click handler through the proxy, so you can
Defproc function to intercept a message, such as a click message that you want to intercept a button
4 other message processing methods in C #
In C #, there are times when you need to preprocess the control's messages, such as you use OWC's Spreedsheet control to handle Excel files, and you don't want users to be able to easily select
Data for editing, you can block out mouse events, this time you have to intercept the system pre-defined events (this is referred to as subclasses in MFC), you can provide an interface through C #
IMessageFilter to implement filtering of messages
public class form1:system.windows.forms.form,imessagefilter{const int WM_ MOUSEMOVE = 0x200 public bool Prefiltermessage (ref Message m) {keys keycode = (keys) (int) M.wparam & Keys.keycode; if (m.msg = = m.msg==wm_mousemove)//| | M.msg = = wm_lbuttondown {//messagebox.show ("ignoring Escape ..."); return true; } return false; }}