如何获取DotNetBrowser中特定元素的绝对坐标?

时间:2016-11-13 05:57:15

标签: dotnetbrowser

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

三江源!!

2 个答案:

答案 0 :(得分:1)

此功能尚未出现在DotNetBrowser DOM API中。但是可以使用Javascript来解决这种情况。

更新:此功能已在DotNetBrowser 1.8.3中添加。现在可以通过DOM API obtain absolute or relative DOMElement position

答案 1 :(得分: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);
        }
    }
}