locked
Drawing rectangle around element to highlight RRS feed

  • Question

  •  

    I am building an application where user can browse to a web page (in my WebBrowser control in .Net 3.5 and C#) and select different elements which i need to highlight. one way to achecive is to change the style of the selected element but that changes page structure and fires page change event which i dont want to do. 

     

    I am thinking of getting screen co-ordinates for the selected element and drawing a rectagle around it.

     

    is there  a simple way in C# to get HTML element's screen co-ordinates and draw a rectangle around it?

     

    I'll highly appreciate a sample code in C# to do this.

     

    Thanks,

    Salim  

    Tuesday, June 17, 2008 2:42 PM

Answers

  • Thanks Dude.

     

    I really appreciate your help. I know this stupid msdn sometimes becomes pain to work with.

     

    Anyways, Thanks again

    Salim

     

    Thursday, June 19, 2008 7:18 PM

All replies

  • You want to get the elements IHTMLElement2 interface and use the getBoundingClientRect method.

    Warning: If the element is in a frame, the coords are relative to the frame's origin, not the browsers.


    Tuesday, June 17, 2008 2:55 PM
  • So how do i map it to screen co-ordinates? What method should i use that works for all elements no matter they are in a frame or not?

     

    Thanks,

    Salim

     

    Thursday, June 19, 2008 1:08 PM
  • *** dude.  I typed it twice.  MSDN keeps failing when I hit submit.

    Long and short of it is use IDisplayServices to transform the points.
    Thursday, June 19, 2008 6:24 PM
  • Step 1: Get IHTMLElement for the element you want to bound (pElement).  Get it's rect and convert the upper left and bottom right to POINT structures. (point)

    Step 2: Get the IHTMLDocument2 pointer for the outermost document (the guy who hosts all the frames) (pDoc2).

    Step 3: Something like:

    Code Snippet

    CComPtr<IDisplayServices> spDisplayServices;
    HRESULT hr = pDoc2->QueryInterface(IID_IDisplayServices, (void **)&spDisplayServices);
    if (SUCCEEDED(hr))
    {
        hr = spDisplayServices->TransformPoint(&point, COORD_SYSTEM_FRAME, COORD_SYSTEM_GLOBAL, pElement);
    }


    Thursday, June 19, 2008 6:35 PM
  • Thanks Dude.

     

    I really appreciate your help. I know this stupid msdn sometimes becomes pain to work with.

     

    Anyways, Thanks again

    Salim

     

    Thursday, June 19, 2008 7:18 PM
  • I treid your approach in C# but i am not able to type cast the web document to IDisplayServices interface. It keeps throwing InValidCastException (mshtml.HTMLDocumentClass cannot be converted into IDisplayServices). No such interface supported.

     

    I am using SHDocvw.InternetExplorer to navigate and retrieve elements from the web page. Anyone knows how to use this in C#?

     

    Thanks,

    Salim

     

    Friday, June 20, 2008 8:31 PM
  • I'm not an expert in C# and Interop, so I'm not sure what's going on.  It could be the same problem that keeps the interface from being marshallable by COM is keeping you from using it.

     

    -jeff

    Monday, June 23, 2008 4:56 PM