Author: Wang Jun, Wenling Telecom Bureau, Zhejiang Province
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 Code 17 K
1. Generate a project named MSGApplication
For how to create a project, see one of C # 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
SolidBrush blackpen = new SolidBrush (Color. FromARGB (0xa0, 0xa0, 0xb0); // create a black brush