一、調用Windows API。
C#下調用Windows API方法如下:
1、引入命名空間:using System.Runtime.InteropServices;
2、引用需要使用的方法,格式:[DllImport("DLL文件")]方法的聲明;
[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參數
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 WM_CHAR = 0X102;
二、找到目標窗口
1)、根據窗口的標題得到句柄
IntPtr myIntPtr = FindWindow(null,"窗口名"); //null為類名,可以用Spy++得到,也可以為空
ShowWindow(myIntPtr, SW_RESTORE); //將窗口還原
SetForegroundWindow(myIntPtr); //如果沒有ShowWindow,此方法不能設置最小化的窗口
2)、遍歷所有窗口得到句柄
1 定義委托方法CallBack,枚舉窗口API(EnumWindows),得到窗口名API(GetWindowTextW)和得到窗口類名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, [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); //得到窗口名並保存在strName中
string strName = sb.ToString();
GetClassNameW(PW,sb,sb.Capacity); //得到窗口類名並保存在strClass中
string strClass = sb.ToString();
if (strName.IndexOf("窗口名關鍵字") >= 0 && strClass.IndexOf("類名關鍵字") >= 0)
{
return false; //返回false中止EnumWindows遍歷
}
else
{
return true; //返回true繼續EnumWindows遍歷
}
}
3)、打開窗口得到句柄
1 定義設置活動窗口API(SetActiveWindow),設置前台窗口API(SetForegroundWindow)
[DllImport("user32.dll")]static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]static extern bool SetForegroundWindow(IntPtr hWnd);
2 打開窗口
Process proc = Process.Start(@"目標程序路徑");
SetActiveWindow(proc.MainWindowHandle);
SetForegroundWindow(proc.MainWindowHandle);
三、向指定的窗口輸入數據
1 利用發送消息API(SendMessage)向窗口發送數據
InputStr(myIntPtr, _GameID); //輸入游戲ID
SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X09, 0); //輸入TAB(0x09)
SendMessage(myIntPtr, WM_SYSKEYUP, 0X09, 0);
InputStr(myIntPtr, _GamePass); //輸入游戲密碼
SendMessage(myIntPtr, WM_SYSKEYDOWN, 0X0D, 0); //輸入ENTER(0x0d)
SendMessage(myIntPtr, WM_SYSKEYUP, 0X0D, 0);
/// <summary>
/// 發送一個字符串
/// </summary>
/// <param name="myIntPtr">窗口句柄</param>
/// <param name="Input">字符串</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 利用鼠標和鍵盤模擬向窗口發送數據
SetWindowPos(PW, (IntPtr)(-1), 0, 0, 0, 0, 0x0040 | 0x0001); //設置窗口位置
SetCursorPos(476, 177); //設置鼠標位置
mouse_event(0x0002, 0, 0, 0, 0); //模擬鼠標按下操作
mouse_event(0x0004, 0, 0, 0, 0); //模擬鼠標放開操作
SendKeys.Send(_GameID); //模擬鍵盤輸入游戲ID
SendKeys.Send("{TAB}"); //模擬鍵盤輸入TAB
SendKeys.Send(_GamePass); //模擬鍵盤輸入游戲密碼
SendKeys.Send("{ENTER}"); //模擬鍵盤輸入ENTER
另:上面還提到了keybd_event方法,用法和mouse_event方法類似,作用和SendKeys.Send一樣。
轉自:http://www.cnblogs.com/zeroone/p/3713051.html