C # in the application and DLL use message

zhaozj2021-02-11  240

C #

In the application and

DLL

Use message

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.

Use custom messages in C #

Using custom messages in C # is very simple, just need the following simple steps:

1, define the message

Define the message method and the definition message in the VC.

For example, in the VC declared a custom message:

#define WM_TEST WM_USER 101

In C #, the message needs to be defined as the original 16 credit number in the Windows system, such as custom messages.

Public const Int user = 0x0400;

Then we have custom news in VC, you can do a corresponding statement in C #:

Public const INT WM_TEST = USER 101;

2, send a message

Message Send 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, message reception

After the message is issued, how do you 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 Messages

Break;

DEFAULT:

Base.defwndProc (REF M); // Call the base class function to handle non-custom messages.

Break;

}

}

Use system messages in C #

We use the processing of the WM_PAINT message as an example, and the message processing of the message is similar to the MFC in C #, but it is simpler. Declare_Message_map needs to be used in the MFC to define message mappings, and it is not required in C #. For example, WM_PAINT messages, 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), locate the form under our engineering name and select, at this time, the window of all FORM classes is listed. Function, as shown:

We selected onpaint (System.winForms.Painteventargs) At this time, 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.Forms.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

}

Example application

1, define the message

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 DLL, please refer to article:

VC.NET passes the message from the DLL to the DLL.

Public Class Message

{

Public const Int user = 0x0400;

// AS MFC Define WM_TEST WM_USER 101

Public const INT WM_TEST = USER 101;

Public const INT WM_MSG = USER 102;

}

2, declare reference function

In the place where the message is used, the referenced function is declared, and we have declared 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 the message function in the DLL

[DLLIMPORT ("Messagedll.dll", entrypoint = "startsendMessage")]]

Private Extern Static Void StartsendMessage (INTPTR HWND);

3, handle system messages

Protected Override Void Onpaint (System.Windows.Forms.Painteventargs E)

{

/// Define the font: black body, 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 Solidbrush (Color.Fromargb (0xA0, 0XA0, 0X));

/// write string

E.Graphics.drawstring ("VC Knowledge Base", Font, Blackpen, 65, 25); /// Offset 4 pixels with different colors once again, achieve stereo effects

E.Graphics.drawstring ("VC Knowledge Base", Font, Bluepen, 61, 21);

}

4, trigger custom message

// Test the 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);

}

5, response and handling custom messages

Protected Override Void DefWndProc (Ref System.Windows.Forms.Message M)

{

String message;

Switch (m.msg)

{

Case message.wm_test: // Processing Messages

Message = string.format ("Received Messages from the application! Parameters: {0}, {1}", M.WParam, M.LParam;

MessageBox.show (Message); // / Display a message box

Break;

Case message.wm_msg:

Message = String.Format ("Received Messages from DLL!

To: {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;

}

}

Program operation results:

When we click on the test DLL message, the pop-up message box displays the parameters that receive the message, the window will also call the WM_PAIN function to redraw the window.

Description: This article is a reference

"C # Development of the WINDOWS Application" One text, finishing

reference:

http://www.51pub.net/class/project.asp?cat_id=12

Program source code:

Msgapp.rar (24K)

转载请注明原文地址:https://www.9cbs.com/read-4419.html

New Post(0)