,net2.0新增了一个属性——UseWaitCursor,即让指定的控件显示漏斗光标,但大部分情况下,这个属性不能正常工作。
UseWaitCursor使用的方式如下:
this.UseWaitCursor=true; //显示漏斗光标
this.UseWaitCursor=false; //显示箭头光标
例如在按纽点击事件中,我们可以这样做:
private void button1_Click(object sender,EventArgs e)
{
this.UseWaitCursor=true;
代码段……
this.UseWaitCursor=false;
}
我们期望在点按纽时,光标变成漏斗,事件处理完毕后,光标恢复正常,上述代码没有任何错误,但实际执行时,效果却不尽人意,光标往往没有任何变化。
对代码稍做改动:
private void button1_Click(object sender,EventArgs e)
{
this.UseWaitCursor=true;
labelInfo.Text="处理中……";
this.Refresh();
代码段……
labelInfo.Text="处理完成。";
this.UseWaitCursor=false;
}
在这段代码中,UseWaitCursor正确地达到了我们想要的效果。也就是在设置UseWaitCursor属性为true后,同时对窗体是其它控件的外观、文字做了改动,并要求窗体重绘时,UseWaitCursor才能正确起到它所描述的作用。
这个问题困扰了我几天,因为对C#的了解还不够深,最初我是自己调用Windwos DLL函数SendMessage来解决这个问题,今天在微软的MSDN论坛中找到一段代码,虽然也是使用SendMessage函数,不过比我的就高明很多了。
下面是完整代码,可以作为公用类库在任何C#程序中调用。
public class HourGlass:IDisposable
{
public HourGlass()
{
Enabled = true;
}
public void Dispose()
{
Enabled = false;
}
public static bool Enabled
{
get { return Application.UseWaitCursor; }
set
{
if (value == Application.UseWaitCursor) return;
Application.UseWaitCursor = value;
Form f = Form.ActiveForm;
if (f != null && f.Handle != null) // Send WM_SETCURSOR
SendMessage(f.Handle, 0x20, f.Handle, (IntPtr)1);
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
调用示例:
private void button1_Click(object sender,EventArgs e)
{
using(new HourGlass())
{
代码段……
}
}
转载自:http://hi.baidu.com/yk%B1%B1%BC%AB%D0%C7/blog/item/94da36ecfa15f7c6b31cb18d.html