Click here to Skip to main content
15,860,943 members
Articles / Programming Languages / C#
Tip/Trick

Simple Application Cursor Management

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Jan 2012CPOL 17.3K   3
Cursors
This simple class will assist you in managing cursors within an application. The class is self explanatory, however ensure you call the cursor reset each time in order to remain in sync.

C#
public static class ManageCursor
{

    public static System.Collections.Generic.Stack<System.Windows.Forms.Cursor> cursorStack = new Stack<System.Windows.Forms.Cursor>();
    public static System.Collections.Generic.Stack<IntPtr> controlStack = new Stack<IntPtr>();

    #region Cursor Management...

    public static void SetCursor(System.Windows.Forms.Form frm, System.Windows.Forms.Cursor cursor)
    {
        cursorStack.Push(cursor);
        controlStack.Push(frm.Handle);
        frm.Cursor = cursor;
    }
    public static void SetCursor(System.Windows.Forms.Control ctrl, System.Windows.Forms.Cursor cursor)
    {
        cursorStack.Push(cursor);
        controlStack.Push(ctrl.Handle);
        ctrl.Cursor = cursor;
    }

    public static void ResetCursor(System.Windows.Forms.Form frm)
    {
        if (cursorStack.Count == 0)
            frm.Cursor = System.Windows.Forms.Cursors.Default;
        else
        {
            cursorStack.Pop();
            controlStack.Pop();
            if (cursorStack.Count == 0)
                frm.Cursor = System.Windows.Forms.Cursors.Default;
            else if (controlStack.Peek() == frm.Handle)
                frm.Cursor = cursorStack.Peek();
            else
                frm.Cursor = System.Windows.Forms.Cursors.Default;
        }
    }
    public static void ResetCursor(System.Windows.Forms.Control ctrl)
    {
        if (cursorStack.Count == 0)
            ctrl.Cursor = System.Windows.Forms.Cursors.Default;
        else
        {
            cursorStack.Pop();
            controlStack.Pop();
            if (cursorStack.Count == 0)
                ctrl.Cursor = System.Windows.Forms.Cursors.Default;
            else if (controlStack.Peek() == ctrl.Handle)
                ctrl.Cursor = cursorStack.Peek();
            else
                ctrl.Cursor = System.Windows.Forms.Cursors.Default;
        }
    }

    public static void ClearCursors()
    {
        cursorStack.Clear();
    }

    #endregion

}


To implement is as simple as...

C#
ManageCursor.SetCursor(this, System.Windows.Forms.Cursors.WaitCursor);
                
//Do your stuf...
                 
ManageCursor.ResetCursor(this);



A small contribution, but a contribution never the less!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhy do we need three overloads of SetCursor? What about the ... Pin
SergeyT213-Jan-12 9:28
SergeyT213-Jan-12 9:28 
GeneralWhat if [//Do your stuf...] throws an exception? ResetCursor... Pin
SergeyT213-Jan-12 9:26
SergeyT213-Jan-12 9:26 
GeneralFantastic!! Pin
raananv27-Jan-12 11:40
raananv27-Jan-12 11:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.