Original reprint from http://www.51testing.com /? Uid-175761-action-viewspace-itemid-227073
I. CallWindowsAPI.
C # use the following method to call a Windows API:
1. Introduce the namespace: using system. runtime. interopservices;
2. reference the method to be used. Format: [dllimport ("DLL file")] method declaration;
[Dllimport ("user32.dll")] Private Static extern bool showwindow (intptr hwnd, int ncmdshow );
[Dllimport ("user32.dll")] Private Static extern bool setforegroundwindow (intptr hwnd );
[Dllimport ("user32.dll")] Private Static extern intptr findwindow (string lpclassname, string lpwindowname );
[Dllimport ("user32.dll")] Private Static extern int sendmessage (intptr hwnd, int MSG, int wparam, int lparam );
[Dllimport ("user32.dll")] Private Static extern bool setcursorpos (int x, int y );
[Dllimport ("user32.dll")] Private Static extern void mouse_event (INT dwflags, int dx, int dy, int dwdata, int dwextrainfo );
[Dllimport ("user32.dll")] Private Static extern void keybd_event (byte bvk, byte bscan, uint dwflags, uint dwextrainfo );
[Dllimport ("user32.dll")] Private Static extern bool setwindowpos (intptr hwnd, intptr hwndlnsertafter, int X, int y, int CX, int cy, uint flags );
// Showwindow Parameters
Private const int sw_shownormal = 1;
Private const int sw_restore = 9;
Private const int sw_shownoactivate = 4;
// Sendmessage Parameters
Private const int wm_keydown = 0x100;
Private const int wm_keyup = 0x101;
Private const int wm_syschar = 0x106;
Private const int wm_syskeyup = 0x105;
Private const int wm_syskeydown = 0x104;
Private const int wm_char = 0x102;
2. Find the target window
1) obtain the Handle Based on the window title
Intptr myintptr = findwindow (null, "window name"); // null indicates the class name, which can be obtained using spy ++ or empty.
Showwindow (myintptr, sw_restore); // restore the window
Setforegroundwindow (myintptr); // if no showwindow exists, this method cannot set the minimized window.
2) traverse all windows to get the handle
1. Define the delegate method callback and enumerate the window API (enumwindows). Get the window name API (getwindowtextw) and get the window class name API (getclassnamew)
Public Delegate bool callback (INT hwnd, int lparam );
[Dllimport ("USER32")] public static extern int enumwindows (callback X, int y );
[Dllimport ("user32.dll")] Private Static extern int getwindowtextw (intptr hwnd, [exploralas (unmanagedtype. lpwstr)] stringbuilder lpstring, int nmaxcount );
[Dllimport ("user32.dll")] Private Static extern int getclassnamew (intptr hwnd, [exploralas (unmanagedtype. lpwstr)] stringbuilder lpstring, int nmaxcount );
2. Call enumwindows to traverse the window
Callback mycallback = new callback (recall );
Enumwindows (mycallback, 0 );
3. Callback method recall
Public bool recall (INT hwnd, int lparam)
{
Stringbuilder sb = new stringbuilder (256 );
Intptr PW = new intptr (hwnd );
Getwindowtextw (PW, Sb, SB. capacity); // obtain the window name and save it in strname.
String strname = sb. tostring ();
Getclassnamew (PW, Sb, SB. capacity); // obtain the window class name and save it in strclass.
String strclass = sb. tostring ();
If (strname. indexof ("window name keyword")> = 0 & strclass. indexof ("Class Name keyword")> = 0)
{
Return false; // return false stop enumwindows Traversal
}
Else
{
Return true; // return true to continue enumwindows Traversal
}
}
3) open the window to get the handle
1. Define setactivewindow and setforegroundwindow)
[Dllimport ("user32.dll")] Static extern intptr setactivewindow (intptr hwnd );
[Dllimport ("user32.dll")] [Return: financialas (unmanagedtype. bool)] Static extern bool setforegroundwindow (intptr hwnd );
2 open a window
Process proc = process. Start (@ "Target Program path ");
Setactivewindow (Proc. main1_whandle );
Setforegroundwindow (Proc. main1_whandle );
3. input data to the specified window
1. send data to the window using the sendmessage API
Inputstr (myintptr, _ gameid); // enter the game ID
Sendmessage (myintptr, wm_syskeydown, 0x09, 0); // enter the tab (0x09)
Sendmessage (myintptr, wm_syskeyup, 0x09, 0 );
Inputstr (myintptr, _ gamepass); // enter the game Password
Sendmessage (myintptr, wm_syskeydown, 0x0d, 0); // enter (0x0d)
Sendmessage (myintptr, wm_syskeyup, 0x0d, 0 );
/// <Summary>
/// Send a string
/// </Summary>
/// <Param name = "myintptr"> window handle </param>
/// <Param name = "input"> string </param>
Public void inputstr (intptr myintptr, string input)
{
Byte [] CH = (asciiencoding. ASCII. getbytes (input ));
For (INT I = 0; I <Ch. length; I ++)
{
Sendmessage (PW, wm_char, CH, 0 );
}
}
2. Use the mouse and keyboard to simulate sending data to the window
Setwindowpos (PW, (intptr) (-1), 0, 0, 0, 0, 0x0040 | 0x0001); // set the window position
Setcursorpos (476,177); // sets the mouse position
Mouse_event (0x0002, 0, 0, 0, 0); // simulate the mouse operation
Mouse_event (0x0004, 0, 0, 0, 0); // simulate the mouse Release Operation
Sendkeys. Send (_ gameid); // enter the game ID on the simulated keyboard
Sendkeys. Send ("{tab}"); // enter a tab on the simulated keyboard.
Sendkeys. Send (_ gamepass); // enter the game password on the simulated keyboard
Sendkeys. Send ("{enter}"); // enter
In addition, the keybd_event method is also mentioned above. Its usage is similar to that of the mouse_event method, and its function is the same as that of sendkeys. Send.