Nikolay
Nikolay

Reputation: 587

Change webbrowser Cursor

I have a form that contains WebBrowser control. I need to change the cursor to WebBrowser.

I try

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

The cursor changes form only, but not for WebBrowser.

How can I change the cursor in WebBrowser control?

Upvotes: 1

Views: 5128

Answers (4)

Elmue
Elmue

Reputation: 8148

If you set the cursor of a Form with a WebBrowser control in it, the Form will show the wait cursor, but the browser will not, because the browser sets the cursor on it's own corresponding to the HTML content. For example if you move the mouse over a hyperlink, Internet Explorer changes the cursor to the hand cursor. Also JavaScript and CSS can modify the cursor. So there is no way to set a WaitCursor while Internet Explorer is controling the cursor.

But I found a trick to do this with one line of code!

If you do a lengthy processing and want to display the wait cursor meanwhile, you can use this code to turn it on and off:

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

The trick is disabling the browser control and it will show the Wait Cursor because a disabled Internet Explorer does not control the cursor anymore.

So your final code will look like this:

SetWaitCursor(true);

doLenghtyProcessing();

SetWaitCursor(false);

Upvotes: 0

Rajendra Yadav
Rajendra Yadav

Reputation: 145

Try this:

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;

Upvotes: 0

Andrei V
Andrei V

Reputation: 7506

Add a reference to your solution to "mshtml.dll". After you load your Document, try this:

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

Please bear in mind that the result depends also on the way you load the web page (load a local/embedded file, set the DocumentStream, etc.).

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Reason for Failure:

you are setting the Cursor for Form Instead of WebBrowser Control as below:

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

thats why it is setting the cursor to Form instead of WebBrowser Control.

you can set the Cursor for any Control as below:

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

but WebBrowser control does not support the Cursor property. hence you can not set this property for WebBrowser Control.even if you set it, it does not give compile errors but throws following Runtime Error.

WebBrowser Control does not Support the Cursor Property.

Upvotes: 0

Related Questions