Winform应用程序中的沙漏问题
-
01-10-2019 - |
题
在我的程序中,在Winform中使用UI。我将光标设置为一个沙漏,以便在ThreadPool中启动方法。
我在UI线程中以设置光标的代码如下:
Application.UseWaitCursor = true;
方法完成后,我返回UI线程将光标设置为正常情况。
Application.UseWaitCursor = false;
我的问题是光标保持到沙漏,直到我不移动鼠标为止。如果用户在动作结束时不移动鼠标,则有点令人不安。
有人可以帮我吗?
Jérôme
解决方案
我无法再现这种行为?这对我来说可以。
如果您使用的是一件事 Control.Cursor = Cursors.WaitCursor
方法是通常这样使用:
this.Cursor = Cursors.WaitCursor
但是,这似乎很好 this
引用表单,因此,如果用户将鼠标移动到其他控件,例如文本框,则鼠标不会显示等待光标。
这可能会给用户带来混乱。如果用户在应用程序忙于做其他工作时继续处理其他事情,可能会引起一些问题。
其他提示
实际上,还有另一种方法可以做到这一点,在研究了这个问题几个小时后,我发现了这一点。
不幸的是,这是一个黑客。
以下是我写的一种方法可以解决问题的方法。
/// <summary>
/// Call to toggle between the current cursor and the wait cursor
/// </summary>
/// <param name="control">The calling control.</param>
/// <param name="toggleWaitCursorOn">True for wait cursor, false for default.</param>
public static void UseWaitCursor(this Control control, bool toggleWaitCursorOn)
{
...
control.UseWaitCursor = toggleWaitCursorOn;
// Because of a weird quirk in .NET, just setting UseWaitCursor to false does not work
// until the cursor's position changes. The following line of code fakes that and
// effectively forces the cursor to switch back from the wait cursor to default.
if (!toggleWaitCursorOn)
Cursor.Position = Cursor.Position;
}
另一种方法:
Cursor.Current = Cursors.WaitCursor;
完成后,只需更改光标:
Cursor.Current = Cursors.Default;
手动设置光标。我就是做这个的。
不隶属于 StackOverflow