关于控制一个外部窗口的相关问题

来源:互联网 发布:daas 数据即服务 编辑:程序博客网 时间:2024/06/16 13:37
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. using System.Text;
  6. class Program
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         try
  11.         {
  12.             Console.WriteLine("/nStarting test.../n");
  13.             Console.WriteLine("Launching Form");
  14.             //待测应用程序路径
  15.             string path = @"../../../AUT/bin/Debug/AUT.exe";
  16.             //启动应用程序
  17.             Process p = Process.Start(path);
  18.             Console.WriteLine("/nFinding main window handle");
  19.             IntPtr mwh = FindMainWindowHandle("Application Under Test", 100, 25);
  20.             Console.WriteLine("Main window handle = " + mwh);
  21.             Console.WriteLine("/nFinding handles to txtChoose, btnCompare");
  22.             IntPtr tb = FindWindowByIndex(mwh, 1);
  23.             IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null"Compare");
  24.             if (tb == IntPtr.Zero || cb == IntPtr.Zero || butt == IntPtr.Zero || lb == IntPtr.Zero)
  25.                 throw new Exception("Unable to find all controls");
  26.             else
  27.                 Console.WriteLine("All control handles found");
  28.             Console.WriteLine("/nTyping 'rock' to txtChoose");
  29.             SendChars(tb, "rock");
  30.             ClickOn(butt);
  31.         }
  32.         catch (Exception ex)
  33.         {
  34.             Console.WriteLine(ex.Message);
  35.         }
  36.     }
  37.     /// <summary>
  38.     /// 获得应用程序主窗体句柄
  39.     /// </summary>
  40.     /// <param name="caption">窗体的标题</param>
  41.     /// <param name="delay">每次查找的延时</param>
  42.     /// <param name="maxTries">限制循环重复的最大次数</param>
  43.     /// <returns></returns>
  44.     static IntPtr FindMainWindowHandle(string caption, int delay, int maxTries)
  45.     {
  46.         return FindTopLevelWindow(caption, delay, maxTries);
  47.     }
  48.     static IntPtr FindTopLevelWindow(string caption, int delay, int maxTries)
  49.     {
  50.         IntPtr mwh = IntPtr.Zero;
  51.         bool formFound = false;
  52.         int attempts = 0;
  53.         do
  54.         {
  55.             mwh = FindWindow(null, caption);
  56.             if (mwh == IntPtr.Zero)
  57.             {
  58.                 Console.WriteLine("Form not yet found");
  59.                 Thread.Sleep(delay);
  60.                 ++attempts;
  61.             }
  62.             else
  63.             {
  64.                 Console.WriteLine("Form has been found");
  65.                 formFound = true;
  66.             }
  67.         } while (!formFound && attempts < maxTries);
  68.         if (mwh != IntPtr.Zero)
  69.             return mwh;
  70.         else
  71.             throw new Exception("Could not find Main Window");
  72.     }
  73.     /// <summary>
  74.     /// 获得控件的句柄
  75.     /// </summary>
  76.     /// <param name="hwndParent">目标控件的父窗体的句柄</param>
  77.     /// <param name="index">目标控件的索引值</param>
  78.     /// <returns>目标控件的句柄</returns>
  79.     static IntPtr FindWindowByIndex(IntPtr hwndParent, int index)
  80.     {
  81.         if (index == 0)
  82.             return hwndParent;
  83.         else
  84.         {
  85.             int ct = 0;
  86.             IntPtr result = IntPtr.Zero;
  87.             do
  88.             {
  89.                 result = FindWindowEx(hwndParent, result, nullnull);
  90.                 if (result != IntPtr.Zero)
  91.                     ++ct;
  92.             } while (ct < index && result != IntPtr.Zero);
  93.             return result;
  94.         }
  95.     }
  96.     /// <summary>
  97.     /// 自动实现鼠标单击一个控件
  98.     /// </summary>
  99.     /// <param name="hControl">要单击的目标控件</param>
  100.     static void ClickOn(IntPtr hControl)
  101.     {
  102.         //WM_LBUTTONDOWN 按下鼠标左键
  103.         uint WM_LBUTTONDOWN = 0x0201;
  104.         //WM_LBUTTONUP 鼠标左键抬起
  105.         uint WM_LBUTTONUP = 0x0202;
  106.         //wParam 当鼠标按键被单击时,其他几个功能键是否被按下
  107.         //lParam 指定鼠标单击在目标控件的什么地方,lParam为0表示单击的控件的左上角(低字节是x坐标,高字节是y坐标)
  108.         PostMessage1(hControl, WM_LBUTTONDOWN, 0, 0);
  109.         PostMessage1(hControl, WM_LBUTTONUP, 0, 0);
  110.     }
  111.     /// <summary>
  112.     /// 发送字符给基于文本的控件
  113.     /// </summary>
  114.     /// <param name="hControl">目标控件</param>
  115.     /// <param name="c">要发送的字符</param>
  116.     static void SendChar(IntPtr hControl, char c)
  117.     {
  118.         //当按键按下时,WM_CHAR消息会发送给拥有键盘焦点的控件
  119.         uint WM_CHAR = 0x0102;
  120.         //此时wParam参数指定的是被按下按键的字符代码            
  121.         //此时lParam参数指定的是不同的按键状态掩码(重复次数、扫描码、扩展见标识、context code、前一按键状态以及状态转换标识)
  122.         SendMessage1(hControl, WM_CHAR, c, 0);
  123.     }
  124.     /// <summary>
  125.     /// 发送字符串给基于文本的控件
  126.     /// </summary>
  127.     /// <param name="hControl">目标控件</param>
  128.     /// <param name="s">要发送的字符串</param>
  129.     static void SendChars(IntPtr hControl, string s)
  130.     {
  131.         foreach (char c in s)
  132.         {
  133.             SendChar(hControl, c);
  134.         }
  135.     }
  136.     //user32.dll指定要使用的非受控函数所在的DLL文件
  137.     [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
  138.     //使用Win32 API函数 FindWindow得到待测主窗体的句柄
  139.     static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  140.     [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
  141.     //使用Win32 API函数 FindWindowEx得到有名字的控件或者窗体的句柄
  142.     static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  143.     [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
  144.     //使用Win32 API函数 SendMessage 向应用程序发送消息,Windows消息处理完毕后返回
  145.     static extern void SendMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
  146. }
原创粉丝点击