Click here to Skip to main content
12,399,471 members (48,690 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.3K views
Posted

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

You may also be interested in...

Comments and Discussions

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

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