Using messages in C # applications interacting with DLLs

Source: Internet
Author: User
Tags definition function definition prototype definition knowledge base
C# is a completely object-oriented high-level programming language similar to Java, and its process is event-driven. However, in the actual use process, it is sometimes easier to process by calling the original messages of the system, especially when dealing with the interaction with DLL files, which has been proved to be very convenient.

Use custom messages in C#

Using custom messages in C# is simple, with a few simple steps:

1. Define the message

The way you define a message is a little different from the way you define a message in VC, where you declare a custom message like this:

#define WM_TEST WM_USER + 101

In C#, messages need to be defined as the original hexadecimal Numbers in the Windows system, such as custom messages

Public const int USER = 0x0400;

Then the corresponding declaration can be made in C# for the custom message we declared in VC:

Public const int WM_TEST = USER+101;

2. Send a message

SendMessage is sent via the windows-provided API function SendMessage, whose prototype definition is:

[DllImport (" User32. DLL, "EntryPoint =" SendMessage)]
Private static extern int SendMessage(
IntPtr hWnd, // handle to destination window
Uint Msg, / / the message
Uint wParam, // first message parameter
Uint lParam // second message parameter
);

3. Message reception

After the message is sent, how is it received in the Form? We can override the DefWinproc function to receive the message.

Protected override void DefWndProc (ref System. Windows. Forms. The Message m)
{
The switch (m.M sg)
{
Case message.wm_test: // process the Message
Break;
Default:
Base. DefWndProc (ref m); // calls to base class functions handle non-custom messages.
Break;
}
}

Use system messages in C#

Let's take the processing of the WM_PAINT message as an example. The processing of the message in C# is similar to that of MFC, but simpler. The message mapping is defined using DECLARE_MESSAGE_MAP in MFC, but not in C#. For example, 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 the Object Browser window (or CTRL+ALT+J), find the Form under our project name and select it, then list all the member functions of the Form class in the right window.

We selected OnPaint (System. WinForms. PaintEventArgs) at this point in the following shows the complete OnPaint function protected void OnPaint (System. The WinForms. PaintEventArgs e) we will string to Copy down the line. Open form1.cs for code editing. We copy the function definition we just copied into the Form1 class and add the override keyword. Now we can add our message handling code to it, please refer to the following code snippet:

Protected override void OnPaint (System. Windows. Forms. PaintEventArgs e)
{
Font = new Font(" bold ",28); /// define 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
DrawString("VC knowledge base ",font,blackpen,65,25); /// writes a string
/// offset 4 pixels and write them in different colors again to achieve the stereo effect
DrawString("VC knowledge base ",font,bluepen,61,21);
}
Related Article

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.