C# 将游标更改为等待整个应用程序的游标
我制作了一个小的可重用的模态表单,它有一个标签(用于“请等待”消息)和一个后台工作程序。(我们称之为WaitForm) 表单在应用程序中是可重用的 当“Load”触发时,它将调用backgroundworker的DoWork事件(该事件被委派,以便在此表单上调用的任何代码都可以执行自己的操作) 当它运行时,我希望所有窗体都显示一个等待光标。因为此表单是模态的,所以等待光标只会在用户将鼠标悬停在WaitForm上时出现。如果将鼠标移到父窗体上,光标将变回默认箭头 我尝试了以下方法,包括单独使用和与其他人组合使用:C# 将游标更改为等待整个应用程序的游标,c#,winforms,cursor,C#,Winforms,Cursor,我制作了一个小的可重用的模态表单,它有一个标签(用于“请等待”消息)和一个后台工作程序。(我们称之为WaitForm) 表单在应用程序中是可重用的 当“Load”触发时,它将调用backgroundworker的DoWork事件(该事件被委派,以便在此表单上调用的任何代码都可以执行自己的操作) 当它运行时,我希望所有窗体都显示一个等待光标。因为此表单是模态的,所以等待光标只会在用户将鼠标悬停在WaitForm上时出现。如果将鼠标移到父窗体上,光标将变回默认箭头 我尝试了以下方法,包括单独使用和与
Application.UseWaitCursor = true;
this.Cursor = Cursors.WaitCursor;
this.Cursor.Current = Cursors.WaitCursor;
_Parent.Cursor = Cursors.WaitCursor; //I tried to pass the calling parent form as a parameter in the constructor of the "WaitForm" so that I can set its cursor.
WaitForm按预期工作。它显示并启动backgroundworker。唯一能让我咬牙切齿的就是光标。我是否遗漏了一些明显的东西?看起来像是一个限制,我认为这是“出于设计”,您可能希望使用Win32 API来覆盖标准的Winform行为
[DllImport("user32.dll")]
static extern IntPtr LoadCursorFromFile(string lpFileName);
[DllImport("user32.dll")]
static extern IntPtr SetCursor(IntPtr hCursor);
[DllImport("user32.dll")]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
private const uint OCR_NORMAL = 32512;
static Cursor ColoredCursor;
//=========设置窗口光标========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//=========设置窗口光标========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//=======设置表单光标========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//=======设置表单光标========================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//=======从图像设置窗体光标==============================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
//=======从图像设置窗体光标==================================
IntPtr cursor = LoadCursorFromFile("example.cur");
bool ret_val = SetSystemCursor(cursor, OCR_NORMAL);
IntPtr cursor = LoadCursorFromFile("example.cur");
ColoredCursor = new Cursor(cursor);
this.Cursor = ColoredCursor;
Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
参考:
有关其他示例,请参见此处:
对于用户控制器中的等待光标,我使用:
this.UseWaitCursor = true;
一种方法是在静态类中创建一个
列表
,每次实例化一个表单时将其添加到列表中,然后当您想要设置每个表单光标时,可以在列表上迭代。但问题是,当您想要一种模式的、单线程的方法时,为什么还要使用后台工作程序呢?就像后台工作人员的目的是释放应用程序,让用户在主线程上做其他不幸无法工作的事情。问题在于窗口是模态的。使用Application.UseWaitCursor正确设置了所有游标。这似乎只是一个带有模态窗体的windows行为。使用后台工作程序的目的是释放UI,而不管您是否希望用户能够做其他事情。我不希望UI的其余部分被冻结,即使用户无法对其执行任何操作。如果光标是WaitCursor
,那么释放UI有什么意义呢?这完全是误导和不直观的。主题是等待游标,而不是后台工作人员。请继续谈这个话题。