I haven't found a class member function to send a message in C #, so I can only use the SendMessage () function that invokes the win API. Because the form's handle (handler) is required in the SendMessage parameter, another API FindWindow () is called, which is used in conjunction with the message sending and receiving functions between different forms.
Another important point is that you need to receive custom messages by overriding the (Override) Form's Defwndproc () procedure. Defwndproc rewrite:
Here is my C # practice routine.
------------------------------------
/////////////////////////////////////////
File Name:Note.cs
///
public class note
{
Declaring API functions
[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
);
[DllImport ("User32.dll", entrypoint= "FindWindow")]
private static extern int FindWindow (string lpclassname,string
Lpwindowname);
Defining Message Constants
public const int USER = 0x500;
public const int TEST = USER + 1;
function to send a message to a form
private void Sendmsgtomainform (int MSG)
{
int window_handler = FindWindow (null,@ "Note Pad");
if (Window_handler = 0)
{
throw new Exception ("Could not find Main window!");
}
SendMessage (window_handler,msg,100,200);
}
}
/////////////////////////////////////////
File Name:Form1.cs
Forms to receive messages
///
public class Form1:System.Windows.Forms.Form
{
Public Form1 ()
{
//
Required for Windows Form Designer support
//
InitializeComponent ();
//
Todo:add any constructor the code after InitializeComponent call
//
}
To override a message handler function for a form
protected override void Defwndproc (ref System.Windows.Forms.Message m)
{
Switch (m.msg)
{
Receives the custom message USER and displays its parameters
Case Note.user:
String message = String. Format ("Received message!
Parameters are: {0},{1} ", M.wparam, m.LParam);
MessageBox.Show (message);
Break
Default
Base. Defwndproc (ref m);
Break
}
Console.WriteLine (m.LParam);
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.