Thread: Restoring the Cursor

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    39

    Question Restoring the Cursor

    Hi all,

    Can somebody suggest me, how after having used the SetSystemCursor() to change a default cursor, can I get the original cursor back?


    Code:
    hblank = CreateCursor( NULL, 0, 0, 32, 32, AndMask, XorMask );
    hblanktemp = CopyCursor(hblank);
    SetSystemCursor(hblanktemp,OCR_NORMAL);
    Here, I am over writting the arrow cursor with a custom made 'blank' cursor. Now what can I do to restore the arrow cursor?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You'll need to save the original.
    LoadCursor

    gg

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    39

    not sure...

    The funny thing about LoadCursor is that it actually acquires the 'handle' of the cursor. So if you manage to change the cursor itself using SetSystemCursor(), then there is no way you can get it back.
    Infact i tried a new technique by using GetIconInfo() to capture entire information of the cursor and then restoring it back using SetSystemcursor.
    But now I face a new problem.
    It cannot restore animated cursors

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Have you tried making a copy of the original?
    CopyCursor

    http://msdn.microsoft.com/library/de...es/cursors.asp

    gg

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    39

    Unhappy

    Yes, I did use CopyCursor, as it is stated in MSDN that SetSystemCursor deletes the handle of the source. But even that didn't work (surprisingly!). That is how I came to this conclusion that copycursor only takes a copy of the handle (something like taking a copy of a pointer and not the actualy data).

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Works fine for me on my Win2K box...
    Code:
    #define OEMRESOURCE
    #include <windows.h>
    
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    int main()
    {
        // load a copy of the wait cursor
        HCURSOR hcWait = LoadCursor(0, IDC_WAIT);
        assert(hcWait);
        HCURSOR hcWaitCopy = CopyCursor(hcWait);    
        assert(hcWaitCopy);
    
        // load a copy of the arrow cursor (so we can restore it)
        HCURSOR hcArrow = LoadCursor(0, IDC_ARROW);
        assert(hcArrow);
        HCURSOR hcArrowCopy = CopyCursor(hcArrow);    
        assert(hcArrowCopy);
    
        // make the system arrow cursor the wait cursor
        // NOTE: IDC_ARROW == MAKEINTRESOURCE(OCR_NORMAL)
        cout << "Changing normal cursor to wait cursor" << endl;
        BOOL ret = SetSystemCursor(hcWaitCopy, OCR_NORMAL);
        assert(ret);
    
        // sleep for a while so the user can play with the cursor
        cout << "Sleeping for 15 seconds" << endl;
        Sleep(15 * 1000);
    
        // restore the arrow cursor
        ret = SetSystemCursor(hcArrowCopy, OCR_NORMAL);
        assert(ret);
    
        DestroyCursor(hcWait);
        DestroyCursor(hcArrow);
    
        cout << "Cursors restored, sleeping for 10 seconds" << endl;
        Sleep(10 * 1000);
    
        cout << "Done" << endl;
    
        return 0;
    }//main
    gg
    Last edited by Codeplug; 06-09-2005 at 08:35 AM.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    39

    Lightbulb

    hmm...it works fine. Will try it out in my code... although my method also gives the same results.
    But there was something I was really hoping that this code would be able do....to restore an animated cursor, but it doesn't
    I use the 'Variations (system scheme)' theme of cursors and noticed that the animations in the arrow cursor was not restored.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    When in doubt - go back and read the manual...
    Quote Originally Posted by MSDN
    Do not use the CopyCursor function for animated cursors. Instead, use the CopyImage function.
    But that's not all.......On my Win2K box, I could not load an animated cursor using the OCR_xxx constants. Didn't matter if I used LoadCursor() or LoadImage().

    It looks like the only way to get a handle to an animated cursor is to load it from file. You can get the path to the the system cursor files by looking in the registry under: "HKCU\Control Panel\Cursors\"

    gg

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You can try this code - it may work as expected under XP:
    Code:
    #define OEMRESOURCE
    #include <windows.h>
    
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    
    HANDLE LoadNoShareCursor(UINT ocr_id)
    {
        HANDLE tmp = LoadImage(0, MAKEINTRESOURCE(ocr_id), IMAGE_CURSOR, 
                               0, 0, LR_SHARED);
        if (!tmp)
            return 0;
    
        return CopyImage(tmp, IMAGE_CURSOR, 0, 0, 0);
    }//LoadNoShareCursor
    
    
    int main()
    {
        HANDLE hWait = LoadNoShareCursor(OCR_WAIT);
        assert(hWait);
    
        HANDLE hArrow = LoadNoShareCursor(OCR_NORMAL);
        assert(hArrow);
        
        cout << "Changing normal cursor to wait cursor" << endl;
        BOOL ret = SetSystemCursor((HCURSOR)hWait, OCR_NORMAL);
        assert(ret);
    
        // sleep for a while so the user can play with the cursor
        cout << "Sleeping for 15 seconds" << endl;
        Sleep(15 * 1000);
    
        // restore the arrow cursor
        ret = SetSystemCursor((HCURSOR)hArrow, OCR_NORMAL);
        assert(ret);
    
        cout << "Cursors restored, sleeping for 10 seconds" << endl;
        Sleep(10 * 1000);
    
        cout << "Done" << endl;
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about cursor functionality
    By kantze in forum Windows Programming
    Replies: 4
    Last Post: 08-22-2008, 06:42 AM
  2. Custom Animated Cursor
    By willc0de4food in forum Windows Programming
    Replies: 3
    Last Post: 05-13-2005, 10:05 PM
  3. Screen Snapshot with Cursor
    By leojose in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2005, 05:35 AM
  4. cursor remains in openGL fullscreen
    By Ken Fitlike in forum Game Programming
    Replies: 5
    Last Post: 03-14-2002, 08:52 PM
  5. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 01:38 AM