//internal Window FindChildWindow(string v1) //{ // int iCount = 0; // while (iCount < 100) // { // WinTree winTree = GetWindowTree(); // IntPtr hWnd = winTree.Find(v1); // if (hWnd != IntPtr.Zero) // { // Window findWindow = (Window)Activator.CreateInstance(typeof(Window), hWnd, v1); // Log.INFO(string.Format("Finded chid Window:[{0},{1}], in process:[{2}]", findWindow._name, findWindow._hWnd.ToString("X8"), _process.Id)); // return findWindow; // } // iCount++; // Thread.Sleep(200); // } // throw new ArgumentException(string.Format("can not find child window:[{0}], in process:[{1}]", v1, _process.Id)); //} internal void FindChildWindow(string v1, Action <Window> actionNomarl, string v2, Action <Window> actionException) { int iCount = 0; while (iCount < 100) { WinTree winTree = GetWindowTree(); IntPtr hWnd = winTree.Find(v2); if (hWnd != IntPtr.Zero && WinAPI.IsWindowEnabled(hWnd)) { Window findWindow = (Window)Activator.CreateInstance(typeof(Window), hWnd, v2); Log.INFO(string.Format("Finded chid Window:[{0},{1}], in process:[{2}]", findWindow._name, findWindow._hWnd.ToString("X8"), _process.Id)); actionException(findWindow); return; } hWnd = winTree.Find(v1); if (hWnd != IntPtr.Zero && WinAPI.IsWindowEnabled(hWnd)) { Window findWindow = (Window)Activator.CreateInstance(typeof(Window), hWnd, v1); Log.INFO(string.Format("Finded chid Window:[{0},{1}], in process:[{2}]", findWindow._name, findWindow._hWnd.ToString("X8"), _process.Id)); actionNomarl(findWindow); return; } iCount++; Thread.Sleep(200); } throw new ArgumentException(string.Format("can not find child window:[{0}], in process:[{1}]", v1 + "," + v2, _process.Id)); }
public Window FindWindow(Selector selector, int waitsecond = 90) { try { Window findWindow = null; WaitUntil((String findName) => { WinTree tree = GetWindowTree(); IntPtr exceptHwnd = tree.Find((IntPtr currhwnd) => { string str = WinAPI.GetWindowText(currhwnd); if (str.ToLower().Contains("fail") || str.ToLower().Contains("warn") || str.ToLower().Contains("not able")) { if (selector.IsTrue(currhwnd)) { return(false); } return(true); } return(false); }); if (exceptHwnd != IntPtr.Zero) { throw new Exception("get error report: " + WinAPI.GetWindowText(exceptHwnd)); } IntPtr hwnd = tree.Find((IntPtr currhwnd) => { return(selector.IsTrue(currhwnd)); }); if (hwnd == IntPtr.Zero) { return(false); } findWindow = (Window)Activator.CreateInstance(typeof(Window), hwnd, WinAPI.GetWindowText(hwnd)); return(true); }, "", waitsecond); Log.INFO(string.Format("Finded Winow:[{0},{1}], in PID:[{2}]", findWindow._name, findWindow.hwnd.ToString("X8"), _process.Id.ToString("X8"))); return(findWindow); } catch (TimeoutException e) { throw new ArgumentException(string.Format(" can not find window:[{0}] in PID:[{1}]", selector.desc(), _process.Id.ToString("X8"))); } }
public WinTree GetWindowTree() { List <IntPtr> listWinHwnd = new List <IntPtr>(); WinAPI.EnumWindows(new WinAPI.EnumWindowsProc((hWnd, lParam) => { uint pid = 0; WinAPI.GetWindowThreadProcessId(hWnd, out pid); if (pid == _process.Id) { listWinHwnd.Add(hWnd); } return(true); }), (IntPtr)0); WinTree root = new WinTree(IntPtr.Zero); foreach (IntPtr hWnd in listWinHwnd) { root.subWin.Add(new WinTree(hWnd)); WinAPI.EnumChildWindows(hWnd, new WinAPI.EnumWindowsProc((hChildWnd, lParam) => { IntPtr parenthWind = WinAPI.GetParent(hChildWnd); root.Find(parenthWind).subWin.Add(new WinTree(hChildWnd)); return(true); }), 0); } return(root); }