Using hotkeys in C # hidden windows

xiaoxiao2021-03-06  101

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 messages and VC definition messages have a little bit different, such as declaring a custom message in VC: #define WM_TEST WM_USER 101 and messages in C # need to be defined as the original 16 credit numbers in the Windows system, such as self Define message public constinary = 0x0400; then we have custom messages declared 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 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) {// definition font: black body, size: 28 font font = new font ("black body", 28); /// creation blue Brush Solidbrush Bluepen = New Solidbrush (Color.Blue); // Create a Black Brush Solidbrush Blackpen = New Solidbrush (Color.Fromargb (0xA0, 0XA0, 0XB0)); /// Writing Strings E.Graphics.drawstring ("VC Knowledge Base, Font, Blackpen, 65, 25); /// Offset 4 pixels with different colors once again, achieve 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);

5, Response and Processing Custom Message Protected Override Void DefwndProc (Ref System.Windows.Forms.Message M) {String Message; Switch (M.MSG) {CASE MESSAGE.WM_TEST: // Process Message Message = String.Format (" Received the message 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! Parameters: {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 running 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.

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

New Post(0)