简体   繁体   English

如何在窗口中查找WPF元素的位置

[英]How do i find location of a wpf element in a window

I need to be able to find the location of a WPF element on an application window so i can then draw a rectangle around the specific element on a screenshot/image capture of the window containing this element. 我需要能够在应用程序窗口中找到WPF元素的位置,以便随后可以在包含该元素的窗口的屏幕快照/图像捕获中的特定元素周围绘制一个矩形。

This is for a VS Coded UI Framework i am developing that has window captures or element capture to an image when an error occurs. 这是针对我正在开发的VS Code的UI框架,当发生错误时,该框架具有窗口捕获或元素捕获到图像。 I want to combine these two and have a window capture that shows a red rectangle around the element in error. 我想将两者结合起来,并有一个窗口捕获,该窗口捕获在错误的元素周围显示一个红色矩形。

Alot of forums have answers around this, but they refer to TranslatePoint . 很多论坛都提供有关此问题的答案,但它们都指向TranslatePoint I cannot see this when working with Visual Studio and Microsofts UITesting Coded UI WpfControls. 使用Visual Studio和Microsoft的UITesting编码UI WpfControls时,我看不到此消息。 Anyone know how to get around this?? 有人知道如何解决这个问题吗?

Never use coordinates. 切勿使用坐标。 Instead, get the control and use the DrawHighlight() method. 而是,获取控件并使用DrawHighlight()方法。 This will draw a blue rectangle outside the control. 这将在控件外部绘制一个蓝色矩形。

TranslatePoint is defined on UIElement . TranslatePoint是在UIElement上定义的。 Any UIElement should expose this. 任何UIElement都应公开此内容。

I suspect the problem is that WpfControl is not a WPF UIElement . 我怀疑问题是WpfControl不是WPF UIElement You should be able to use it's BoundedRectangle property to get the proper surrounding rectangle instead of TranslatePoint on a UIElement . 您应该能够使用它的BoundedRectangle属性来获取适当的周围矩形,而不是UIElement上的TranslatePoint。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM