Using the Hotkey Implicit window in C #

Source: Internet
Author: User
Tags definition copy function definition prototype definition knowledge base
The event-driven approach is used in C #, but it is sometimes easier to process the system's original messages when we use them, especially when dealing with DLL files.
Using custom messages in C #
Using custom messages in C # is simple, and you just need a few simple steps:
1. Define Message
The method of defining the message is a little different from the definition message in VC
For example, in VC to declare a custom message:
#define WM_TEST Wm_user + 101
In C #, the message needs to be defined as the original 16-digit number in the Windows system, such as a custom message
public const int USER = 0x0400;
Then we declare in VC custom message, in C # can make the corresponding declaration:
public const int wm_test = USER+101;

2, send the message
Message delivery is implemented through the API function SendMessage provided by Windows, and its prototype definition is: [DllImport ("User32.dll", entrypoint= "SendMessage")]
private static extern int SendMessage (
IntPtr hWnd,//Handle to Destination window
UINT MSG,//message
UINT WParam,//Parameter
UINT LParam//second message parameter
);
3. Message Receiving
After the message is sent, how do you receive it in the form? We can overload the Defwinproc function to receive messages.
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 base class function to handle non custom messages.
Break
}
}
Using System messages in C #
We take the processing of WM_PAINT messages as an example, processing messages in C # is similar to MFC's message processing, but simpler. MFC needs to use DECLARE_MESSAGE_MAP to define message mappings, which are not needed in C #. For example, WM_PAINT messages, we simply overload the OnPaint virtual methods in the parent class, as follows:
In the menu View->other windows->object Browser Open the Object Browsing window (or open it with ctrl+alt+j), locate the form and select it in our project name, and then list the member functions of all form classes in the right-hand window. As shown in the figure:

We select OnPaint (System.WinForms.PaintEventArgs), and the complete OnPaint function protected void OnPaint is shown below ( System.WinForms.PaintEventArgs e) We copy this line of string. Open Form1.cs for code editing, we copy the function definition just copied to the Form1 class, and add the override keyword, at this time we can add our message processing code, please refer to the following code snippet:
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
Font font = new Font ("bold", 28);///definition 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
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 in a different color to write again, to achieve stereo effect
}
Example Application
1. Define Message
We added a message class to the project to define the messages. The three member variables are then added, where USER is the initial value of the custom message and is equivalent to the Wm_user in MFC. Wm_test for custom messages that respond to applications, wm_msg for custom messages that are used to respond to DLLs. How to define a message in a DLL please refer to the article: Vc.net passes messages to DLLs from DLLs.
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. Declaring reference functions
In the place where the message is used, declare the referenced function, as we stated here in the MsgForm.cs file:
Declare the message function to be sent
[DllImport ("User32.dll", entrypoint= "SendMessage")]
private static extern int SendMessage (
IntPtr hWnd,//Handle to Destination window
UINT MSG,//message
UINT WParam,//Parameter
UINT LParam//second message parameter
);

To declare a startup message function in a DLL
[DllImport ("MessageDLL.dll", entrypoint= "Startsendmessage")]
private extern static void Startsendmessage (IntPtr hWnd);

3, processing system messages
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
Definition font: Bold, Size: 28
Font font = new Font ("bold", 28);
Create a blue Brush
SolidBrush Bluepen = new SolidBrush (Color.Blue);
Create a black brush
SolidBrush Blackpen = new SolidBrush (Color.FromArgb (0xa0,0xa0,0xb0));
Write String
E.graphics.drawstring ("VC Knowledge Base", font,blackpen,65,25);
Offset 4 pixels with a different color to write again, to achieve stereo effect
E.graphics.drawstring ("VC Knowledge Base", font,bluepen,61,21);
}
4, trigger the custom message

Test application messages
private void Testappbutton_click (object sender, System.EventArgs e)
{
SendMessage (this. HANDLE,MESSAGE.WM_TEST,100,200);
}
Testing DLL messages
private void Testdllbutton_click (object sender, System.EventArgs e)
{
Startsendmessage (this. Handle);
}

5, response and processing of custom messages
protected override void Defwndproc (ref System.Windows.Forms.Message m)
{
String message;
Switch (m.msg)
{
Case MESSAGE.WM_TEST://Processing Message
Message = string. Format (Received message from application! parameter is: {0},{1} ", M.wparam,m.lparam);
MessageBox.Show (message);///Display a messages box
Break
Case MESSAGE.WM_MSG:
Message = string. Format ("received message from DLL!)
is: {0},{1} ", M.wparam,m.lparam);
MessageBox.Show (message);///Display a messages box
Break
Default
Base. Defwndproc (ref m);//Call base class function to handle non custom messages.

Break
}
}
Program Run Result:
When we click the test DLL message, the pop-up message box displays the parameters of the message, and the window is also called Wm_pain function to redraw the window.





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.