Use messages in interaction between C # applications and DLL

Source: Internet
Author: User

In C #, it is a completely object-oriented High-level class similar to Java.Programming LanguageThe processing process is event-driven. However, in actual use, it is easier to call the system's original message for processing, especially when processing interactions with DLL files, practice has proved to be very convenient.

Use custom messages in C #

Using custom messages in C # is very simple. You only need to perform the following steps:

1. Define a message

The method for defining a message is a little different from that for defining a message in VC. For example, the method for declaring a custom message in VC is as follows:

# Define wm_test wm_user ++ 101

In C #, messages must be defined as original hexadecimal numbers in windows, such as custom messages.

Public const int user = 0x0400;

Then we can declare the custom message in VC in C:

Public const int wm_test = user+ 101;

2. Send messages

Message sending is implemented through the sendmessage API function provided by windows. Its prototype is defined as follows:

[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. receive messages

After a message is sent, how do I receive it in form? We can reload the defwinproc function to receive messages.

Protected override void defwndproc (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
Case message. wm_test: // process the message
Break;
Default:
Base. defwndproc (ref m); // call the base class function to process non-custom messages.
Break;
}
}

Use System messages in C #

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. 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.csCodeEdit. Copy the copied function definition to the form1 class and add the override keyword. Then, we can add our message processing code to the class. See the following code snippet:

Protected override void onpaint (system. Windows. Forms. 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
E. Graphics. drawstring ("VC knowledge base", Font, blackpen,); // write a string

/// Four offset pixels are written again in different colors to achieve the stereoscopic effect.
E. Graphics. drawstring ("VC knowledge base", Font, bluepen, 61,21 );
}

Example Application

1. Define a message

We add a message class in the project to define the message. Three member variables are added. user is the initial value of the custom message, which is equivalent to wm_user in MFC. Wm_test is customized to respond to applicationsProgramWm_msg is a custom message used to respond to the messages passed by the DLL. For how to define messages in DLL, seeArticle: VC. Net transmits messages from DLL to 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 referenced Functions

When using the message, declare the referenced function. Here we declare in the msgform. CS file:

// Declare the message sending 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
);

// Declare that the message function is enabled in the DLL.
[Dllimport ("messagedll. dll", entrypoint = "startsendmessage")]
Private extern static void startsendmessage (intptr hwnd );

3. process system messages

Protected override void onpaint (system. Windows. Forms. painteventargs E)
{
/// Define the font:, size: 28
Font font = new font ("", 28 );
/// Create a blue paint brush
Solidbrush bluepen = new solidbrush (color. Blue );
/// Create a black paint brush
Solidbrush blackpen = new solidbrush (color. fromargb (0xa0, 0xa0, 0xb0 ));
/// Write a string
E. Graphics. drawstring ("VC knowledge base", Font, blackpen, 65,25 );
/// Four offset pixels are written again in different colors to achieve the stereoscopic effect.
E. Graphics. drawstring ("VC knowledge base", Font, bluepen, 61,21 );
}

4. Trigger custom messages

// Test the application message
Private void testappbutton_click (Object sender, system. eventargs E)
{
Sendmessage (this. Handle, message. wm_test, 100,200 );
}
// Test the DLL message
Private void testdllbutton_click (Object sender, system. eventargs E)
{
Startsendmessage (this. Handle );
}

5. Respond to and process custom messages

Protected override void defwndproc (Ref system. Windows. Forms. Message m)
{
String message;
Switch (M. msg)
{
Case message. wm_test: // process the message
Message = string. Format ("receives the message from the application! Parameter: {0}, {1} ", M. wparam, M. lparam );
MessageBox. Show (Message); // display a message box
Break;
Case message. wm_msg:
Message = string. Format ("received message from DLL! Parameter: {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 process non-custom messages.
Break;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.