WinForm应用程序中的沙漏问题

Red*_*din 13 c# user-interface winforms hourglass

在我的程序中使用WinForm中的UI.我将光标设置为在ThreadPool中启动方法之前的沙漏.

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

Application.UseWaitCursor = true;
Run Code Online (Sandbox Code Playgroud)

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

Application.UseWaitCursor = false;
Run Code Online (Sandbox Code Playgroud)

我的问题是光标留在沙漏,直到我不移动鼠标.如果用户在不移动鼠标的情况下等待操作结束,则会有点令人不安.

有人可以帮帮我吗?

杰罗姆

Jak*_*eta 14

实际上,还有一种方法可以做到这一点,我在经过几个小时的研究后发现了这个问题.

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

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

/// <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;
    }
Run Code Online (Sandbox Code Playgroud)


260*_*986 12

还有一种方法:

Cursor.Current = Cursors.WaitCursor;
Run Code Online (Sandbox Code Playgroud)

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

Cursor.Current = Cursors.Default;
Run Code Online (Sandbox Code Playgroud)


cod*_*ger 6

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

有一点需要注意,如果你使用这种Control.Cursor = Cursors.WaitCursor方法是它通常使用如下:

this.Cursor = Cursors.WaitCursor

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

这可能会导致用户混淆.或者,当应用程序忙于执行其他工作时,如果用户继续处理其他内容,则可能会导致一些问题.