Link to home
Start Free TrialLog in
Avatar of famoso
famosoFlag for United States of America

asked on

MSHTML mouseover events to identify html elements

I need code - C# preferrablly.

I have a WinForm, Browser Control and TextBox.  I navigate to http://www.google.com/ when the form loads.

I would like the elements on the page to appear in the Textbox as I move the mouse over the webpage.

Examples:
As I move the mouse over the textbox on the Google page, in my WinForm Textbox should be something like:
Input

As I move the mouse over one of the buttons on the Google page, in my WinForm Textbox should be something like:
(Bad example but ... )  Input  (I could get further if I had code to get me that far)

I would collect more info as in the case of the Button ie: Input - type=submit, etc.

I just need a nudge in the right direction.  Anybody???
Avatar of AndyAelbrecht
AndyAelbrecht

OK, as we are in the VB.NET section of the website, this shouldn't be here.

now that is out of the way, here's a possible solution to your problem:

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.google.be");
        }

        void thisdoc_MouseMove(object sender, HtmlElementEventArgs e)
        {
            textBox1.Text = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).TagName;
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            HtmlDocument thisdoc;
            thisdoc = webBrowser1.Document;
            thisdoc.MouseMove += new HtmlElementEventHandler(thisdoc_MouseMove);
        }

explanation: the WebBrowser has no MouseMove event handler, but the underlying document does; we only need to add an eventhandler to the current document every time we navigate in the webbrowser; this event handler has 1 line of code: it will get the element(frompoint)'s tagname and put it in your textbox.

if you want to see other properties of the elements you are hovering, feel free to replace the TagName with something else (just remove it and let IntelliSense guide you).

I hope this helps,

cheers,
Andy
I was almost forgetting that you can't just add this code to your project ofcourse :-)
But it is pretty obvious what events are firing the eventhandlers you see here.

just in case:
Form1_Load -> Form Load event
webBrowser1_Navigated -> webBrowser(1) Navigated

thisdoc_MouseMove -> this can be added as is (this eventhandler is coupled to the thisdoc.MouseMove event every time we navigate to a new page)
Avatar of famoso

ASKER

I appologize but I had asked this question days ago and I've been having problems having it listed on here.  I got frantic and placed it here too assuming that I would get a VB answer but if C# was available that would be even better.

So far this is what I have:  The problem is that I can't get axWebBrowserq.Document to have a GetElementFormPoint() method.  I only get: Equals, GetHashCode, GetType, and ToString

Also was I right to add System.Windows.Forms to the HtmlDocument?

    void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e) {
      System.Windows.Forms.HtmlDocument thisdoc;
      thisdoc=(System.Windows.Forms.HtmlDocument)axWebBrowser1.Document;
      thisdoc.MouseMove+=new HtmlElementEventHandler(thisdoc_MouseMove);
    }

    void thisdoc_MouseMove(object sender, HtmlElementEventArgs e) {
      txtPageInfo.AppendText(axWebBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).TagName);
    }
hmmm
My code assumes Visual Studio 2005 actually, which uses a WebBrowser component and not an ActiveX Internet Explorer component.

If you have VS2005, I highly suggest using the WebBrowser component.

If you don't, I'm afraid somebody will have to help you further as I don't have VS2003 (mandatory upgrades :()

cheers,
Andy
Avatar of famoso

ASKER

Understood.  Thank you for your assistance.
Avatar of famoso

ASKER

Please refund points.
Avatar of famoso

ASKER

I got by with a little help from my friends.

                Dim doc As mshtml.HTMLDocument
                doc = wbrETL.Document
                If klicked = False Then
                    klicked = True
                    Try
                        //  Also, there is an onMouseOver() from HTMLDocumentEvents2_Events.
                        AddHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf Document_onclick
                    Catch ex As Exception
                        MessageBox.Show("AddHandler Failed: " + ex.Message)
                    End Try
                Else
                    klicked = False
                    Try
                        RemoveHandler CType(doc, mshtml.HTMLDocumentEvents2_Event).onclick, AddressOf Document_onclick
                    Catch ex As Exception
                        'No need for an Error Handler here [famoso]
                    End Try
                End If

    Private Function Document_onclick(ByVal e As mshtml.IHTMLEventObj) As Boolean
        lstEventLog.Items.Insert(0, "onClick: " & e.srcElement.outerHTML.ToString())
        Return False
    End Function
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial