I was fighting a bit to change my Add-In cursor to a wait cursor during execution process. First cycle goes smooth and from then on I could not find the change in the cursor. All I could realise is that the cursor is not gettin changed but in reality the cursor was getting changed to WAITCURSOR which was not reflecting in the screen.
The reason behind was Windows sends the window that contains the mouse cursor the WM_SETCURSOR message, giving it an opportunity to change the cursor shape. A control like TextBox takes advantage of that, changing the cursor into a I-bar. The Control.Cursor property determines what shape will be used.
Use this class to set the cursor
After Implementing the class above I was able to see my cursor change happening in the screen. Similar goes for Settin status message sometimes :)
Cheers
Happy coding
The reason behind was Windows sends the window that contains the mouse cursor the WM_SETCURSOR message, giving it an opportunity to change the cursor shape. A control like TextBox takes advantage of that, changing the cursor into a I-bar. The Control.Cursor property determines what shape will be used.
The Current.Cursor property changes the shape directly, without waiting for a WM_SETCURSOR response. In most cases, that shape is unlikely to survive for long. As soon as the user moves the mouse, WM_SETCURSOR changes it back to Control.Cursor.
The UseWaitCursor property was added in .NET 2.0 to make it easier to display an hourglass. Unfortunately,
it doesn't work very well. It requires a WM_SETCURSOR message to change the shape and that won't happen when you set the property to true and then do something that takes a while.
Use this class to set the cursor
public class UseWaitCursor : IDisposable
{
public UseWaitCursor()
{
Enabled = true;
}
#region IDisposable Members
public void Dispose()
{
Enabled = false;
}
public static bool Enabled
{
get { return MyAddinTool._ myDocWindow.UseWaitCursor; }
set
{
if (value.Equals(MyAddinTool._myDocWindow.UseWaitCursor)) return;
MyAddinTool._ myDocWindow.UseWaitCursor = value;
if (_myDocWindow.ActiveControl != null && MyAddinTool._ myDocWindow.ActiveControl.Handle != null)
SendMessage(MyAddinTool._ myDocWindow.ActiveControl.Handle, 0x20, MyAddinTool._ myDocWindow.ActiveControl.Handle, (IntPtr)1);
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wp, IntPtr lp);
#endregion
}
using (new UseWaitCursor())
{
//TODO all your code goes here or this ares says how long your curosr needs to
be in WAITCUROSR style.
}
If required, use Finally block to change your cursor back to default Tool's cursor.
After Implementing the class above I was able to see my cursor change happening in the screen. Similar goes for Settin status message sometimes :)
Cheers
Happy coding
No comments:
Post a Comment