• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 941
  • Last Modified:

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???
0
famoso
Asked:
famoso
  • 5
  • 4
  • 3
1 Solution
 
sumixCommented:

 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.
        }
 
0
 
famosoAuthor Commented:
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);
      }
    }
0
 
levyukCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
levyukCommented:
This is one of my previous posts regarding the timer idea. Check it out it might help you although it is in VB.net the principle is the same

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21513482.html
0
 
famosoAuthor Commented:
Does it make a difference that I'm using .NET 2.0 and the MSHTML WebBrowser?
0
 
levyukCommented:
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.
0
 
famosoAuthor Commented:
Sumix, any ideas about my comment on 05/30/2006 11:19AM EDT???
0
 
sumixCommented:

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.
0
 
famosoAuthor Commented:
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.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21857127.html
0
 
sumixCommented:
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


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

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now