Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Using Cursor Resources

Cursor resources are almost identical to `ICON` resources. Cursor files are small bitmaps with the extension `.CUR` and can be created in most compiler IDEs or with separate image processing programs. Cursors are usually 32×32 with 16 colors, but they can be up to 64×64 with 256 colors and even animated!

Assuming that you have created a cursor file with your IDE or a separate paint program, the steps to add them to an `.RC` file and access them via your program are similar to the steps for `ICON`s. To define a cursor, use the `CURSOR` keyword in your `.RC` file.

Method 1—By string name:

    cursor_name CURSOR FILENAME.CUR

Examples:

    windowcursor CURSOR crosshair.cur

    MyCoolCursor CURSOR greenarrow.cur

or

Method 2—By integer ID:

    cursor_id CURSOR FILENAME.CUR

Examples:

    windowcursor CURSOR bluearrow.cur

    292 CURSOR redcross.cur

Of course, if you use symbolic IDs, you must create an `.H` file with the symbol’s defines.

Contents of `RESOURCES.H`:

    #define ID_CURSOR_CROSSHAIR  200 // these numbers are arbitrary

    #define ID_CURSOR_GREENARROW 201

Contents of `RESOURCES.RC`:

    #include "RESOURCES.H"

    // here are the icon defines, note the use of C++ comments

    ID_CURSOR_CROSSHAIR CURSOR crosshair.cur

    ID_CURSOR_GREENARROW CURSOR greenarrow.cur

And there isn’t any reason why a resource data file can’t exist in another directory. For example, the `greenarrow.cur` might exist in the root directory in a `CURSOR\` directory, like this:

    ID_CURSOR_GREENARROW CURSOR C:\CURSOR\greenarrow.cur

I have created a few cursor `.ICO` files for this chapter. Try looking at them with your IDE, or just open up the directory and Windows will show the bitmap of each one by its filename!

Now that you know how to add a `CURSOR` resource to an `.RC` file, here’s the code to load the resource from the application by string name only.

In an `.RC` file:

    CrossHair CURSOR crosshair.CUR

In program code:

    // Notice the use of hinstance instead of NULL.

    winclass.hCursor = LoadCursor(hinstance, "CrossHair");

And to load a cursor with a symbolic ID defined in an `.H` file, here’s what you would do:

In an `.H` file:

    #define ID_CROSSHAIR 200

In an `.RC` file:

    ID_CROSSHAIR CURSOR crosshair.CUR

In program code:

    // Notice the use of hinstance instead of NULL.

    winclass.hCursor = LoadCursor(hinstance, MAKEINTRESOURCE(ID_CROSSHAIR));

Again, you use the `MAKEINTRESOURCE()` macro to convert the symbolic integer ID into the form Windows wants.

All right, there’s one little detail that may not have crossed your mind. So far you have only messed with the Windows class icon and cursor. But is it possible to manipulate the window icon and cursor at the window level? For example, you might want to create two windows and make the cursor change in each one. To do this, you could use this `SetCursor()` function:

    HCURSOR SetCursor(HCURSOR hCursor);

Here, `hCursor` is the handle of the cursor retrieved by `LoadCursor()`. The only problem with this technique is that `SetCursor()` isn’t that smart, so your application must do the tracking and change the cursor as the mouse moves from window to window. Here’s an example of setting the cursor:

    // load the cursor somewhere maybe in the WM_CREATE

    HCURSOR hcrosshair = LoadCursor(hinstance, "CrossHair");

    // later in program code to change the cursor…

    SetCursor(hcrosshair);

For an example of both setting the window icon and the mouse cursor, take a look `DEMO3_1.CPP` on the CD-ROM. The following list contains excerpts of the important code sections that load the new icon and cursor:

    // include resources

    #include "DEMO3_1RES.H"

    .

    .

    // changes to the window class definition

    winclass.hIcon=

        LoadIcon(hinstance, MAKEINTRESOURCE(ICON_T3DX));

    winclass.hCursor =

        LoadCursor(hinstance, MAKEINTRESOURCE(CURSOR_CROSSHAIR));

    winclass.hIconSm = LoadIcon(hinstance, MAKEINTRESOURCE(ICON_T3DX));

Furthermore, the program uses the resource script named `DEMO3_1.RC` and the resource header named `DEMO3_1RES.H`.

Contents of `DEMO3_1RES.H`:

    #define ICON_T3DX             100

    #define CURSOR_CROSSHAIR      200

Contents of `DEMO3_1.RC`:

    #include "DEMO3_1RES.H"

    // note that this file has different types of resources

    ICON_T3DX        ICON   t3dx.ico

    CURSOR_CROSSHAIR CURSOR crosshair.cur

To build the application yourself, you’ll need the following:

  `DEMO3_1.CPP`—The main C/C++ file
  `DEMO3_1RES.H`—The header with the symbols defined in it
  `DEMO3_1.RC`—The resource script itself
  `T3DX.ICO`—The bitmap data for the icon
  `CROSSHAIR.CUR`—The bitmap data for the cursor

All these files should be in the same directory as your project. Otherwise, the compiler and linker will have trouble finding them. Once you create and run the program or use the precompiled `DEMO3_1.EXE`, you should see something like what’s shown in Figure 3.5. Pretty cool, huh?

Figure 3.5 The output of `DEMO3_1.EXE` with custom `ICON` and `CURSOR`.

As an experiment, try opening the `DEMO3_1.RC` file with your IDE. Figure 3.6 shows what VC++ 5.0 does when I do this. However, you may get different results with your particular compiler, so don’t tweak if it doesn’t look the same. Alas, there is one point I want to make about the IDE before moving on. As I said, you can use the IDE to create both the `.RC` and `.H` file, but you’ll have to read the manual on this yourself.

Figure 3.6 The results of opening the resource file `DEMO3_1.RC` in VC++ 5.0.

However, there is one problem with loading a handmade `.RC` file—if you save it with your IDE, it will undoubtedly be inflicted with a zillion comments, macros, `#defines`, and other garbage that Windows compilers like to see in `.RC` files. Thus, the moral of the story is that if you want to edit your handmade `.RC` files, do the editing by loading the `.RC` file as text. That way the compiler won’t try to load it as an `.RC`, but just as plain ASCII text.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.