About using C# to call Win32API, grab third-party handles, simulate mouse clicks, and send keyboard events

Because the recent work needs to be used, I sorted out the basic knowledge

The following examples, Win32 and self-encapsulated libraries are all in the project file

Link: https://pan.baidu.com/s/1imOVeULlxe82Ejv0dPNy_Q?pwd=6666
Extraction code: 6666
--Sharing from Baidu Netdisk Super Member V1

1 Install handle grabber SPY++

The tool I use is SPY++

If you don’t have this tool, install it yourself as follows

Select “Get Tool Functions” under the Tools menu

1.

Select “C++ Core Function” in a single component to install it

The main purpose of downloading this tool is to grab the handle

see next operation

First open our SPY++ tool

Click on the window to create a new one

Then all the information about the program running will appear

We only find what we need, open the form we need to find the handle

Click Search > Find Window

Then there will be such an interface

We only need to drag the finder, that is, which round thing to the specified form to get the handle of the form

We dragged to the Program button and got the button handle

Next, let’s practice it in C#

0x00010A3C is the handle we got just now. 0x0201 and 0X0202 are the left mouse button pressed and popped up respectively. After running, we can see that the button on the software is indeed clicked. You have to practice by yourself

 // import Windows API function, used to send messages to the window
        [DllImport("User32.dll")]
        public static extern int SendMessage(int hWnd, int msg, int wParam, string lParam);

        private void button7_Click(object sender, EventArgs e)
        {
            PostMessage((IntPtr)0x00010A3C, 0x0201, 0x0001, 0); //press the left button
            Thread. Sleep(200);
            PostMessage((IntPtr)0x00010A3C, 0x0202, 0x0001, 0); // bounce
        }

Here we need to pay attention to our third-party software handle is re-allocated every time the software is reopened, so the handle is an uncertain value, so how do we get it?

 // import Windows API function, used to find the window handle
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // Import Windows API function to find child window handle
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpClassName, string lpWindowName);


        /// <summary>
        /// Get the main window handle
        /// </summary>
        /// <param name="APPname">If there is no main form name, fill in null</param>
        /// <param name="APPclass">Main form class This must be filled</param>
       public IntPtr Get_Main_handle(string APPname,string APPclass)
        {
           return FindWindow(APPclass, APPname);
        }
        /// <summary>
        /// Get the handle of the subform under the main form
        /// </summary>
        /// <param name="Main_APPname">Main form name</param>
        /// <param name="Main_APPclass">Main form class</param>
        /// <param name="child_APPclass">child form class</param>
        /// <param name="child_APPname">The child form name can be null</param>
        /// <returns>Return found handle</returns>
        public IntPtr Get_child_handle(string Main_APPname, string Main_APPclass, string child_APPclass, string child_APPname)
        {
            IntPtr stmHandle = FindWindow(Main_APPclass, Main_APPname);
            //Start searching from the 0th form under the parent form, the class is SWT_Window0 and the title is null, which means ignore the title
           return FindWindowEx(stmHandle, IntPtr. Zero, child_APPclass, child_APPname);
        }

Next, let’s see the actual operation effect

We write a click event

You can clearly see our effect. The handle of the form has been obtained in the text box 110BA8

I encapsulated some commonly used API links and put them on the top

Next, implement the analog keyboard to send A

        // Import Windows API function for sending keyboard messages
        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
        private void button7_Click(object sender, EventArgs e)
        {
            //Simulate pressing A
            keybd_event(0X65, 0, 0, 0);
           
            //Release button A
            keybd_event(0X65, 0, 2, 0);
        }

Listed below are some of the more common messages, you can replace them yourself

This incident is not related to this aspect. Many third-party forms have drop-down list boxes, text boxes, radio boxes, etc. A series of basics can be realized by mouse clicks and keyboard transmission

Refer to the Internet for these common messages How does the C# form simulate keyboard keys (combination keys) to generate events through the keybd_event() function- Xiaoxiaoxie- Blog Garden (cnblogs.com)

