C # message processing when developing a WINDOWS Application

Source: Internet
Author: User

WINDOWS applications are message-driven. In VC, we can use CLASSWIZARD to add message processing functions for a window class. CLASSWIZARD will add message ing for you. For WINDOWS messages, the generated message processing function reloads the Virtual Methods of the base class. In C #, how does one process messages? This article introduces WINDOWS messages and how to process custom messages in C # In VS. NET BETA1.

Download Sample Code17 K

1. Generate a project named MSGApplication
For how to create a project, see:C # One of Study Notes

Ii. Process WM_PAINT messages
Let's take WM_PAINT message processing as an example. In C #, message processing is similar to that in MFC, but it is simpler. In MFC, DECLARE_MESSAGE_MAP must be used to define message ing, which is not required in C. For example, for the WM_PAINT message, we only need to reload the OnPaint virtual method in the parent class (although the process of reloading the parent virtual method in BETA1 is a bit cumbersome), the method is as follows:
Choose View> Other Windows> Object Browser to open the Object browsing window (or press CTRL + ALT + J to open the window), find the Form under our project name, and select, in this case, the window on the right lists all Form class member functions ,:
We select OnPaint (System. WinForms. PaintEventArgs). The complete OnPaint function protected void OnPaint (System. WinForms. PaintEventArgs e) is displayed below. We Copy this line of string. Open Form1.cs to edit the code. Copy the copied function definition to the Form1 class and add the override keyword. Then, we can add our message processing code to it, see the following code snippet:

Protected override void OnPaint (System. winForms. paintEventArgs e) {Font font = new Font ("", 28); // defines the Font:, size: 28 SolidBrush bluepen = new SolidBrush (Color. blue); // create a Blue paint brush. fromARGB (0xa0, 0xa0, 0xb0); // create a black brush e. graphics. drawString ("VC knowledge base", font, blackpen, 65,25); // write string e. graphics. drawString ("VC knowledge base", font, bluepen,); // offset four pixels and write them again in different colors to achieve the stereoscopic effect}

The same method can be used to process other messages and to overload other parent-class virtual functions.

3. Custom Message Processing
To facilitate processing, we encapsulate the custom messages that need to be used into a class. The process of adding a class is as follows:
In ClassView, select our project MSGApplication, right-click it, and choose Add-> Add Class from the pop-up menu. Class Wizard is displayed. we name the Class WM, and other options are inconvenient, confirm that the class is added. We add two member variables for the WM class, as shown in the following code:

public class WM{public const int USER = 0x0400;public const int TEST1 = USER+1;}
We add a button in Form1 and add event processing code for the button. (If you are not familiar with this process, see:C # One of Study Notes)
Send a custom TEST1 message to the main window. The Code is as follows:
protected void button1_Click (object sender, System.EventArgs e){SendMessage(WM.TEST1,100,200);}

The message has been sent. In Form1, how do we respond to the message? We can overload the DefWndProc method.

Protected override void DefWndProc (ref System. winForms. message m) {switch (m. msg) {case WM. TEST1: // The Format function of string and CString in MFC is used differently. string message = string. format ("Receive message! Parameter: {0}, {1} ", m. wParam, m. lParam); MessageBox. show (message); // display a message box break; default: base. defWndProc (ref m); // call the base class function to process non-custom messages. Break ;}}

Program running result

When you click send message, the message box is displayed and the received parameters are displayed.

Note: It is expected that the operations in the above process will be greatly changed in a later version of VS. NET BETA1, but the principles are consistent. Let's look forward to a more perfect C # display in front of us.


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.