C # How to define and receive messages?

Source: Internet
Author: User

 

C # How to define and receive messages?

Wason

Amateur Learning Achievements: Finally, We can get custom messages and share more things!

Currently, in C #, I have not found the class member function for sending messages, so I can only use SendMessage () function that calls WIN 32 API. Because the SendMessage parameter needs to obtain the form handle (handler), it needs to call another API FindWindow (). The two are used together to send and receive messages between different forms.

Another important point is that the DefWndProc () process of the Override form needs to receive custom messages. Rewrite DefWndProc:

Protected override void DefWndProc (ref System. Windows. Forms. Message m)
{
Switch (m. Msg)
{
Case ...:
Break;
Default:
Base. DefWndProc (ref m );
Break;
}
}

The following is my C # practice routine.
------------------------------------
/////////////////////////////////////////
/// File name: Note. cs
///
Public class Note
{
// Declare API functions

[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
);
[DllImport ("User32.dll", EntryPoint = "FindWindow")]
Private static extern int FindWindow (string lpClassName, string
LpWindowName );
// Define the message constant
Public const int USER = 0x500;
Public const int TEST = USER + 1;

// Function for sending messages to the form

Private void SendMsgToMainForm (int MSG)
{
Int WINDOW_HANDLER = FindWindow (null, @ "Note Pad ");
If (WINDOW_HANDLER = 0)
{
Throw new Exception ("cocould not find Main window! ");
}
SendMessage (WINDOW_HANDLER, MSG, 100,200 );
}
}


/////////////////////////////////////////
/// File name: Form1.cs
/// Message receiving form
///

Public class Form1: System. Windows. Forms. Form
{
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// Rewrite the message processing function of the form
Protected override void DefWndProc (ref System. Windows. Forms. Message m)
{
Switch (m. Msg)
{
// Receive the USER of the custom message and display 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 );
}

--

Wilson Wei

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.