Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

In one of the WPF application, I am setting cursor to Wait cursor with below code

Mouse.OverrideCursor = Windows.Input.Cursor.Wait

after some time I want to set it to default cursor.

Though in MSDN cursor class (System.Windows.Input.Cursor) default property is mentioned

I am not able to use it.

Is there any way to set the default cursor like we set in the form using Windows.Forms.Cursors.Default.




Appreciate the answer.

Vijay
Posted

You can do this:
C#
System.Windows.Input.Cursor saveCursor = someFrameworkElement.Cursor;
someFrameworkElement.Cursor = Cursors.WaitCursor;

and, at the end
C#
someFrameworkElement.Cursor = saveCursor;


Please also see my article where I show how to use using statement based in System.IDisposable to do such things:
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

If works for both System.Windows.Forms and WPF.

Also, see more generic alternative to my solution, which I gladly accepted: Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^].

Is is based on the RAII pattern: http://en.wikipedia.org/wiki/RAII[^].

This is something which is very good to know.

Good luck,
—SA
 
Share this answer
 
v3
Comments
Vijay hit 19-Nov-12 21:52pm    
Hi Sergey,

Thanks for the answer.

Cursor.Current = Cursors.Default din't worked as Default is not appearing in cusors.
I am getting error like default is not a member of cursors.


But your blog helped a lot. I just used your technique of setting Previous and it worked well.

Dim previous as system.windows.input.cursor = me.cursor
me.Cursor = cursors.wait

after my task I reset the cursor by
me.cursor = previous.

Thanks again,
Vijay
Sergey Alexandrovich Kryukov 19-Nov-12 23:37pm    
Great.
Sorry, I messed up a bit, edited the sample; thank you for fixing it.
You are very welcome.
Good luck, call again.
--SA
You might use an extension method in a public static class

C#
public static void UseWaitCursor(this FrameworkElement fel)
        {
            Mouse.OverrideCursor=Cursors.Wait;
            fel.Dispatcher.InvokeAsync(() => { Mouse.OverrideCursor=null; }, System.Windows.Threading.DispatcherPriority.ApplicationIdle);
        }


Then its easy like:

C#
private void MainWindowLoaded(object sender, RoutedEventArgs e)
   {
     UseWaitCursor();
   ....
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900