In C #, the program uses a driver that is event driven instead of the original message driver. Although the. NET Framework provides a rich range of events, the rich message defined in the previous system provides a convenient way of implementing system programming, so using messages in C # sometimes greatly improves programming efficiency.
 
1 definition messages
 
In C # The message needs to be defined as the original 16-digit number in the Windows system, such as
 
const int Wm_lbutton = 0x201; The left click message of the mouse is defined
 
public const INT user = 0X0400//is a Windows system-defined user message
 
2 message sending
 
Message sending 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,///parameter
 
int LParam//second message parameter
 
);
 
 
3 Acceptance of the message
 
 
In C #, any window has a receive processing function that is also a message, which is the Defproc function
 
 
You can overload the function in form to process messages
 
 
protected override void Defwndproc (ref System.WinForms.Message m)
 
{
 
Switch (m.msg)
 
{
 
Case Wm_lbutton:
 
String differs from the use of the format function for CString in MFC
 
String message = String. Format (Receive Message! parameter is: {0},{1} ", M.wparam,m.lparam);
 
MessageBox.Show (message);///Display a messages box
 
Break
 
Default
 
Base. Defwndproc (ref m);///calls the base class function to handle non custom messages.
 
Break
 
}
 
}
 
In fact, events in C # are also implemented by encapsulating system messages, if you do not handle that in the Defwndproc function
 
So, he'll hand it over to the system to process the message, and the system will implement the mouse-click processing function 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 message of a control, such as if you're working with an Excel file with a OWC Spreedsheet control, and you don't want the user to randomly select
 
Data to edit, you can screen out mouse events, this time you must intercept the system predefined events (this is called Subclasses in MFC), you can provide through C # an interface
 
IMessageFilter to implement the 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;
 
}
 
}