Click here to Skip to main content
Click here to Skip to main content

Tagged as

Simple Application Cursor Management

, 15 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
 
    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...
 
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)

Share

About the Author

hellohello
Web Developer
South Africa South Africa
No Biography provided

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 16 Jan 2012
Article Copyright 2012 by hellohello
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid