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 sumix
sumix


 in form constructor:
     this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

 in webBrowser1_DocumentCompleted method assign a handler to Mouseover event of the loaded document:

      void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.MouseOver += new HtmlElementEventHandler(Document_MouseOver);
        }

 in Document_MouseOver method you have acces to the hovered HtmlElement, you can retrieve any of its properties:
     
       void Document_MouseOver(object sender, HtmlElementEventArgs e)
        {
            this.textBox1.Text = e.ToElement.TagName+","+e.ToElement.Name; // etc.
        }
 
Avatar of famoso

ASKER

Sumix, I love your work. I've seen it all over this site.

A problem I'm having is that there isn't a MouseOver for Document.

I have this:

    void Document_MouseOver(object sender, HtmlElementEventArgs e) {
      this.txtPageInfo.AppendText(e.ToElement.TagName+","+e.ToElement.Name); // etc.
    }

    private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e) {
      //MessageBox.Show("Document Really Complete");
      try {

// The problem is the axWebBrowser1.Document doesn't let me have a MouseOver handler
// all I have is: Equals, GetHashCode, GetType, and ToString
        axWebBrowser1.Document.MouseOver+=new HtmlElementEventHandler(Document_MouseOver);

        // Initialize all the documents
        htmlDoc=(HTMLDocument)axWebBrowser1.Document;
        htmlDoc1=(mshtml.IHTMLDocument)axWebBrowser1.Document;
        htmlDoc2=(mshtml.IHTMLDocument2)axWebBrowser1.Document;
        htmlDoc3=(mshtml.IHTMLDocument3)axWebBrowser1.Document;

        txtAddressBar.Text=htmlDoc2.url.ToString();
        txtPageInfo.AppendText(System.Environment.NewLine+"axWebBrowser1_DocumentComplete Fired");

        getHeaders();

        getHTML();

        getForms();

        getLinks();

        getImages();
      } catch (Exception ex) {
        MessageBox.Show("axWebBrowser1_DocumentComplete: "+ex.Message);
      }
    }
This is something I used to get the element at the location of the mouse. I made a timer and on every tick of the timer I got the element at the mouse location. It worked well but then I thought it would be better to get the element only when someone clicks. But you should try the timer idea. I don't have the code anymore so can't paste it.
ASKER CERTIFIED SOLUTION
Avatar of levyuk
levyuk

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
Avatar of famoso

ASKER

Does it make a difference that I'm using .NET 2.0 and the MSHTML WebBrowser?
No it shouldn't make a difference. Although I also used net 2.0 but I changed to using the WebBrowser control instead of the mshtml control. It is easier to use and get access to the HTML page properties. The same timer principle should work no matter which webbrowser control you use.
Avatar of famoso

ASKER

Sumix, any ideas about my comment on 05/30/2006 11:19AM EDT???

Hello, famoso, thank you..

I have, indeed, used the webbrowser control and htmldocument class from 2.0 framework (System.Windows.Forms namespace, mouseover is an event of this HTMLDocument class). You should use those classes if you have .net 2.0.
Avatar of famoso

ASKER

Sumix,

Although I would like to give the points to you, it doesn't seem I will be able to use that solution since I am using axWebBrowser1.  Although I do need help on the following issue still withstanding.  I got the href to the area but now I can't produce a click() event for it?  Please read and help and I will award you points for it even if I need to open a separate question.  Please let me know how you would like to handle it.

https://www.experts-exchange.com/questions/21857127/MSHTML-Area-Map-Click-C-preferred.html
ok, i'll try to give you a hint for that question here since the other one was closed:
     try
         ((mshtml.IHTMLElement)areaElement).click();

as a note, if you have more items in the collection and you need to perform a click on a specific area you can identify it as in the response you got, only if AREA tag has a name attribute. Otherwise you need to loop through the collection using its Enumerator or a foreach loop, identify the desired Area and call a click method something like:
     foreach (mshtml.IHTMLAreaElement areaElement in collection)
      if (areaElement.href.EndsWith("somelink.html")) // or any other way to identify the Area element
                           ((mshtml.IHTMLElement)areaElement).click();

   hope this is of some help, no need points, maybe with another occasion

 regards


Avatar of famoso

ASKER

That worked.  YOU ARE AMAZING.  How can I get points to you???  Any way just let me know.  Thank you OH WISE ONE.

  I don't even know exactly, don't bother anyway, the fact you are so pleased worths much more for me