private void dispatcherTimer_Tick(object sender, EventArgs e) { try { enablePassThrough = true; var screenPoint = System.Windows.Forms.Cursor.Position; AutomationElement element = null; if (IsWindowHighlight) { IntPtr hWnd = UiCommon.GetRootWindow(screenPoint); if (hWnd != IntPtr.Zero) { element = UIAUiNode.UIAAutomation.FromHandle(hWnd); } } else { try { element = UIAUiNode.UIAAutomation.FromPoint(screenPoint); } catch (Exception) { if (element == null) { IntPtr hWnd = UiCommon.WindowFromPoint(screenPoint); if (hWnd != IntPtr.Zero) { element = UIAUiNode.UIAAutomation.FromHandle(hWnd); } } } if (inspectJavaUiNode(element, screenPoint)) { return; } } CurrentHighlightElement = new UIAUiNode(element); if (element == null) { return; } var rect = element.BoundingRectangle; //计算鼠标点击时的相对元素的偏移,以供后期有必要时使用 CurrentHighlightElementRelativeClickPos = new Point(screenPoint.X - rect.Left, screenPoint.Y - rect.Top); this.MoveRect(rect); } catch (Exception) { } finally { enablePassThrough = false; this.TopMost = true; } }
public UiNode FindRelativeNode(int position, int offsetX, int offsetY) { UIAUiNode relativeNode = null; Rectangle rect = automationElement.BoundingRectangle; int posX, posY; Point point; switch (position) { case 0: //中心位置偏移 posX = rect.Location.X + rect.Width / 2; posY = rect.Location.Y + rect.Height / 2; point = new Point(posX + offsetX, posY + offsetY); break; case 1: //左上角偏移 posX = rect.Location.X; posY = rect.Location.Y; point = new Point(posX + offsetX, posY + offsetY); break; case 2: //右上角偏移 posX = rect.Location.X + rect.Width; posY = rect.Location.Y; point = new Point(posX + offsetX, posY + offsetY); break; case 3: //左下角偏移 posX = rect.Location.X; posY = rect.Location.Y + rect.Height; point = new Point(posX + offsetX, posY + offsetY); break; case 4: //右下角偏移 posX = rect.Location.X + rect.Width; posY = rect.Location.Y + rect.Height; point = new Point(posX + offsetX, posY + offsetY); break; default: //默认中心偏移 posX = rect.Location.X + rect.Width / 2; posY = rect.Location.Y + rect.Height / 2; point = new Point(posX + offsetX, posY + offsetY); break; } try { this.automationElement = UIAUiNode.UIAAutomation.FromPoint(point); } catch (Exception) { if (this.automationElement == null) { IntPtr hWnd = UiCommon.WindowFromPoint(point); if (hWnd != IntPtr.Zero) { this.automationElement = UIAUiNode.UIAAutomation.FromHandle(hWnd); } } } relativeNode = new UIAUiNode(automationElement); return(relativeNode); }