关于控制一个外部窗口的相关问题
来源:互联网 发布:daas 数据即服务 编辑:程序博客网 时间:2024/06/16 13:37
- 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);
- }
- }
-
-
-
-
-
-
-
- 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");
- }
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
- static void ClickOn(IntPtr hControl)
- {
-
- uint WM_LBUTTONDOWN = 0x0201;
-
- uint WM_LBUTTONUP = 0x0202;
-
-
- PostMessage1(hControl, WM_LBUTTONDOWN, 0, 0);
- PostMessage1(hControl, WM_LBUTTONUP, 0, 0);
- }
-
-
-
-
-
- static void SendChar(IntPtr hControl, char c)
- {
-
- uint WM_CHAR = 0x0102;
-
-
- SendMessage1(hControl, WM_CHAR, c, 0);
- }
-
-
-
-
-
- static void SendChars(IntPtr hControl, string s)
- {
- foreach (char c in s)
- {
- SendChar(hControl, c);
- }
- }
-
- [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
-
- static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
-
- static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
- [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
-
- static extern void SendMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
- }