0

Anyone know how to get the absolute coordinate of a specific elements using DotNetBrowser?

Thankyou!!

2 Answers2

0

This functionality is not present in the DotNetBrowser DOM API yet. But it is possible to workaround this case using Javascript.

Update: This functionality was added in DotNetBrowser 1.8.3. Now it is possible to obtain absolute or relative DOMElement position via the DOM API.

Anna Dolbina
  • 1
  • 1
  • 8
  • 9
  • It's not enough to know the relative or absolute relative to the viewport, but to the whole screen (incluing the pixel resolution). In Internet Explorer there is a transforformation service in the DOM, anything equivalent in DotNetBrowser: var rect = el2.getBoundingClientRect(); var displayServices = el.document as IDisplayServices; var point = new tagPOINT { x = rect.left, y = rect.top }; displayServices.TransformPoint(ref point, _COORD_SYSTEM.COORD_SYSTEM_FRAME, _COORD_SYSTEM.COORD_SYSTEM_GLOBAL, el); – Paulo Aug 02 '17 at 14:27
0

You could put together absolute coordinates of a window, a viewport and an element. To get a screen resolution you could use Screen.PrimaryScreen.Bounds property

Here is a code sample:

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);
        }
    }
}
Vladyslav Tsvek
  • 244
  • 1
  • 10