Tricks of the Windows Game Programming Gurus:The Windows Programming Model

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

And here is the prototype for `LoadCursor()` (along with Table 2.8, which contains the various system cursor identifiers):

    HCURSOR LoadCursor(HINSTANCE hInstance,// handle of application instance

        LPCTSTR lpCursorName); // name string or cursor resource identifier

Again, `hInstance` is the application instance of your `.EXE` that contains the resource data to extract a custom cursor by name with. However, you aren’t using this functionality yet and will set `hInstance` to `NULL` to allow default system cursors only.

`lpCursorName` identifies the resource name string or handle to the resource (which we aren’t using at this point), or is a constant that identifies one of the system defaults shown in Table 2.8.

Table 2.8 Values for `LoadCursor()`

Value | Description
--- | ---

`IDC_ARROW` | Standard arrow
`IDC_APPSTARTING` | Standard arrow and small hourglass
`IDC_CROSS` | Crosshair
`IDC_IBEAM` | Text I-beam
`IDC_NO` | Slashed circle
`IDC_SIZEALL` | Four-pointed arrow
`IDC_SIZENESW` | Double-pointed arrow pointing northeast and southwest
`IDC_SIZENS` | Double-pointed arrow pointing north and south
`IDC_SIZENWSE` | Double-pointed arrow pointing northwest and southeast
`IDC_SIZEWE` | Double-pointed arrow pointing west and east
`IDC_UPARROW` | Vertical arrow
`IDC_WAIT` | Hourglass

Now we’re cooking! We’re almost done—the remaining fields are a little more interesting. Let’s move on to `hbrBackground`.

Whenever a window is drawn or refreshed, at the very least, Windows will repaint the background of the window’s client area for you with a predefined color, or brush in Windows-speak. Hence, `hbrbackground` is a handle to the brush that you want the window to be refreshed with. Brushes, pens, colors, and graphics are all part of GDI—the Graphics Device Interface—and we will discuss them in detail in the next chapter. For now, I’m going to show you how to request a basic system brush to paint the window with. This is accomplished with the `GetStockObject()` function, as shown in the following line of code:

    winclass.hbrBackground = GetStockObject(WHITE_BRUSH);

`GetStockObject()` is a general function that retrieves a handle to one of the Windows stock brushes, pens, palettes, or fonts. `GetStockObject()` takes a single parameter indicating which one of these resources to load. Table 2.9 contains a list of possible stock objects for brushes and pens only.

Table 2.9 Stock Object Identifiers for `GetStockObject()`

Value | Description
--- | ---

`BLACK_BRUSH` | Black brush
`WHITE_BRUSH` | White brush
`GRAY_BRUSH` | Gray brush
`LTGRAY_BRUSH` | Light gray brush
`DKGRAY_BRUSH` | Dark gray brush
`HOLLOW_BRUSH` | Hollow brush
`NULL_BRUSH` | Null brush
`BLACK_PEN` | Black pen
`WHITE_PEN` | White pen
`NULL_PEN` | Null pen

The next field in the `WNDCLASS` structure is the `lpszMenuName`. This is a null-terminated ASCII string of the menu resource’s name to load and attach to the window. We will see how this works later in Chapter 3, “Advanced Windows Programming.” For now, we’ll just set it to `NULL`:

    winclass.lpszMenuName = NULL;  // the name of the menu to attach

As I mentioned a while ago, each Windows class represents a different type of window that your application can create. Classes are like templates, in a manner of speaking, but Windows needs some way to track and identify them. Therefore, the next field, `lpszClassName`, is for just that. This field is filled with a null-terminated string that contains a text identifier for your class. I personally like using identifiers like `“WINCLASS1”`, `“WINCLASS2”`, and so forth. It’s up to you, but it’s better to keep it simple, like this:

    winclass.lpszClassName = "WINCLASS1"; // the name of the class itself

After this assignment, you will refer to the new Windows class by its class name, `“WINCLASS1”`—kinda cool, huh?

Last but not least is the small application icon. This is a new addition to the Windows class `WNDCLASSEX` structure and wasn’t available in the older `WNDCLASS`. Basically, this handle points to the icon you want to display on your window’s title bar and on the Windows desktop taskbar. Usually you would load a custom resource, but for now let’s just use one of the standard Windows icons via `LoadIcon()`:

    winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // the handle of the

    small icon

That’s it. Now let’s take a look at the whole class definition at once:

    WNDCLASSEX winclass; // this will hold the class we create

    // first fill in the window class structure

    winclass.cbSize = sizeof(WNDCLASSEX);

    winclass.style     = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;

    winclass.lpfnWndProc = WindowProc;

    winclass.cbClsExtra  = 0;

    winclass.cbWndExtra  = 0;

    winclass.hInstance   = hinstance;

    winclass.hIcon       = LoadIcon(NULL, IDI_APPLICATION);

    winclass.hCursor     = LoadCursor(NULL, IDC_ARROW);

    winclass.hbrBackground    = GetStockObject(BLACK_BRUSH);

    Twinclass.lpszMenuName    = NULL;

    winclass.lpszClassName    = "WINCLASS1";

    winclass.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);

And of course, if you want to save some typing, you could initialize the structure on-the-fly like this:

    WNDCLASSEX winclass = {

                   winclass.cbSize = sizeof(WNDCLASSEX),

                   CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW,

                   WindowProc,

                   0,

                   0,

                   hinstance,

                   LoadIcon(NULL, IDI_APPLICATION),

                   LoadCursor(NULL, IDC_ARROW),

                   GetStockObject(BLACK_BRUSH),

                   NULL,

                   "WINCLASS1",

                   LoadIcon(NULL, IDI_APPLICATION)};

It saves typing!

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.