Treatment of the two study notes C # C # the development of WINDOWS application message: Wenling City in Zhejiang Province Wang Jun bureau
The Windows application is driven by message. In the VC, we can add message processing functions for a window class through ClassWizard. ClassWizard will add message mapping for you. For Windows messages, the generated message processing function is overloaded with the virtual method of the base class. . How to deal with the message in C #? This article briefly introduces the Windows message and the processing method of custom messages in vs.Net beta1 environments. Sample Code Download 17K 1, Generate a Works of Engineering Engineering Named MsGApplication For reference: C # learning note, handle WM_PAINT Message We use the WM_PAINT message processing as an example, handle messages and MFC messages in C # are Similar, but simpler. Declare_Message_map needs to be used in the MFC to define message mappings, and it is not required in C #. For example, the WM_Paint message, we only need to overload the onpaint virtual method in the parent class (although the operation process of heavy-old parental virtual methods in beta1 is a bit cumbersome), the method is as follows: on the menu view-> Other windows-> Object Browser open Object Browsing Window (or opens with Ctrl Alt J), find Form under our engineering name and select, then list all FORM members' functions, as shown in the figure: We selected onpaint (System " .Winforms.painteventargs) At this point, the full OnPaint function protected (System.winForms.Painteventargs E) will come down to COPY. Open Form1.cs for code editing, we copy the function definition that just copied to the Form1 class, and add an Override keyword. At this time, we can add our message to handle the code, please refer to the following code segment: Protected Override Void Onpaint (System.winForms.PainteventArgs E)
{
Font font = new font ("black body", 28); // definition font: black body, size: 28
Solidbrush Bluepen = New Solidbrush (Color.Blue); // Create Blue Brush
Solidbrush Blackpen = New Solidbrush (Color.Fromargb (0xA0, 0XA0, 0X)); /// Create a black brush
E.Graphics.drawstring ("VC Knowledge Base", Font, Blackpen, 65, 25); /// Writing
E.Graphics.drawstring ("VC Knowledge Base", Font, Bluepen, 61, 21); /// Offset 4 pixels with different colors once again, achieve stereo effects
}
The same method can be employed for the processing of other messages and overloading of other parent class virtual functions. Third, the processing of custom messages is for handling, here we will need to use custom messages into a class, the addition process of the class is as follows: Select our project in ClassView MsGApplication, right mouse button, put out Select Add-> Add Class in the menu, pop up Class Wizard, we name the class is WM, the rest of the option is inconvenient, confirm the addition of the class. We add two member variables to 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 in Form1 and add event handling code for the button (if you are not familiar with this process, please refer to: C # learning notes) Send Test1 custom messages to the main window, the specific code is as follows: protected void button1_click (Object sender, system.eventargs e) {
SendMessage (wm.test1, 100, 200);
}
The news has been issued, how do we respond to this message in Form1? We can overload the DEFWNDPROC method. Protected Override Void DefWndProc (Ref System.winForms.Message M)
{
Switch (m.msg)
{
Case wm.test1:
/// String is different from the method of usage of cstring in the MFC.
String message = string.format ("Received Message! Parameters: {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 handle non-custom messages.
Break;
}
}
The program operation results are shown when we send the message button, pop up the message box and display the received parameters. Postscript: It can be expected that the operation of the above process in the subsequent version of VS.NET Beta1 is greatly changed, but the principle is consistent. Let us look forward to a more perfect C # presentation in front of us.