Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser Control .NET - What's the intended way to interact with this control?

I'm trying to master the WebBrowser control for use in a data extraction application I'm building.

One of the requirements here is to be able to record user actions and play them back. While I'm having a little success, I'm confused about whether I'm going about this in the right way.

There seem to be code samples over the web that use the control in very different ways. Not to mention that there is a WinForms implementation, a WPF implementaion and a Silverlight implementation.

Can someone confirm:

  1. Am I right in my findings so far that the WPF version of the control does not have the same functionality as the WinForms version - and is somewhat limited in terms of reacting to some events?

  2. Why would someone choose to use the mshtml based classes when using the control, when it seems that in WinForms at least, equivalent means of performing the same task exist in the Windows Forms classes?

Examples

Winforms Click Event Handle

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
   foreach (HtmlElement ele in uc_webBrowser.Document.All)
   {
      HtmlElementEventHandler eventhandler = new HtmlElementEventHandler(documentClickHandler);

      if (ele.TagName.ToLower() == "a" || ele.TagName.ToLower() == "input" || ele.TagName.ToLower() == "select" || ele.TagName.ToLower() == "img")
      {
         ele.Click -= eventhandler;
         ele.Click += eventhandler;
      }
   }
}

Click event handle using mshtml classes

void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
   // Add doc null check otherwise the event handlers are assigned multiple times
   // http://stackoverflow.com/questions/5400020/how-come-htmldocumentevent-onclick-fires-up-multiple-times

   if (doc == null)
   {
      var eventHdlr = new mshtml.HTMLDocumentEvents2_onclickEventHandler(ClickEventHandler);

      doc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;
      mshtml.HTMLDocumentEvents2_Event iEvent = (mshtml.HTMLDocumentEvents2_Event)doc;
      iEvent.onclick -= eventHdlr;
      iEvent.onclick += eventHdlr;
      //iEvent.oncellchange += new HTMLDocumentEvents2_oncellchangeEventHandler(iEvent_oncellchange);
      //iEvent.oncontrolselect += new HTMLDocumentEvents2_oncontrolselectEventHandler(iEvent_oncontrolselect);
      //iEvent.onselectionchange += new HTMLDocumentEvents2_onselectionchangeEventHandler(iEvent_onselectionchange);
   }
}
like image 461
gb2d Avatar asked Nov 14 '22 19:11

gb2d


1 Answers

Years ago I implemented a somewhat complex WYSIWYG editor using WinForms' WebBrowser, back then the browser was IE 7 (there was that or Firefox 1 or 2, I don't remember). If you go any further than loading a page in the browser, you will see that the MSHTML PIA is a pain to work with.

The behaviour of the browser component wasn't 100% deterministic with IE 7 (hopefully that may have changed with IE 9, but worse yet, you may have to deal with different versions of the Microsoft browser). However a well designed OO layer above all that will make miracles for your productivity.

I will try to answer your doubts about using a web browser:

  1. According to the following link, in the WPF's WebBrowser you will get as much functionality as you get with JavaScript. As macros has been done before in browsers, you will certainly have enough events to do what you want.

    What functional differences exist between WPF and WinForms WebBrowser control?

  2. If you don't feel like you need the WebBrowser control, run from it while you can. A browser in a Desktop GUI can help a lot, but it comes at a cost.

However, if you feel like using one, you might want to give different browsers a try. The ASP.NET designer at MonoDevelop for instance, was developed with Mozilla Composer (Gecko engine) a while ago and before WebForms got so unwanted, there was a project to develop that from scratch using the WebKit engine.

like image 51
michelpm Avatar answered Nov 17 '22 06:11

michelpm