14

在我的带有 WinForm 用户界面的程序中。在 ThreadPool 中启动一个方法之前,我将光标设置为沙漏。

我在 UI 线程中设置光标的代码如下所示:

Application.UseWaitCursor = true;

方法完成后,我返回 UI 线程将光标设置为正常情况。

Application.UseWaitCursor = false;

我的问题是光标停留在沙漏上,直到我不移动鼠标。如果用户在操作结束时等待而不移动鼠标,这会有点令人不安。

任何人都可以帮助我吗?

杰罗姆

4

5 回答 5

17

实际上,还有另一种方法可以做到这一点,这是我在研究这个问题数小时后发现的。

不幸的是,这是一个黑客。

下面是我编写的处理该问题的方法。

/// <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;
    }
于 2011-01-05T22:37:56.090 回答
11

另一种方法:

Cursor.Current = Cursors.WaitCursor;

完成后,只需将光标变回:

Cursor.Current = Cursors.Default;
于 2010-06-28T09:45:48.203 回答
7

我无法重现这种行为?这对我来说可以。

如果您使用该Control.Cursor = Cursors.WaitCursor方法,需要注意的一件事是它通常像这样使用:

this.Cursor = Cursors.WaitCursor

然而,这似乎工作正常,this是指表单,因此如果用户将鼠标移动到不同的控件,例如 TextBox,则鼠标不会显示等待光标。

这可能会给用户带来困惑。或者如果用户在应用程序忙于做其他工作时继续做其他事情,可能会导致一些问题。

于 2010-06-28T07:35:35.427 回答
1

我的解决方案....

public class SetMouseCursor
{
    public static void Wait()
    {
        Application.UseWaitCursor = true;
        Cursor.Current = Cursors.WaitCursor;
    }

    public static void Default()
    {
        Application.UseWaitCursor = false;
        Cursor.Current = Cursors.Default;
    }
}
于 2021-02-16T21:10:52.567 回答
0

手动设置光标。我就是做这个的。

于 2010-06-28T07:20:32.930 回答