SendMessage is used in c sharp
- 2020-05-05 11:10:24
- OfStack
in C # program using the driver using the event-driven instead of the original message driven, though. net framework provides event has is very rich, but rich messages defined in the previous system of system programming provides a convenient method to realize, so use in C # message can be greatly improve the efficiency of programming.
1 define the message
In c# the message needs to be defined as the original hexadecimal number in the windows system, such as
const int WM_Lbutton = 0x201; // defines the left-click message
for the mousepublic const int USER = 0x0400 // is the user message
defined by the windows system
2 message sending
Message delivery is achieved through the API function SendMessage provided by windows whose 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 message acceptance
In C#, any window has a receive handler for the message, which is the defproc function
You can override this function in form to handle the message
protected override void DefWndProc ( ref System.WinForms.Message m )
{
switch(m.msg)
{
case WM_Lbutton :
///string is used differently from CString's Format function in MFC
string message = string.Format(" message received! Parameters as follows: the & # 123; 0 & # 125; , & # 123; 1 the & # 125;" , m. wParam, m. lParam);
MessageBox. Show (message); /// displays a message box
break;
default:
base. DefWndProc (ref m); /// calls to base class functions to handle non-custom messages.
break;
}
}
In fact, events in C# are also implemented by encapsulating system messages, if you do not handle
in the DefWndProc functionThen, he hands over to the system to process the message, and the system implements the mouse-click handler via the proxy, so you can use
The defproc function intercepts messages, such as when you want to intercept a button click message
4 other message handling methods in C#,
Sometimes in C#, you need to preprocess the message of the control. For example, if you use owc's spreedsheet control to process the Excel file, you don't want the user to be free to select
As you edit the data, you can block mouse events, which must be pre-defined by the system (this is referred to as subclassing in MFC), and you can do so through
, an interface provided by C#IMessageFilter to implement message filtering
public class Form1: System.Windows.Forms.Form,IMessageFilter
{
const int WM_MOUSEMOVE = 0x200
public bool PreFilterMessage(ref Message m)
The & # 123; Keys keyCode = (Keys)(int) m. WParam & Keys. KeyCode;
if(m.Msg == m.Msg ==WM_MOUSEMOVE) //|| m.Msg == WM_LBUTTONDOWN
The & # 123;
/ / MessageBox. Show (" Ignoring Escape..." );
return true;
The & # 125;
return false;
The & # 125;
}