我如何向用户显示等待/忙碌游标(通常是沙漏),让他们知道程序正在做什么?
当前回答
实际上,
Cursor.Current = Cursors.WaitCursor;
临时设置等待游标,但不确保等待游标直到操作结束才显示。程序中的其他程序或控件可以很容易地将光标重置回默认箭头,就像在操作仍在运行时移动鼠标一样。
一个更好的显示等待游标的方法是在表单中设置UseWaitCursor属性为true:
form.UseWaitCursor = true;
这将显示窗体上所有控件的等待游标,直到将此属性设置为false。 如果你想等待光标显示在应用程序级别,你应该使用:
Application.UseWaitCursor = true;
其他回答
你可以使用:
Mouse.OverrideCursor = Cursors.Wait;
&&
Mouse.OverrideCursor = Cursors.Arrow;
在WPF中使用:
Cursor = Cursors.Wait;
// Your Heavy work here
Cursor = Cursors.Arrow;
对于Windows窗体应用程序,可选的禁用ui控件是非常有用的。所以我的建议是这样的:
public class AppWaitCursor : IDisposable
{
private readonly Control _eventControl;
public AppWaitCursor(object eventSender = null)
{
_eventControl = eventSender as Control;
if (_eventControl != null)
_eventControl.Enabled = false;
Application.UseWaitCursor = true;
Application.DoEvents();
}
public void Dispose()
{
if (_eventControl != null)
_eventControl.Enabled = true;
Cursor.Current = Cursors.Default;
Application.UseWaitCursor = false;
}
}
用法:
private void UiControl_Click(object sender, EventArgs e)
{
using (new AppWaitCursor(sender))
{
LongRunningCall();
}
}
好的,其他人的观点很清楚,但我想补充一些,如下:
Cursor tempCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
//do Time-consuming Operations
Cursor.Current = tempCursor;
使用下面的类,您可以使Donut的建议“异常安全”。
using (new CursorHandler())
{
// Execute your time-intensive hashing code here...
}
类CursorHandler
public class CursorHandler
: IDisposable
{
public CursorHandler(Cursor cursor = null)
{
_saved = Cursor.Current;
Cursor.Current = cursor ?? Cursors.WaitCursor;
}
public void Dispose()
{
if (_saved != null)
{
Cursor.Current = _saved;
_saved = null;
}
}
private Cursor _saved;
}
推荐文章
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题