#region bVk parameter constant definition

        public const byte vbKeyLButton = 0x1; // left mouse button
        public const byte vbKeyRButton = 0x2; // Right mouse button
        public const byte vbKeyCancel = 0x3; // CANCEL key
        public const byte vbKeyMButton = 0x4; // middle mouse button
        public const byte vbKeyBack = 0x8; // BACKSPACE key
        public const byte vbKeyTab = 0x9; // TAB key
        public const byte vbKeyClear = 0xC; // CLEAR key
        public const byte vbKeyReturn = 0xD; // ENTER key
        public const byte vbKeyShift = 0x10; // SHIFT key
        public const byte vbKeyControl = 0x11; // CTRL key
        public const byte vbKeyAlt = 18; // Alt key (keycode 18)
        public const byte vbKeyMenu = 0x12; // MENU key
        public const byte vbKeyPause = 0x13; // PAUSE key
        public const byte vbKeyCapital = 0x14; // CAPS LOCK key
        public const byte vbKeyEscape = 0x1B; // ESC key
        public const byte vbKeySpace = 0x20; // SPACEBAR key
        public const byte vbKeyPageUp = 0x21; // PAGE UP key
        public const byte vbKeyEnd = 0x23; // End key
        public const byte vbKeyHome = 0x24; // HOME key
        public const byte vbKeyLeft = 0x25; // LEFT ARROW key
        public const byte vbKeyUp = 0x26; // UP ARROW key
        public const byte vbKeyRight = 0x27; // RIGHT ARROW key
        public const byte vbKeyDown = 0x28; // DOWN ARROW key
        public const byte vbKeySelect = 0x29; // Select key
        public const byte vbKeyPrint = 0x2A; // PRINT SCREEN key
        public const byte vbKeyExecute = 0x2B; // EXECUTE key
        public const byte vbKeySnapshot = 0x2C; // SNAPSHOT key
        public const byte vbKeyDelete = 0x2E; // Delete key
        public const byte vbKeyHelp = 0x2F; // HELP key
        public const byte vbKeyNumlock = 0x90; // NUM LOCK key

        // Commonly used keys letter keys A to Z
        public const byte vbKeyA = 65;
        public const byte vbKeyB = 66;
        public const byte vbKeyC = 67;
        public const byte vbKeyD = 68;
        public const byte vbKeyE = 69;
        public const byte vbKeyF = 70;
        public const byte vbKeyG = 71;
        public const byte vbKeyH = 72;
        public const byte vbKeyI = 73;
        public const byte vbKeyJ = 74;
        public const byte vbKeyK = 75;
        public const byte vbKeyL = 76;
        public const byte vbKeyM = 77;
        public const byte vbKeyN = 78;
        public const byte vbKeyO = 79 ;
        public const byte vbKeyP = 80 ;
        public const byte vbKeyQ = 81 ;
        public const byte vbKeyR = 82 ;
        public const byte vbKeyS = 83 ;
        public const byte vbKeyT = 84 ;
        public const byte vbKeyU = 85 ;
        public const byte vbKeyV = 86 ;
        public const byte vbKeyW = 87 ;
        public const byte vbKeyX = 88 ;
        public const byte vbKeyY = 89 ;
        public const byte vbKeyZ = 90 ;

        // Numeric keypad 0 to 9
        public const byte vbKey0 = 48 ; // key 0
        public const byte vbKey1 = 49 ; // 1 key
        public const byte vbKey2 = 50 ; // 2 keys
        public const byte vbKey3 = 51 ; // 3 keys
        public const byte vbKey4 = 52 ; // 4 keys
        public const byte vbKey5 = 53 ; // 5 keys
        public const byte vbKey6 = 54 ; // 6 keys
        public const byte vbKey7 = 55 ; // 7 keys
        public const byte vbKey8 = 56 ; // 8 keys
        public const byte vbKey9 = 57 ; // 9 keys


        public const byte vbKeyNumpad0 = 0x60 ; //0 key
        public const byte vbKeyNumpad1 = 0x61 ; //1 key
        public const byte vbKeyNumpad2 = 0x62 ; //2 keys
        public const byte vbKeyNumpad3 = 0x63 ; //3 keys
        public const byte vbKeyNumpad4 = 0x64 ; //4 keys
        public const byte vbKeyNumpad5 = 0x65 ; //5 keys
        public const byte vbKeyNumpad6 = 0x66 ; //6 keys
        public const byte vbKeyNumpad7 = 0x67 ; //7 keys
        public const byte vbKeyNumpad8 = 0x68 ; //8 keys
        public const byte vbKeyNumpad9 = 0x69 ; //9 keys
        public const byte vbKeyMultiply = 0x6A ; // MULTIPLICATIONSIGN(*) key
        public const byte vbKeyAdd = 0x6B ; // PLUS SIGN( + ) key
        public const byte vbKeySeparator = 0x6C ; // ENTER key
        public const byte vbKeySubtract = 0x6D ; // MINUS SIGN(-) key
        public const byte vbKeyDecimal = 0x6E ; // DECIMAL POINT(.) key
        public const byte vbKeyDivide = 0x6F ; // DIVISION SIGN(/) key


        //F1 to F12 keys
        public const byte vbKeyF1 = 0x70 ; //F1 key
        public const byte vbKeyF2 = 0x71 ; //F2 key
        public const byte vbKeyF3 = 0x72 ; //F3 key
        public const byte vbKeyF4 = 0x73 ; //F4 key
        public const byte vbKeyF5 = 0x74 ; //F5 key
        public const byte vbKeyF6 = 0x75 ; //F6 key
        public const byte vbKeyF7 = 0x76 ; //F7 key
        public const byte vbKeyF8 = 0x77 ; //F8 key
        public const byte vbKeyF9 = 0x78 ; //F9 key
        public const byte vbKeyF10 = 0x79 ; //F10 key
        public const byte vbKeyF11 = 0x7A ; //F11 key
        public const byte vbKeyF12 = 0x7B ; //F12 key

        #endregion