Sometimes Following Code Doesn't work in program.
Current.Cursor = Cursors.WaitCursor;
//Do something
Current.Cursor = Cursors.Default;
So i Usually used
Current.Cursor = Cursors.WaitCursor;
Application.UseWaitCursor = true;
//Do something
Application.UseWaitCursor = false;
Current.Cursor = Cursors.Default;
but i hope to more good code for using so
i found it.
It Using WinAPI that definition code is it:
using System;
using System.Windows.Forms;
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);
}
And Usage is:
private void button1_Click(object sender, EventArgs e) {
using (new HourGlass()) {
// Do something that takes time...
System.Threading.Thread.Sleep(2000);
}
}
It works by the way it change default cursor if your mouse outside of form.
Comments
Post a Comment