- using System;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Text;
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- Console.WriteLine("/nStarting test.../n");
- Console.WriteLine("Launching Form");
- //待测应用程序路径
- string path = @"../../../AUT/bin/Debug/AUT.exe";
- //启动应用程序
- Process p = Process.Start(path);
- Console.WriteLine("/nFinding main window handle");
- IntPtr mwh = FindMainWindowHandle("Application Under Test", 100, 25);
- Console.WriteLine("Main window handle = " + mwh);
- Console.WriteLine("/nFinding handles to txtChoose, btnCompare");
- IntPtr tb = FindWindowByIndex(mwh, 1);
- IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "Compare");
- if (tb == IntPtr.Zero || cb == IntPtr.Zero || butt == IntPtr.Zero || lb == IntPtr.Zero)
- throw new Exception("Unable to find all controls");
- else
- Console.WriteLine("All control handles found");
- Console.WriteLine("/nTyping 'rock' to txtChoose");
- SendChars(tb, "rock");
- ClickOn(butt);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- /// <summary>
- /// 获得应用程序主窗体句柄
- /// </summary>
- /// <param name="caption">窗体的标题</param>
- /// <param name="delay">每次查找的延时</param>
- /// <param name="maxTries">限制循环重复的最大次数</param>
- /// <returns></returns>
- static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries)
- {
- return FindTopLevelWindow(caption, delay, maxTries);
- }
- static IntPtr FindTopLevelWindow(string caption, int delay, int maxTries)
- {
- IntPtr mwh = IntPtr.Zero;
- bool formFound = false;
- int attempts = 0;
- do
- {
- mwh = FindWindow(null, caption);
- if (mwh == IntPtr.Zero)
- {
- Console.WriteLine("Form not yet found");
- Thread.Sleep(delay);
- ++attempts;
- }
- else
- {
- Console.WriteLine("Form has been found");
- formFound = true;
- }
- } while (!formFound && attempts < maxTries);
- if (mwh != IntPtr.Zero)
- return mwh;
- else
- throw new Exception("Could not find Main Window");
- }
- /// <summary>
- /// 获得控件的句柄
- /// </summary>
- /// <param name="hwndParent">目标控件的父窗体的句柄</param>
- /// <param name="index">目标控件的索引值</param>
- /// <returns>目标控件的句柄</returns>
- static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
- {
- if (index == 0)
- return hwndParent;
- else
- {
- int ct = 0;
- IntPtr result = IntPtr.Zero;
- do
- {
- result = FindWindowEx(hwndParent, result, null, null);
- if (result != IntPtr.Zero)
- ++ct;
- } while (ct < index && result != IntPtr.Zero);
- return result;
- }
- }
- /// <summary>
- /// 自动实现鼠标单击一个控件
- /// </summary>
- /// <param name="hControl">要单击的目标控件</param>
- static void ClickOn(IntPtr hControl)
- {
- //WM_LBUTTONDOWN 按下鼠标左键
- uint WM_LBUTTONDOWN = 0x0201;
- //WM_LBUTTONUP 鼠标左键抬起
- uint WM_LBUTTONUP = 0x0202;
- //wParam 当鼠标按键被单击时,其他几个功能键是否被按下
- //lParam 指定鼠标单击在目标控件的什么地方,lParam为0表示单击的控件的左上角(低字节是x坐标,高字节是y坐标)
- PostMessage1(hControl, WM_LBUTTONDOWN, 0, 0);
- PostMessage1(hControl, WM_LBUTTONUP, 0, 0);
- }
- /// <summary>
- /// 发送字符给基于文本的控件
- /// </summary>
- /// <param name="hControl">目标控件</param>
- /// <param name="c">要发送的字符</param>
- static void SendChar(IntPtr hControl, char c)
- {
- //当按键按下时,WM_CHAR消息会发送给拥有键盘焦点的控件
- uint WM_CHAR = 0x0102;
- //此时wParam参数指定的是被按下按键的字符代码
- //此时lParam参数指定的是不同的按键状态掩码(重复次数、扫描码、扩展见标识、context code、前一按键状态以及状态转换标识)
- SendMessage1(hControl, WM_CHAR, c, 0);
- }
- /// <summary>
- /// 发送字符串给基于文本的控件
- /// </summary>
- /// <param name="hControl">目标控件</param>
- /// <param name="s">要发送的字符串</param>
- static void SendChars(IntPtr hControl, string s)
- {
- foreach (char c in s)
- {
- SendChar(hControl, c);
- }
- }
- //user32.dll指定要使用的非受控函数所在的DLL文件
- [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
- //使用Win32 API函数 FindWindow得到待测主窗体的句柄
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
- //使用Win32 API函数 FindWindowEx得到有名字的控件或者窗体的句柄
- static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
- //使用Win32 API函数 SendMessage 向应用程序发送消息,Windows消息处理完毕后返回
- static extern void SendMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
- }