Called with C # Windows API to send a specified window

Reprinted from the original http://www.51testing.com/?uid-175761-action-viewspace-itemid-227073

a call Windows API.
 Under C # Windows API call is as follows:
 1, the introduction of the namespace: a using System.Runtime.InteropServices;
 2, reference methods require the use of the format: [DllImport ( "DLL file")] declaring the method;
 [DllImport ( "user32. DLL ")] Private static extern BOOL the ShowWindow (IntPtr the hWnd, the nCmdShow int);
 [the DllImport (" User32.dll ")] Private static extern BOOL the SetForegroundWindow (IntPtr the hWnd);
 [the DllImport (" User32.dll ")] Private static extern the FindWindow IntPtr (the lpClassName String, String lpWindowName);
 [the DllImport ( "User32.dll")] Private static extern int the SendMessage (IntPtr the hWnd, int Msg, the wParam int, int the 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参数
 private const int SW_SHOWNORMAL = 1;
 private const int SW_RESTORE = 9;
 private const int SW_SHOWNOACTIVATE = 4;
 //SendMessage参数
 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 the WM_CHAR = 0x102;

Second, find the target window
1), to give the handle according to the title of the window
 IntPtr myIntPtr = FindWindow (null, "window name"); // null class name, can be obtained by Spy ++, also be empty
 ShowWindow (myIntPtr, SW_RESTORE); // restore the window
 SetForegroundWindow (myIntPtr); // if not ShowWindow this method can not set the minimized window
2), obtained through all windows handles
a delegate and the CallBack, the window enumeration API (EnumWindows), to give the name of the window API (GetWindowTextW) to give the window class name and the API (GetClassNameW)
 public BOOL the delegate 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, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
 [DllImport("user32.dll")]private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
2 调用EnumWindows遍历窗口
 CallBack myCallBack = new CallBack(Recall);
 EnumWindows(myCallBack, 0);
3 回调方法Recall
 public bool Recall(int hwnd, int lParam)
 {
  StringBuilder sb = new StringBuilder(256);
  IntPtr PW = new IntPtr(hwnd);

  GetWindowTextW (PW, sb, sb.Capacity) ; // get strName window name and stored in the
  String sb.ToString strName = ();

  GetClassNameW (the PW, SB, sb.Capacity); // get a window class name and stored in strClass the
  String strClass sb.ToString = ();

  IF (strName.IndexOf ( "window name keyword")> = 0 && strClass.IndexOf ( " class name keyword")> = 0)
  {
   return to false; // returns traversing the suspended false EnumWindows
  }
  the else
  {
   return to true; returns true // continue to traverse EnumWindows
  }
 }
3), a handle to open the window to obtain
a definition of the active window is provided API (SetActiveWindow), the foreground window is provided the API (the SetForegroundWindow)
 [the DllImport ( "User32.dll ")] static extern IntPtr SetActiveWindow ( IntPtr hWnd);
 [The DllImport ( "User32.dll")] [return: the MarshalAs (UnmanagedType.Bool)] static extern BOOL the SetForegroundWindow (IntPtr the hWnd);
2 opens a window
 Process proc = Process.Start (@ "target program path");
 SetActiveWindow (proc .MainWindowHandle);
 the SetForegroundWindow (proc.MainWindowHandle);

three, the input data to the window specified
transmission data using the transmission message to a window the API (the SendMessage)
 inputstr (myIntPtr, _GameID); // input game ID
 the SendMessage (myIntPtr, WM_SYSKEYDOWN, 0x09 , 0); // enter the TAB (0x09)
 SendMessage (myIntPtr, WM_SYSKEYUP, 0x09, 0);
 inputstr (myIntPtr, _GamePass); // enter the password of the game
 SendMessage (myIntPtr, WM_SYSKEYDOWN, 0X0D, 0); // input eNTER ( 0x0D)
 the SendMessage (myIntPtr, WM_SYSKEYUP, 0X0D, 0);

 /// <Summary>
 Sending a string ///
 /// </ Summary>
 /// <param name = "myIntPtr"> window handle </ param>
 /// <param name = "the Input"> string </ param>
 public void inputstr (IntPtr myIntPtr, the Input String)
 {
  byte [] = CH (ASCIIEncoding.ASCII.GetBytes (the Input));
  for (int I = 0; I <ch.Length; I ++)
  {
   the SendMessage (the PW, the WM_CHAR, CH, 0 );
  }
 }
2 using the mouse and keyboard to the analog transmit window data
 SetWindowPos (PW, (IntPtr) ( - 1), 0, 0, 0, 0, 0x0040 | 0x0001); // set the window position
 SetCursorPos (476, 177) ; // set the mouse position
 mouse_event (0x0002, 0, 0, 0, 0); // simulate mouse pressing operation
 mouse_event (0x0004, 0, 0, 0, 0); // simulate mouse release operation
 SendKeys.Send (_GameID); // simulated keyboard input game ID
 SendKeys.Send ( "the TAB {}"); // simulated keyboard input the TAB
 SendKeys.Send (_GamePass); // simulated keyboard input game password
 SendKeys.Send ( " {eNTER} "); // simulated keyboard input eNTER

another: keybd_event above mentioned methods also, similar to the usage and mouse_event methods, functions and SendKeys.Send same.

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/10/30/2231523.html

Guess you like

Origin blog.csdn.net/weixin_34060299/article/details/93054134