window| Program
Windows applications are message driven, in VC we can add a message handler function for a window class by ClassWizard, ClassWizard will add message mapping for you, and for Windows messages, the generated message handler functions overload the virtual method of the base class. And how do you handle messages in C #? This article simply describes Windows messages and how to handle custom messages for C # in the Vs.net BETA1 environment.
Sample code Download 17K
First, the production of a project called Msgapplication
How to build the project please refer to: C # Learning notes One
Ii. Processing of WM_PAINT messages
We take the processing of WM_PAINT messages as an example, processing messages in C # is similar to MFC's message processing, but simpler. MFC needs to use DECLARE_MESSAGE_MAP to define message mappings, which are not needed in C #. For example WM_PAINT messages, we simply overload the OnPaint virtual methods in the parent class (although it is a bit tedious to overload the parent-class virtual methods in BETA1), the following methods are:
In the menu View->other windows->object Browser Open the Object Browsing window (or open it with ctrl+alt+j), locate the form and select it in our project name, and then list the member functions of all form classes in the right-hand window. As shown in the figure:
We select OnPaint (System.WinForms.PaintEventArgs), and the complete OnPaint function protected void OnPaint is shown below ( System.WinForms.PaintEventArgs e) We copy this line of string. Open Form1.cs for code editing, we copy the function definition just copied to the Form1 class, and add the override keyword, at this time we can add our message processing code, please refer to the following code snippet:
protected override void OnPaint (System.WinForms.PaintEventArgs e)
{
Font font = new Font ("bold", 28);///definition font: bold, Size: 28
SolidBrush Bluepen = new SolidBrush (color.blue);///Create a blue brush
SolidBrush Blackpen = new SolidBrush (Color.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,61,21);///offset 4 pixels in a different color to write again, to achieve stereo effect
}
The same method can be used for handling other messages and for overloading other virtual functions of the parent class.
Iii. Handling of custom messages
For ease of handling, here we encapsulate the custom messages we need to use as a class, and the class is added as follows:
In ClassView Select our project msgapplication, press the right mouse button, in the pop-up menu, select Add->add class, Pop-up class Wizard, we will name the class wm, the rest of the options inconvenience, confirm the completion of the class Add. We add two member variables for the WM class, see the following code:
public class WM
{
public const int USER = 0x0400;
public const int TEST1 = user+1;
}
We add a button to the Form1 and add event handling code for the button (if you are unfamiliar with the process, please refer to one of the C # Learning notes)
Send TEST1 custom messages to the main window, as follows:
protected void Button1_Click (object sender, System.EventArgs e)
{
SendMessage (WM. TEST1,100,200);
}
The message has been sent, how do we respond to the message in the Form1? We can overload the Defwndproc method.
protected override void Defwndproc (ref System.WinForms.Message m)
{
Switch (m.msg)
{
Case WM. TEST1:
String differs from the use of the format function for CString in MFC
String message = String. Format (Receive Message! parameter is: {0},{1} ", M.wparam,m.lparam);
MessageBox.Show (message);///Display a messages box
Break
Default
Base. Defwndproc (ref m);///calls the base class function to handle non custom messages.
Break
}
}
Program running results as shown
When we click the Send Message button, the message box pops up and displays the received parameters.
PostScript: It can be expected that the operation of the above process will change significantly in subsequent versions of Vs.net BETA1, but the principle is consistent. Let's hope for a more perfect C # to unfold in front of us.