【问题标题】:Change webbrowser Cursor更改浏览器光标
【发布时间】:2013-11-08 11:58:55
【问题描述】:

我有一个包含 WebBrowser 控件的表单。 我需要将光标更改为 WebBrowser。

我试试

this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.TopLevelControl.Cursor = Cursors.WaitCursor;

光标只改变窗体,但不用于 WebBrowser。

如何更改 WebBrowser 控件中的光标?

【问题讨论】:

  • WebBrowser 控件不支持 Cursor 属性。
  • 我认为这不可能。您必须在显示的网页中更改光标(CSS:body { cursor: wait; })。
  • 如果光标是由 HTML 文档通过 CSS 设置的,就像@AndreiV 指出的那样?您是否试图覆盖此行为?
  • 在我的测试中,直接在网页中设置光标按要求工作。只有当您可以直接访问所有将要加载的页面时,才能使用该选项。我不知道您是否以及如何注入这样的规则(无需解析源代码并“手动”设置)。
  • 有没有办法设置(CSS:body {cursor:wait;})不重新加载页面到滚动条的位置,选中和输入的文字不改变?

标签: c# .net webbrowser-control


【解决方案1】:

添加对“mshtml.dll”解决方案的引用。加载 Document 后,试试这个:

IHTMLDocument2 doc = (webDocument1.Document.DomDocument) as IHTMLDocument2;
IHTMLStyleSheet style = doc.createStyleSheet("", 0);
style.cssText = @"body { cursor: wait; }";

请记住,结果还取决于您加载网页的方式(加载本地/嵌入文件、设置DocumentStream 等)。

【讨论】:

  • 谢谢,真的很有帮助,无需重新加载页面。如何删除已安装的 cssStyle "body {cursor: wait;}"?
  • @Nikolay,如果要将光标设置回默认箭头,只需使用style.cssText = @"body { cursor: default; }";
【解决方案2】:

失败原因:

您将光标设置为 Form 而不是 WebBrowser 控件,如下所示:

this.Cursor = System.Windows.Forms.Cursors.WaitCursor;

这就是为什么它将光标设置为Form 而不是WebBrowser Control。

您可以为任何控件设置光标,如下所示:

controlName.Cursor=System.Windows.Forms.Cursors.WaitCursor;

WebBrowser 控件不支持Cursor 属性。 因此您不能为WebBrowser Control 设置此属性。即使您设置了它,它也不会给出编译错误,但会引发运行时错误。

WebBrowser Control does not Support the Cursor Property.

【讨论】:

  • 这不能回答问题如何更改 WebBrowser 控件中的光标?.
【解决方案3】:

试试这个:

Icon ico = new Icon(@"C:\temp\someIcon.ico");
this.Cursor = new Cursor(ico.Handle);
The static class System.Windows.Forms.Cursors contains all system cursors.
To switch back to the default system cursor, use this:

this.Cursor = System.Windows.Forms.Cursors.Default;

【讨论】:

    【解决方案4】:

    如果您在其中设置了带有 WebBrowser 控件的表单的光标,该表单将显示等待光标,但浏览器不会,因为浏览器将光标设置在它自己对应的 HTML 内容上。例如,如果您将鼠标移到超链接上,Internet Explorer 会将光标更改为手形光标。 JavaScript 和 CSS 也可以修改光标。因此,当 Internet Explorer 控制光标时,无法设置 WaitCursor。

    但我发现了一个用一行代码就能做到这一点的技巧!

    如果你做了一个冗长的处理,并且想同时显示等待光标,你可以使用这个代码来打开和关闭它:

        public void SetWaitCursor(bool b_Wait)
        {
            Application.UseWaitCursor = b_Wait;
    
            // The Browser control must be disabled otherwise it does not show the wait cursor.
            webBrowser.Enabled = !b_Wait;
            Application.DoEvents();
        }
    

    诀窍是禁用浏览器控件,它会显示等待光标,因为禁用的 Internet Explorer 不再控制光标。

    所以你的最终代码将如下所示:

    SetWaitCursor(true);
    
    doLenghtyProcessing();
    
    SetWaitCursor(false);
    

    【讨论】:

      猜你喜欢
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多