Cursor Resources

A cursor, or pointer, is a small picture which can be used as a marker for the current mouse position. The picture, like an icon, has two parts: a normal picture, and a stencil or mask which indicates which parts of the cursor image are transparent. You can create cursors and include them in your programs as resources. This can be useful for providing visual feedback, such as the pointing hand that indicates the mouse is over a hyperlink in most web browsing software.

You create a cursor using a cursor editing program (sometimes graphics programs can create cursors) to create a cursor file (.cur). Note: I am planning to write up a simple cursor editor one of these days and put it up on the net, with source code of course, but if anyone knows of a good cursor editor (free or otherwise) please drop me an email.

After you have a cursor file you can use the following syntax to include the cursor in your resource script:

CURSOR_ID CURSOR "test.cur"

CURSOR_ID, in this example, is a preprocessor constant defined elsewhere (probably in an include file), you would substitute your own ID constants for cursors you create. Similarly, the cursor file is named test.cur in the example, but you would replace that with the name of the cursor you had created in your own resource scripts.

Once you have a cursor resource in your program you can use the LoadCursor function to retrieve it:

    HCURSOR hCursor = LoadCursor (hInstance, MAKEINTRESOURCE(CURSOR_ID));

Note that you can also use the LoadCursor function to get handles to standard Windows cursors included with the operating system:

    HCURSOR hCursorSystem = LoadCursor (NULL, nID);

where nID is one of the following (for example):

Once you have a cursor handle you can set the current cursor using SetCursor (don't forget to save the old one):

    HINSTANCE hInstance;
    HCURSOR hNewCursor, hPreviousCursor;

    hInstance = (HINSTANCE) GetModuleHandle (NULL); /* This program's instance handle. */

    hNewCursor = LoadCursor (hInstance, MAKEINTRESOURCE(ID_MY_CURSOR));
    hPreviousCursor = SetCursor (hNewCursor);

/* ... part of the code where the cursor is different ... */

    SetCursor (hPreviousCursor); /* Restore the cursor */

Of course, generally the setting of the cursor and restoration will be in different functions, and you will have to store the previous cursor handle in a static variable or (better) in a class member variable. Also, since the cursor is shared by many windows and programs, it is good programming practice only to set the cursor when you know your window has 'control' of it (possibly because you have used the SetCapture function).

Another use for cursor handles is setting the cursor for a window class. All windows of the given class will then have that cursor:

    HCURSOR hCursor = LoadCursor (hInstance, MAKEINTRESOURCE(ID_WINDOW_CURSOR));
    WNDCLASSEX wc;

/* ... initialize other members of wc ... */

    wc.hCursor = hCursor;

    RegisterClassEx (&wc); /* Register the window class */

The cursor identified by ID_WINDOW_CURSOR in the application's resource script will appear whenever the mouse pointer is over a window created with that window class.