WebBrowser Control .NET - 与此控件交互的预期方式是什么?

发布于 2024-11-26 11:50:13 字数 2086 浏览 3 评论 0原文

我正在尝试掌握 WebBrowser 控件,以便在我正在构建的数据提取应用程序中使用。

这里的要求之一是能够记录用户操作并回放它们。虽然我取得了一些成功,但我对自己是否以正确的方式去做这件事感到困惑。

网络上似乎有一些代码示例以非常不同的方式使用该控件。更不用说还有WinForms 实现、WPF 实现和Silverlight 实现。

有人可以确认一下:

  1. 到目前为止,我的发现是否正确,WPF 版本的控件不具有与 WinForms 版本相同的功能 - 并且在对某些事件的反应方面受到一定限制?

  2. 为什么有人在使用控件时选择使用基于 mshtml 的类,而至少在 WinForms 中,Windows 窗体类中存在执行相同任务的等效方法?

示例

Winforms 单击事件句柄

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;
      }
   }
}

使用 mshtml 类的单击事件句柄

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);
   }
}

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);
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-12-03 11:50:13

几年前,我使用 WinForms 的 WebBrowser 实现了一个有点复杂的 WYSIWYG 编辑器,当时的浏览器是 IE 7(有那个,或者 Firefox 1 或 2,我不记得了)。如果您不只是在浏览器中加载页面,您会发现使用 MSHTML PIA 是一件痛苦的事情。

IE 7 中浏览器组件的行为并不是 100% 确定的(希望 IE 9 中这一点可能有所改变,但更糟糕的是,您可能必须处理不同版本的 Microsoft 浏览器)。然而,精心设计的 OO 层最重要的是,它将为您的生产力创造奇迹。

我将尽力解答您对使用 Web 浏览器的疑问:

  1. 根据以下链接,在 WPF 的 WebBrowser 中您将获得与 JavaScript 一样多的功能。由于之前已经在浏览器中完成了宏,因此您肯定会有足够的事件来执行您想要的操作。

    <块引用>

    之间存在哪些功能差异WPF 和 WinForms WebBrowser 控件?

  2. 如果您觉得不需要 WebBrowser 控件,请在可以的时候运行它。桌面 GUI 中的浏览器可以提供很大帮助,但这是有代价的。

但是,如果您想使用其中一个,则可能需要尝试不同的浏览器。例如,MonoDevelop 的 ASP.NET 设计器不久前是使用 Mozilla Composer(Gecko 引擎)开发的,在 WebForms 变得如此受欢迎之前,有一个项目使用 从头开始​​开发它WebKit 引擎。

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.

意中人 2024-12-03 11:50:13

我很早就写过一些关于 WebBrowser 的文章。如果你愿意,你可以尝试这个:
http://www.dotnetfunda.com/articles/文章840-使用-webbrowser-in-wpf-.aspx

谢谢

I have written something about WebBrowser way back. You can try this if you want :
http://www.dotnetfunda.com/articles/article840-working-with-webbrowser-in-wpf-.aspx

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文