如何在WPF中显示默认光标 [英] How to display default cursor in WPF

查看:310
本文介绍了如何在WPF中显示默认光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





在其中一个WPF应用程序中,我将光标设置为Wait光标,代码如下



Mouse.OverrideCursor = Windows.Input.Cursor.Wait



一段时间后我想把它设置为默认光标。



虽然在MSDN游标类(System.Windows.Input.Cursor)中提到了默认属性



我无法使用它。



有没有办法设置我们在窗体中使用Windows.Forms.Cursors.Default设置的默认光标。









感谢答案。



Vijay

解决方案

你可以这样做:

系统.Windows.Input.Cursor saveCursor = someFrameworkElement.Cursor; 
someFrameworkElement.Cursor = Cursors.WaitCursor;



,最后

 someFrameworkElement。 Cursor = saveCursor; 





另请参阅我的文章,其中我展示如何使用使用基于 System.IDisposable 的语句来做这样的事情:

沙漏鼠标光标始终更改回原始图像。怎么样? [ ^ ]。



如果适用于 System.Windows.Forms 和WPF。



此外,请参阅我的解决方案的更通用的替代方案,我很乐意接受:沙漏鼠标光标总是变回原始图像。怎么样? [ ^ ]。



基于 RAII 模式:http://en.wikipedia.org/wiki/RAII [ ^ ]。



这是非常好的事情。



祝你好运,

-SA


您可以在公共场合使用扩展方法静态类



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





然后很简单:



< pre lang =cs> private void MainWindowLoaded( object sender,RoutedEventArgs e)
{
UseWaitCursor();
....
}


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

解决方案

You can do this:

System.Windows.Input.Cursor saveCursor = someFrameworkElement.Cursor;
someFrameworkElement.Cursor = Cursors.WaitCursor;


and, at the end

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


You might use an extension method in a public static class

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:

private void MainWindowLoaded(object sender, RoutedEventArgs e)
   {
     UseWaitCursor();
   ....
   }


这篇关于如何在WPF中显示默认光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