C# VS2005用API学习小例子
1.判断当前系统活动窗口是否是指定的窗口
//找到系统当前活动的窗口的句柄
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
//根据任务栏应用程序显示的名称找相应窗口的句柄
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//获取指定句柄窗口的标题
[DllImport("User32.dll")]
static extern int GetWindowText(IntPtr handle, StringBuilder text, int MaxLen);
IntPtr findPtr = FindWindow(null, "AION Client");//AION Client为游戏在任务栏的名称
StringBuilder text1 = new StringBuilder(50);//按要求找到的进程的标题
StringBuilder text2 = new StringBuilder(50);//当前活动进程的标题
GetWindowText(GetForegroundWindow(), text1, 50);
GetWindowText(findPtr, text2, 50);
if(text1.Equals(text2))
{
MessageBox.Show("当前活动窗口是指定窗口");
}
else
{
MessageBox.Show("当前活动窗口不是指定窗口");
}
2.设置指定窗口选中(激活)
private const int SW_RESTORE = 9;
[DllImport("user32.dll ")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
IntPtr findPtr = FindWindow(null, "AION Client");//AION Client为游戏在任务栏的名称
if (findPtr.ToInt32() != 0)
{
ShowWindow(findPtr, SW_RESTORE); //将窗口还原,如果不用此方法,缩小的窗口不能激活
SetForegroundWindow(findPtr);//将指定的窗口选中(激活)
}
3.向指定的窗口的文本框发送字符和按键
private const uint SWP_SHOWWINDOW = 0x0040;
private const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
private const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
//设置鼠标位置
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);
//设置窗口位置
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
//模拟鼠标
[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
//设置窗口位置和大小
SetWindowPos(findPtr, (IntPtr)(-1), 150, 100, 950, 750, SWP_SHOWWINDOW);
SetCursorPos(590, 445); //设置鼠标位置
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
System.Threading.Thread.Sleep(800);
SendKeys.Send(gameId); //模拟键盘输入游戏ID
SendKeys.Send("{TAB}"); //模拟键盘输入TAB
SendKeys.Send(gamePwd); //模拟键盘输入游戏密码
SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
4.枚举指定窗口中的所有控件,有的游戏不能枚举,像魔兽等等,因为界面是画出来的,没有句柄概念
public delegate bool EnumChildWindowsProc(IntPtr hwnd, uint lParam);
[DllImport("user32.dll", EntryPoint = "EnumChildWindows")]
public static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildWindowsProc lpEnumFunc, int lParam);
[DllImport("user32.dll", EntryPoint = "GetClassName")]
public static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount);
private void btnOk_Click(object sender, EventArgs e)
{
IntPtr nowPtr = FindWindow(null, "QQ游戏");
EnumChildWindowsProc myEnumChild = new EnumChildWindowsProc(EumWinChiPro);
try
{
EnumChildWindows(nowPtr, myEnumChild, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\r\n" + ex.Source + "\r\n\r\n" + ex.StackTrace.ToString());
}
lblMsg.Text = count.ToString();//显示指定窗口上有多少子控件
}
public static int count;
public static bool EumWinChiPro(IntPtr hWnd,uint lParam)
{
StringBuilder s = new StringBuilder(50); //s即为控件的类名称
GetClassName(hWnd, s,50);
count++;
return true;
}
5. 获取指定窗口句柄相对屏幕中的位置
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private static extern bool ClientToScreen(IntPtr hwnd, ref Point lpPoint);
IntPtr findPtr = FindWindow(null, "QQ游戏");
Point p = new Point (0);
ClientToScreen(findPtr, ref p);
MessageBox.Show(p.X+","+p.Y);
6.判断指定窗口是否是最大化
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool IsZoomed(IntPtr hWnd);
IntPtr findPtr = FindWindow(null, "QQ游戏");
if (IsZoomed(findPtr))
{
MessageBox.Show("最大化");
}
else
{
MessageBox.Show("最小化");
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/slyzly/archive/2010/03/05/5348825.aspx