4

I want to do a HTML Render that shows a HTML Document, not necessary an online webpage. Then when I click over a HTML Control, it shows only the HTML where I clicked. The real intention is to get the xpath from the root element to the selected TAG.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Johans
  • 49
  • 1
  • 3

1 Answers1

5

I think that you must use System.Windows.Forms.WebBrowser control for loading your html document. Override for example OnLeftButton event of the Form. And then call WebBrowser.Document.GetElementFromPoint method. So this method will return object of HtmlElement type. As the result you'll get html element from which you could navigate to inner html source code or navigate by hierarchy of tags from your selected tag;)

I create some example for you:

private static String GetTagNameByClick(WebBrowser refWebBrowser, Int32 valScreenX, Int32 valScreenY)
    {
        Point refPoint = refWebBrowser.PointToClient(new Point(valScreenX, valScreenY));

        HtmlElement refHtmlElement = refWebBrowser.Document.GetElementFromPoint(refPoint);

        return refHtmlElement.TagName;
    }

Good luck!

Edward83
  • 6,664
  • 14
  • 74
  • 102
  • 1
    Thank you edward, it really helped me find the way =) !! now i'm just trying to find out how to get the position clicked on the browser. I tryied your way, but the i had to overload the Document.Click event from the webBrowser.Document to get the event fired. Thank you very much for this light. – Johans Nov 23 '10 at 00:22
  • @Johans i did not see your comment! Sorry! As i understand your problem: valScreenX and valScreenY in example are coordinates from the screen, not from Form or WebBrowser control;) – Edward83 Nov 25 '10 at 16:34
  • @Johans use this class http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx for getting mouse cursor positions "A Point that represents the cursor's position in screen coordinates." ;) – Edward83 Nov 25 '10 at 16:40