You usually want to display the cursor in your Compact Framework application when you are running a long process so the user of your application knows that there is something going on. Using the System.Windows.Forms.Cursor is pretty straight forward. To show the cursor you do a:
//Show Cursor
Cursor.Current = Cursors.WaitCursor;
Cursor.Show();
And to hide the cursor you can write the following:
//Hide the cursor
Cursor.Current = Cursors.Default;
Cursor.Hide();
This is not rocket science but what it can lead to (and I have seen it) is showing/hiding the cursor all over the place which makes for messy code.
By creating a new class called Cursor2 and implementing IDisposable we can use the using statement in our code and makes for much cleaner and nicer code. Here is the Cursor2 class implementation:
public class Cursor2 : IDisposable
{
private static int _refCount = 0;
public Cursor2()
{
if (_refCount == 0)
{
Cursor.Current = Cursors.WaitCursor;
Cursor.Show();
}
_refCount++;
}
public void Dispose()
{
_refCount--;
if (_refCount < 0)
_refCount = 0;
if (_refCount == 0)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
}
}
}
Now in your long running process you can use the following to show the cursor:
LongLoadingForm llf;
using (new OpenNETCF.Windows.Forms.Cursor2())
{
llf = new LongLoadingForm();
//Extra initialization done
}
llf.ShowDialog();
Once your form has completed loading the cursor will automatically hide because of the using statement. Result is nice clean code to maintain and if the Compact Framework cursor implementation changes or there are additions there is only one place you have to make the change.