In C #, the event driver is used, but during the process we use, it is sometimes simple to process by calling the original message, especially when processing the interaction with the DLL file, is indeed very very Convenience. Using custom messages in C # Using custom messages in C # is very simple, only the following simple steps are required: 1. Define messages defined messages and VC definition messages in VC, such as in VC. A custom message: #define wm_test WM_USER 101 and in C # Messages need to be defined as the original 16 credit numbers in the Windows system, such as custom message public constinary = 0x0400; then we declare in VC Define the message, you can do a corresponding declaration in C #: public const INT WM_TEST = USER 101; 2, send message messaging is implemented by the API function sendMessage provided by Windows, its prototype definition: [DLLIMPORT ("User32 .dll ", EntryPoint =" SendMessage ")] private static extern int SendMessage (IntPtr hWnd, // handle to destination window uint Msg, // message uint wParam, // first message parameter uint lParam // second message parameter); 3 After the message receives the message, how to receive it in Form? We can overload the DEFWINPROC function to receive the message. Protected Override Void DefwndProc (Ref System.Windows.Forms.Message M) {Switch (M.MSG) {CASE message.WM_TEST: // Processing Message Break; default: base.defwndproc (ref m); // Call the base class function Treatment of non-custom messages. Break;}} Using System Messages in C # Using System Messages as an example, processing messages in C # are similar to MFC messages, but more simple. 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, the method is as follows: In the menu view-> Other windows-> Object Browser Open Object Browse Window (or open with Ctrl Alt J), in our Locate Form and select it, then list all FORM class's member functions, as shown in the figure: We selected onpaint (System.winForms.Painteventargs) This time you will display the full onpaint function Protected Void OnPaint (System.winForms.Painteventargs E) We will click this line string 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.windows e) {font font = new font ("black body", 28); /// definition font: black body, 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 with different colors once, achieve stereo effect} Example Application 1, Defining Messages We add a Message class to the project to define a message. Then add three member variables, where user is the initial value of the custom message, is quite the WM_USER in the MFC. WM_TEST is a custom-defined message that responds to the application, WM_MSG is a custom-defined message that responds to DLL. How to define messages in a DLL message: VC.NET passes messages from DLL to DLL.
Public Class Message {public const amount = 0x0400; // AS MFC Define WM_TEST WM_USER 101 PUBLIC Const Int WM_TEST = USER 101; Public Const INT WM_MSG = USER 102;} 2, the declaration reference function is in use, Declaring the referenced function, we are here in the msgform.cs file: // State the send message function [DLLIMPORT ("User32.dll", entrypoint = "sendMessage")] Private static extern int sendMessage (INTPTR HWND, // Handle To destination window uint Msg, // message uint wParam, // first message parameter uint lParam // second message parameter); // start declaration message DLL function [DllImport ( "MessageDLL.dll", EntryPoint = "StartSendMessage")] private Extern Static Void StartsendMessage (INTPTR HWND); 3, Processing System Message Protected Override Void Onpaint (System.Windows.Forms.PainteventArgs E) {// Defining Font: Black, Size: 28 Font Font = New Font ("Black Body", 28); // Create a blue brush Solidbrush Bluepen = new solidbrush (color.blue); // Create a black brush Solidbrush Blackpen = New Solidb Rush (Color.Fromargb (0xA0, 0XA0, 0X)); /// Writing string E.Graphics.drawstring ("VC Knowledge Base", Font, Blackpen, 65, 25); // / Off 4 pixels Write once with different colors, reach stereo effect E.Graphics.drawString ("VC Knowledge Base", Font, Bluepen, 61, 21);} 4, trigger custom message // Test application message Private void testAppButton_Click (Object sender, System.EventArgs e) {SendMessage (this.Handle, Message.WM_TEST, 100,200);} // test DLL message private void TestDLLbutton_Click (object sender, System.EventArgs e) {StartSendMessage (this.Handle);