0

任何人都知道如何使用 DotNetBrowser 获取特定元素的绝对坐标?

谢谢!!

4

2 回答 2

1

DotNetBrowser DOM API 中尚不存在此功能。但是可以使用 Javascript 解决这种情况。

更新:此功能是在 DotNetBrowser 1.8.3 中添加的。现在可以通过 DOM API获取绝对或相对 DOMElement 位置。

于 2016-11-14T06:00:12.713 回答
0

您可以将窗口、视口和元素的绝对坐标放在一起。要获得屏幕分辨率,您可以使用Screen.PrimaryScreen.Bounds属性

这是一个代码示例:

public partial class Form1 : Form
{
    BrowserView browserView;

    public Form1()
    {
        InitializeComponent();
        browserView = new WinFormsBrowserView();

        browserView.Browser.FinishLoadingFrameEvent += Browser_FinishLoadingFrameEvent;
        Controls.Add((Control)browserView);
        browserView.Browser.LoadURL("google.com");
    }

    private void Browser_FinishLoadingFrameEvent(object sender, DotNetBrowser.Events.FinishLoadingEventArgs e)
    {
        if (e.IsMainFrame)
        {
            DOMDocument document = e.Browser.GetDocument();
            DOMElement element = document.GetElementByName("btnK");
            Rectangle rectangle = element.BoundingClientRect;

            Rectangle resolution = Screen.PrimaryScreen.Bounds;
            Debug.WriteLine("Screen resolution = " + resolution.Width +
               'x' + resolution.Height);

            Debug.WriteLine("Form X = " + Location.X);
            Debug.WriteLine("Form Y = " + Location.Y);
            Debug.WriteLine("browserView X = " + ((Control)browserView).Location.X);
            Debug.WriteLine("browserView Y = " + ((Control)browserView).Location.Y);
            Debug.WriteLine("X = " + rectangle.Location.X);
            Debug.WriteLine("Y = " + rectangle.Location.Y);

            int absoluteX = Location.X + 
               ((Control)browserView).Location.X + rectangle.Location.X;
            int absoluteY = Location.Y + 
               ((Control)browserView).Location.Y + rectangle.Location.Y;

            Debug.WriteLine("Absolute X = " + absoluteX);
            Debug.WriteLine("Absolute Y = " + absoluteY);
        }
    }
}
于 2017-08-04T16:08:04.597 回答