我有一個只有一個按鈕的 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 的值始終是默認游標。為什么不保持對當前游標的更改???
uj5u.com熱心網友回復:
必須使用 this.Cursor 而不是 System.Windows.Forms.Cursor.Current。
至于為什么?我真的不知道。
uj5u.com熱心網友回復:
這并不能真正回答您的問題,但是...
我一直使用此代碼(我可能在 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();
}
它總是像一個魅力。不知道你為什么會遇到這個問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348005.html
