C # custom message communication is often implemented in an event-driven manner,Sometimes we have to adopt the message communication mechanism of the operating system. For example, it is more convenient to interact with DLL developed in the underlying language. Some implementation methods are listed below for your reference:
1. Send via sendmessage or postmessage function:
1. Define a message
It is easy to reference underlying functions in C ++. The custom message is as follows:
# Define wm_test wm_user ++ 101
In C #, messages must be defined as original hexadecimal numbers in windows, such as custom messages.
Public const int user = 0x0400;
Public const int wm_test = user+ 101;
2. Send messages
Message sending is an API function provided by windows.SendmessageOrPostmessageIts prototype definition:
[dllimport ("user32.dll", entrypoint = "sendmessage")]
Private Static extern int sendmessage (
intptr hwnd, // form handle
uint MSG, // message identifier
uint wparam, // depends on the message
uint lparam // depends on the message
);
[Dllimport ("user32.dll", entrypoint = "postmessage")]
Private Static extern int sendmessage (
Intptr hwnd, // handle of the window that receives the message. If it is set to hwnd_broadcast, it indicates that it is delivered to all top-level windows in the system. If it is set to zero, a thread message is delivered (see postthreadmessage)
Uint MSG, // message identifier
Uint wparam, // depends on the message
Uint lparam // depends on the message
);
As for the differences between the two functions, I will not go over them here. If you are interested, you can check your own information.
3. receive messages
After a message is sent, how do I receive it in form? We can reload the defwinproc function to receive messages.
Protected override void defwndproc (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
Case message. wm_test: // process the message
Break;
Default:
Base. defwndproc (ref m); // call the base class function to process non-custom messages.
Break;
}
}
Ii. Use the postthreadmessage function to send messages to the thread
1. ing message struct prototype and custom message
Public struct tagmsg
{
Public int hwnd;
Public uint message;
Public int wparam;
Public long lparam;
Public uint time;
Public int pt;
}
Public const int wm_cx_null = 0x400 + 100;
2. Send messages
[Dllimport ("user32.dll")]
Private Static extern int postthreadmessage (
Int threadid, // thread ID
Uint MSG, // message identifier
Int wparam, // depends on the message
Int lparam); // depends on the message.
This function gets a unique thread identifier of the current thread. Note that the Win32 API cannot identify the management thread. You must send a message to the Windows Thread ID, instead of managing the thread ID.
[Dllimport ("kernel32.dll")]
Private Static extern int getcurrentthreadid ();
Therefore, the message sending process is as follows:
Private int _ newthreadid = getcurrentthreadid ();
Postthreadmessage (_ newthreadid, wm_cx_null, 1, 1 );
3. receive messages
This function retrieves a message from the message queue of the calling thread and places it in the specified structure. This function can get messages that are in contact with a specified window and thread messages sent by postthreadmesssge. This function receives a certain range of message values. Getmessage is not received by other threads or applications.ProgramMessage
[Dllimport ("user32.dll")]
Private Static extern int getmessage (
Ref tagmsg lpmsg, // pointer to the MSG structure, which receives message information from the thread's Message Queue;
Int hwnd, // the handle of the window for obtaining the message. This is a special value (null ). Getmessage is used to retrieve messages from any window that belongs to the call thread;Int wmsgfiltermin, // specifies the integer of the minimum message value to be retrieved
Int wmsgfiltermax); // specifies the integer of the maximum message value to be retrieved.
The receiving implementation is as follows:
Public void threadexectue ()
{
_ Newthreadid = getcurrentthreadid (); // The sending thread and receiving thread must be the same thread; otherwise, the message cannot be received.
While (getmessage (ref MSG, 0, 0, 0)> 0)
{
If (msg. Message = wm_cx_null)
{
MessageBox. Show ("Message received! ");
}
}
}
3. Use application. addmessagefilter to intercept system messages
1. Implement the message filter Interface
Internal class mymessager: imessagefilter
{
// Intercept the message for processing
Public bool prefiltermessage (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
Case custom_message: // intercept custom messages
MessageBox. Show ("Message received! ");
Return true; default:
Return false; // if false is returned, the message is not removed and the system will process it.
}
}
} 2. Install the message filter private void form1_load (Object sender, eventargs E)
{
Application. addmessagefilter (New mymessager ());
}