在c# WinForms中不维护更改鼠标指针



我有一个只有一个按钮的WinForms应用程序。我创建这个应用程序是为了演示在一个更大的应用程序上发生了什么。

按钮将布尔值从true变为false,并设置鼠标指针。

private bool ChangeMouse = true;
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("CURSOR-TOP: " + System.Windows.Forms.Cursor.Current.ToString());
if (ChangeMouse)
{
ChangeMouse = false;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Cross;
}
else
{
ChangeMouse = true;
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
}
Console.WriteLine("CURSOR-BOTTOM: " + System.Windows.Forms.Cursor.Current.ToString());
Console.WriteLine("");
}

这是我得到的结果,当我点击按钮4次:

CURSOR-TOP: [Cursor: Default]
CURSOR-BOTTOM: [Cursor: Cross]
CURSOR-TOP: [Cursor: Default]
CURSOR-BOTTOM: [Cursor: Default]
CURSOR-TOP: [Cursor: Default]
CURSOR-BOTTOM: [Cursor: Cross]
CURSOR-TOP: [Cursor: Default]
CURSOR-BOTTOM: [Cursor: Default]

可以看到,Cursor - top的值始终是默认游标。为什么不维护对当前游标的更改?

this。必须使用游标而不是System.Windows.Forms.Cursor.Current.

为什么?我真的不知道。

这并没有真正回答你的问题,但是…

我一直在使用这段代码(我在大约10个桌面实用程序中使用它):

using System;
using System.Windows.Forms;
namespace MyAppsNamespace
{
public class WorkingCursor : IDisposable
{
private readonly Cursor _oldCursor;
public WorkingCursor()
{
_oldCursor = Cursor.Current;
Cursor.Current = Cursors.WaitCursor;
}
public void Dispose()
{
Cursor.Current = _oldCursor;
}
}
}

你这样消费它:

using (new WorkingCursor()) {
DoSomethingThatTakesAWhile();
}

它总是像魔法一样有效。不知道为什么会有这个问题

最新更新