private bool EnumProc(IntPtr hWnd, IntPtr lParam) { uint Pid; NM.GetWindowThreadProcessId(hWnd, out Pid); if (new IntPtr(Pid) == lParam) { if (NM.IsWindowVisible(hWnd)) { NM.tagRect WindowSize; NM.GetClientRect(hWnd, out WindowSize); if (WindowSize.right > 0) //If the window has 0 width then ignore it { TreeNode ParentNode; GetIdentity(IntPtr.Zero, hWnd); string APEType = GetAPEType(m_Identity); ListOfTopLevelWindows.Add(hWnd, m_Identity.Name); if (m_Identity.TechnologyType == "Windows Native") { ParentNode = WindowTree.Nodes.Add("0:" + hWnd.ToString(), APEType + " (Windows Native) [" + hWnd.ToString() + "]"); } else { ParentNode = WindowTree.Nodes.Add("0:" + hWnd.ToString(), APEType + " " + m_Identity.Name + " [" + hWnd.ToString() + "]"); } AddChildNode(hWnd, ParentNode, hWnd); } } } return(true); }
private bool EnumWindowsToGetToolTips(IntPtr hWnd, IntPtr lParam) { uint pid; NM.GetWindowThreadProcessId(hWnd, out pid); uint currentProcessId = (uint)AUTProcess.Id; if (pid == currentProcessId) { if (NM.IsWindowVisible(hWnd)) { NM.tagRect WindowSize; NM.GetClientRect(hWnd, out WindowSize); if (WindowSize.right > 0) //If the window has 0 width then ignore it { string className = NM.GetClassName(hWnd); if (className.Contains("tooltips_class")) { m_ToolTipWindows.Add(hWnd); } } } } return(true); }
/// <summary> /// Get the forms window state /// </summary> /// <returns>The window state</returns> private string FormWindowState() { if (NM.IsWindow(Identity.Handle)) { if (NM.IsWindowVisible(Identity.Handle)) { // Get the windows current state GUI.m_APE.AddFirstMessageFindByHandle(DataStores.Store0, Identity.ParentHandle, Identity.Handle); GUI.m_APE.AddQueryMessageReflect(DataStores.Store0, DataStores.Store1, "WindowState", MemberTypes.Property); GUI.m_APE.AddQueryMessageReflect(DataStores.Store1, DataStores.Store2, "ToString", MemberTypes.Method); GUI.m_APE.AddRetrieveMessageGetValue(DataStores.Store2); GUI.m_APE.SendMessages(EventSet.APE); GUI.m_APE.WaitForMessages(EventSet.APE); //Get the value(s) returned MUST be done straight after the WaitForMessages call string windowState = GUI.m_APE.GetValueFromMessage(); return(windowState); } else { throw new Exception("Form is not visible"); } } else { throw new Exception("Form does not exist"); } }
private void LocateButton_Click(object sender, EventArgs e) { if (WindowTree.SelectedNode != null) { string[] SplitCharacters = { ":" }; string[] Handles = WindowTree.SelectedNode.Name.Split(SplitCharacters, StringSplitOptions.None); IntPtr ParentHandle = new IntPtr(int.Parse(Handles[0])); IntPtr Handle = new IntPtr(int.Parse(Handles[1])); if (NM.IsWindow(Handle) && NM.IsWindowVisible(Handle)) { if (ParentHandle == IntPtr.Zero) { NM.BringWindowToTop(Handle); } else { NM.BringWindowToTop(ParentHandle); } NM.tagRect area = Display.GetWindowRectangleDIP(Handle); IntPtr hWindowDC = NM.GetDC(IntPtr.Zero); IntPtr hRectanglePen = NM.CreatePen(NM.PenStyle.PS_SOLID, 3, (uint)ColorTranslator.ToWin32(Color.Red)); IntPtr hPrevPen = NM.SelectObject(hWindowDC, hRectanglePen); IntPtr hPrevBrush = NM.SelectObject(hWindowDC, NM.GetStockObject(NM.StockObjects.HOLLOW_BRUSH)); for (int i = 0; i < 3; i++) { NM.Rectangle(hWindowDC, area.left, area.top, area.right, area.bottom); Thread.Sleep(300); ClearHighlight(area); if (i < 2) { Thread.Sleep(300); } } NM.SelectObject(hWindowDC, hPrevPen); NM.SelectObject(hWindowDC, hPrevBrush); NM.ReleaseDC(Handle, hWindowDC); } else { BuildTree(); } } WindowTree.Focus(); }
/// <summary> /// Closes the form by click the 'x' in the top right hand corner of the form /// </summary> public void Close() { if (NM.IsWindow(Identity.Handle)) { if (NM.IsWindowVisible(Identity.Handle)) { if (NM.IsIconic(Identity.Handle)) { throw new Exception("Can not close the window as it is minimised"); } GUI.m_APE.AddFirstMessageGetTitleBarItemRectangle(Identity.Handle, NM.TitleBarStateElement.Close); GUI.m_APE.SendMessages(EventSet.APE); GUI.m_APE.WaitForMessages(EventSet.APE); //Get the value(s) returned MUST be done straight after the WaitForMessages call NM.StateSystem State = (NM.StateSystem)GUI.m_APE.GetValueFromMessage(); int Top = GUI.m_APE.GetValueFromMessage(); int Left = GUI.m_APE.GetValueFromMessage(); int Bottom = GUI.m_APE.GetValueFromMessage(); int Right = GUI.m_APE.GetValueFromMessage(); // Check the close button is actually displayed if (State != NM.StateSystem.STATE_SYSTEM_NORMAL && State != NM.StateSystem.STATE_SYSTEM_PRESSED) { throw new Exception("Can not close the window as the close button is in state '" + State.ToString() + "'"); } NM.tagRect WindowRect; NM.GetWindowRect(Identity.Handle, out WindowRect); int X = Left + ((Right - Left) / 2) - WindowRect.left; int Y = Top + ((Bottom - Top) / 2) - WindowRect.top; Input.Block(Identity.ParentHandle, Identity.Handle); try { GUI.Log("Close the " + Identity.Description, LogItemType.Action); base.SingleClickInternal(X, Y, MouseButton.Left, MouseKeyModifier.None); //Wait for the window to disappear base.WaitForControlToNotBeVisible(); } finally { Input.Unblock(); } } } }
public static void ClickCommon(IntPtr Parent, IntPtr Handle, int X, int Y) { if (!NM.IsWindowVisible(Handle)) { throw new Exception("Window is not visible"); } if (!NM.IsWindowEnabled(Handle)) { throw new Exception("Window is not enabled"); } IntPtr ActualParent; if (Parent == IntPtr.Zero) { ActualParent = Handle; } else { ActualParent = Parent; } if (NM.IsIconic(ActualParent)) { throw new Exception("Window is minimised"); } if (!ActiveWindow(ActualParent)) { SetFocus(Parent, Handle); } else { NM.BringWindowToTop(ActualParent); } NM.tagPoint thePoint = MouseMove(Handle, X, Y); IntPtr WindowAtPoint = NM.WindowFromPoint(thePoint); if (WindowAtPoint != Handle) { throw new Exception("Window is obscured"); } }
//TODO remove theses and replace with above so we get better error messages private static void WaitToBeVisibleAndEnabled(IntPtr handle) { Stopwatch timer = Stopwatch.StartNew(); while (true) { if (timer.ElapsedMilliseconds > GUI.GetTimeOut()) { throw new Exception("Control is not enabled"); } if (NM.IsWindowEnabled(handle)) { if (NM.IsWindowVisible(handle)) { break; } } } timer.Stop(); }
private static void WaitToBeVisibleAndEnabled(GUIObject control) { Stopwatch timer = Stopwatch.StartNew(); while (true) { if (timer.ElapsedMilliseconds > GUI.GetTimeOut()) { throw new Exception(control.Description + " is not enabled"); } if (NM.IsWindowEnabled(control.Handle)) { if (NM.IsWindowVisible(control.Handle)) { break; } } } timer.Stop(); }
private bool EnumProcToGetProcesses(IntPtr hWnd, IntPtr lParam) { uint Pid; NM.GetWindowThreadProcessId(hWnd, out Pid); if (NM.IsWindowVisible(hWnd)) { NM.tagRect WindowSize; NM.GetClientRect(hWnd, out WindowSize); if (WindowSize.right > 0) //If the window has 0 width then ignore it { if (!m_Pid.ContainsKey(Pid)) { m_Pid.Add(Pid, Pid); } } } return(true); }
/// <summary> /// Waits for the control to not be visible /// </summary> public void WaitForControlToNotBeVisible() { //Wait for the control to not be visible Stopwatch timer = Stopwatch.StartNew(); while (true) { if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { throw GUI.ApeException(this.Description + " failed to become nonvisible"); } if (!NM.IsWindowVisible(this.Handle)) { break; } Thread.Sleep(15); } // Small sleep to let focus switch Thread.Sleep(20); }
private void AddChildNode(IntPtr RootParent, TreeNode ParentNode, IntPtr ParentWindow) { //string Name; IntPtr Child = NM.GetWindow(ParentWindow, NM.GetWindowEnum.CHILD); if (Child != IntPtr.Zero) { if (NM.IsWindowVisible(Child)) { GetIdentity(RootParent, Child); string APEType = GetAPEType(m_Identity); //Name = GetName(RootParent, Child); TreeNode ChildNode = ParentNode.Nodes.Add(RootParent.ToString() + ":" + Child.ToString(), APEType + " " + m_Identity.Name + " [" + Child.ToString() + "]"); AddChildNode(RootParent, ChildNode, Child); } IntPtr Sibling = Child; do { Sibling = NM.GetWindow(Sibling, NM.GetWindowEnum.HWNDNEXT); if (Sibling != IntPtr.Zero) { if (NM.IsWindowVisible(Sibling)) { GetIdentity(RootParent, Sibling); string APEType = GetAPEType(m_Identity); //Name = GetName(RootParent, Sibling); TreeNode SiblingNode = ParentNode.Nodes.Add(RootParent.ToString() + ":" + Sibling.ToString(), APEType + " " + m_Identity.Name + " [" + Sibling.ToString() + "]"); AddChildNode(RootParent, SiblingNode, Sibling); } } }while (Sibling != IntPtr.Zero); } }
internal void WaitForAnimation(IntPtr Handle, bool ClearClientArea, WaitForAnimationSource Source) { if (Source == WaitForAnimationSource.Form) { if (GUI.FormAnimationDisabled) { return; } } Stopwatch timer = Stopwatch.StartNew(); do { if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { throw new Exception("Window is not visible"); } Thread.Sleep(15); }while (NM.IsWindowVisible(Handle) == false); NM.tagRect theRect = Display.GetWindowRectangleDIP(Handle); int Width; //if (theRect.Right - theRect.Left > 50) //{ // Width = 50; //} //else //{ Width = theRect.right - theRect.left; //} int Height; //if (theRect.Bottom - theRect.Top > 50) //{ // Height = 50; //} //else //{ Height = theRect.bottom - theRect.top; //} Bitmap A = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); Bitmap B = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); int SameCount = 0; int i = 0; int Loops = 0; //Debug.WriteLine("till bitmap loop " + timer.ElapsedMilliseconds.ToString()); do { Loops++; if (i == 0) { Display.GetWindowBitmap(Handle, theRect.left, theRect.top, ref A, false, ClearClientArea); //A.Save(@"c:\temp\" + y.ToString() + ".png"); //A.Save(@"c:\temp\a.png"); i++; } else { Display.GetWindowBitmap(Handle, theRect.left, theRect.top, ref B, false, ClearClientArea); //B.Save(@"c:\temp\" + y.ToString() + ".png"); //B.Save(@"c:\temp\b.png"); i--; } if (timer.ElapsedMilliseconds > GUI.m_APE.TimeOut) { timer.Stop(); throw new Exception("Failed to detect window finishing animation"); } if (Display.CompareBitmap(A, B)) { SameCount++; //Debug.WriteLine("same " + SameCount.ToString() + " " + timer.ElapsedMilliseconds.ToString()); } else { SameCount = 0; //Debug.WriteLine("reset " + timer.ElapsedMilliseconds.ToString()); } //Thread.Sleep(15); }while (SameCount < 2 || Loops < 9); timer.Stop(); //Debug.Listeners[0].WriteLine("\t Loops: " + Loops.ToString() + " Time: " + timer.ElapsedMilliseconds.ToString()); }
private void FindByIdentifierRenderedActiveX(ControlIdentifier identifier, ref IntPtr handle, ref string name, ref string theText, ref string typeNameSpace, ref string typeName, ref string technologyType, ref string uniqueId, ref bool foundControl) { IntPtr parentHandle = IntPtr.Zero; int currentIndex = 0; if (Ax.Items.Count > 0) { lock (Ax.AxItemsLock) { object control = null; int item; int items = Ax.Items.Count; for (item = 0; item < items; item++) { if (Ax.Items[item].Control == null) { continue; } handle = Ax.Items[item].Handle; parentHandle = Ax.Items[item].ParentHandle; name = Ax.Items[item].Name; uniqueId = Ax.Items[item].UniqueId; control = Ax.Items[item].Control; if (identifier.ParentHandle == parentHandle || NM.GetAncestor(identifier.ParentHandle, NM.GetAncestorFlags.GetRoot) == parentHandle || (identifier.ParentHandle == IntPtr.Zero && parentHandle == handle)) { } else { continue; } if (!NM.IsWindowVisible(handle)) { continue; } //if (!Ax.Items[item].Rendered) //{ // continue; //} if (identifier.Name != null) { if (name != identifier.Name) { continue; } } if (identifier.TechnologyType != null) { if ("Windows ActiveX" != identifier.TechnologyType) { continue; } } if (identifier.TypeNameSpace != null) { typeNameSpace = Ax.Items[item].TypeNameSpace; //Lazy load it only if we need it if (typeNameSpace == null) { continue; } else { if (!Regex.IsMatch(typeNameSpace, identifier.TypeNameSpace)) { continue; } } } if (identifier.TypeName != null) { typeName = Ax.Items[item].TypeName; //Lazy load it only if we need it if (typeName == null) { continue; } else { if (!Regex.IsMatch(typeName, identifier.TypeName)) { continue; } } } if (identifier.ModuleName != null) { if (Path.GetFileName(NM.GetWindowModuleFileName(handle)) != identifier.ModuleName) { continue; } } if (identifier.AssemblyName != null) { continue; } if (identifier.ChildOf != IntPtr.Zero) { if (identifier.ChildOf == handle || NM.IsChild(identifier.ChildOf, handle)) { } else { continue; } } if (identifier.SiblingOf != IntPtr.Zero) { continue; } if (identifier.ParentOf != IntPtr.Zero) { continue; } //Make sure the type name space and type name are populated typeName = Ax.Items[item].TypeName; typeNameSpace = Ax.Items[item].TypeNameSpace; if (typeName == "Label" && string.IsNullOrEmpty(typeNameSpace)) { theText = (string)m_ComReflectDelegater.Invoke("Caption", Ax.Items[item].Control, null); } if (identifier.Text != null) { if (theText == null) { continue; } else { if (!Regex.IsMatch(theText, identifier.Text)) { continue; } } } if (identifier.AccessibilityObjectName != null) { continue; } currentIndex++; if (identifier.Index > 0) { if (currentIndex != identifier.Index) { continue; } } technologyType = "Windows ActiveX"; //WF.MessageBox.Show("Found handle: " + name + " " + handle.ToString()); foundControl = true; return; } } } }
internal void WaitForAnimation(IntPtr Handle, bool ClearClientArea, WaitForAnimationSource Source) { if (GUI.m_APESpy) { return; } Stopwatch timer = Stopwatch.StartNew(); while (true) { if (NM.IsWindowVisible(Handle)) { break; } if (timer.ElapsedMilliseconds > GUI.GetTimeOut()) { throw GUI.ApeException("Window is not visible"); } Thread.Sleep(15); } if (Source == WaitForAnimationSource.Form) { // The window animations we care about are the following: // newly opened window // maximising a window // restoring a window // in all these cases the window should have focus, so if the window doesn't then return if (!Input.HasFocus(IntPtr.Zero, Handle)) { return; } if (GUI.FormAnimationDisabled) { return; } } NM.tagRect theRect = Display.GetWindowRectangleDIP(Handle); int width = theRect.right - theRect.left; int height = theRect.bottom - theRect.top; if (width < 1 || height < 1) { return; } Bitmap A = new Bitmap(width, height, PixelFormat.Format24bppRgb); Bitmap B = new Bitmap(width, height, PixelFormat.Format24bppRgb); try { if (WhiteWindow == null && Source == WaitForAnimationSource.Form) { // If the window has transparency and a window is animating in behind the window then we won't be able to determine // when the window we are interested in has finished animating, so place a solid white window behind the window we are // interested in to remove any possible background animations. Making the white window have WS_EX_TOOLWINDOW extended // style stops it animating when shown or hidden. WhiteWindow = new Form(); WhiteWindow.BackColor = Color.White; WhiteWindow.ShowInTaskbar = false; WhiteWindow.FormBorderStyle = FormBorderStyle.None; NM.SetWindowPos(WhiteWindow.Handle, Handle, theRect.left, theRect.top, width, height, NM.SetWindowPosFlags.HideWindow); long currentExStyle = NM.GetWindowLongPtr(Handle, NM.GWL.GWL_EXSTYLE).ToInt64(); NM.SetWindowLongPtr(WhiteWindow.Handle, NM.GWL.GWL_EXSTYLE, currentExStyle | WS_EX_TOOLWINDOW); } if (Source == WaitForAnimationSource.Form) { NM.SetWindowPos(WhiteWindow.Handle, Handle, theRect.left, theRect.top, width, height, NM.SetWindowPosFlags.ShowWindow); } int SameCount = 0; int i = 0; int Loops = 0; //Debug.WriteLine("till bitmap loop " + timer.ElapsedMilliseconds.ToString()); do { Loops++; if (i == 0) { Display.GetWindowBitmap(Handle, theRect.left, theRect.top, ref A, false, ClearClientArea); //A.Save(@"c:\temp\" + Loops.ToString() + ".png"); //A.Save(@"c:\temp\a.png"); i++; } else { Display.GetWindowBitmap(Handle, theRect.left, theRect.top, ref B, false, ClearClientArea); //B.Save(@"c:\temp\" + Loops.ToString() + ".png"); //B.Save(@"c:\temp\b.png"); i--; } if (timer.ElapsedMilliseconds > GUI.GetTimeOut()) { timer.Stop(); throw GUI.ApeException("Failed to detect window finishing animation"); } if (Display.CompareBitmap(A, B)) { SameCount++; //Debug.WriteLine("same " + SameCount.ToString() + " " + timer.ElapsedMilliseconds.ToString()); } else { SameCount = 0; //Debug.WriteLine("reset " + timer.ElapsedMilliseconds.ToString()); } //Thread.Sleep(15); }while (SameCount < 2 || Loops < 9); } finally { if (Source == WaitForAnimationSource.Form) { NM.SetWindowPos(WhiteWindow.Handle, Handle, theRect.left, theRect.top, width, height, NM.SetWindowPosFlags.HideWindow); } } timer.Stop(); //Debug.Listeners[0].WriteLine("\t Loops: " + Loops.ToString() + " Time: " + timer.ElapsedMilliseconds.ToString()); }