Good at calling Windows API

Source: Internet
Author: User

Some time ago saw someone else do an automatic fill in the information and click the login program, it is very interesting.

is actually called in the program Windows API, then how to call, the following is a simple introduction.

Write simple rough, do not like light spray.

0, first introduce the namespace System.Runtime.InteropServices to import the Windows DLL.

1. The following is the function prototype:

1.1, this is the method of simulating mouse press

[DllImport ("user32.dll", EntryPoint ="mouse_event")]         Public Static extern voidMouse_event (intDwFlags,intDX,intDy,intCbuttons,intdwextrainfo);


1.2, this is the analog button press the method

[DllImport ("user32.dll", EntryPoint ="keybd_event")]         Public Static extern voidkeybd_event (byteBVK,//Virtual Key Code                byteBscan,//Hardware Scan code for this key (typically 0)DWORD DwFlags,//a set of flags for each aspect of a function operationDWORD Dwextralnfo//additional 32-bit values associated with keystrokes        );

      
PS: There are three kinds of values for the third parameter:
        • 0: Press;
        • 1: Extension key;
        • 2: Bounce up.

3. Related examples

        Const intMouseeventf_move =0x0001;//Move Mouse        Const intMouseeventf_leftdown =0x0002;//simulate the left mouse button press        Const intMouseeventf_leftup =0x0004;//simulate the left mouse button to lift up        Const intMouseeventf_rightdown =0x0008;//simulate the right mouse button press        Const intMouseeventf_rightup =0x0010;//Simulate right mouse button lift        Const intMouseeventf_middledown =0x0020;//simulate the middle mouse button press        Const intMouseeventf_middleup =0x0040;//Simulate mouse middle button lift        Const intMouseeventf_absolute =0x8000;//indicate if absolute coordinates are used


       




   PublicForm1 () {InitializeComponent (); intX = -; intY = -; Mouse_event (Mouseeventf_rightdown, X, Y,0,0); Mouse_event (Mouseeventf_rightup, X, Y,0,0); X+=Ten; Y+= $; Mouse_event (Mouseeventf_move, X, Y,0,0); Mouse_event (Mouseeventf_leftdown, X, Y,0,0); Mouse_event (Mouseeventf_leftup, X, Y,0,0); Keybd_event ( $,0,0,0);//aKeybd_event ( the,0,1,0);//bKeybd_event ( -,0,0,0);//Enter        }

4, a very practical example (implementation of the Paste copy function)

Equivalent to pressing Ctrl+ckeybd_event (Convert.ToInt32 (System.Windows.Forms.Keys.ControlKey), 0, 0, 0);  Press Ctrlkeybd_event (Convert.ToInt32 (SYSTEM.WINDOWS.FORMS.KEYS.C), 0, 0, 0); keybd_event (Convert.ToInt32 ( System.Windows.Forms.Keys.ControlKey), 0, 0x2, 0);//Bounce up ctrl,******* is important, or CTRL will always be in the press state, the keyboard is failure, my own personal experience. 
Equivalent to pressing Ctrl+vkeybd_event (Convert.ToInt32 (System.Windows.Forms.Keys.ControlKey), 0, 0, 0);  Press Ctrlkeybd_event (Convert.ToInt32 (SYSTEM.WINDOWS.FORMS.KEYS.V), 0, 0, 0); keybd_event (Convert.ToInt32 ( System.Windows.Forms.Keys.ControlKey), 0, 0x2, 0);//Bounce Ctrl

5, in fact, there are many Windows API, here only two, the following are also very common

[DllImport ("user32.dll")]Private Static extern BOOLShowWindow (IntPtr hWnd,intncmdshow); [DllImport ("user32.dll")]Private Static extern BOOLSetForegroundWindow (IntPtr hWnd); [DllImport ("user32.dll")]Private Static externIntPtr FindWindow (stringLpclassname,stringlpwindowname); [DllImport ("user32.dll")]Private Static extern intSendMessage (IntPtr hWnd,intMSG,intWParam,intLParam); [DllImport ("user32.dll")]Private Static extern BOOLSetcursorpos (intXintY);[DllImport ("user32.dll")]Private Static extern BOOLSetWindowPos (IntPtr hWnd, IntPtr Hwndlnsertafter,intXintYintCxintCyUINTFlags);



6, to add something not mentioned

Call the Windows API in C # to send a keystroke message to the specified window

Good at calling Windows API

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.