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

The main point of any Windows program is to open a window. A window is nothing more than a workspace that displays information, such as text and graphics, that the user can interact with. To create a fully functional Windows program, you only have to do a few things:

  1. Create a Windows class.
  2. Create an event handler or WinProc.
  3. Register the Windows class with Windows.
  4. Create a window with the previously created Windows class.
  5. Create a main event loop that retrieves and dispatches Windows messages to the event handler.

Let’s take a look at each step in detail.

### The Windows Class

Windows is really an object-oriented OS, so a lot of concepts and procedures in Windows have their roots in C++. One of these concepts is Windows classes. Each window, control, list box, dialog box, gadget, and so forth in Windows is actually a window. What makes them all different is the class that defines them. A Windows class is a description of a window type that Windows can handle.

There are a number of predefined Window classes, such as buttons, list boxes, file selectors, and so on. However, you’re free to create your own Windows classes. In fact, you will create at least one Windows class for each application you write. Otherwise, your program would be rather boring. So you can think of a Windows class as a template for Windows to follow when drawing your window, as well as processing messages for it.

Two data structures are available to hold Windows class information: `WNDCLASS` and `WNDCLASSEX`. `WNDCLASS` is the older of the two and will probably be obsolete soon, so we will use the new “extended” version, `WNDCLASSEX`. The structures are very similar, and if you are interested, you can look up the old `WNDCLASS` in the Win32 Help. Anyway, let’s take a look at `WNDCLASSEX` as defined in the Windows header files:

    typedef struct _WNDCLASSEX

            {

            UINT    cbSize;        // size of this structure

            UINT    style;         // style flags

            WNDPROC lpfnWndProc;   // function pointer to handler

            int     cbClsExtra;    // extra class info

            int     cbWndExtra;    // extra window info

            HANDLE  hInstance;     // the instance of the application

            HICON   hIcon;         // the main icon

            HCURSOR hCursor;       // the cursor for the window

            HBRUSH  hbrBackground; // the background brush to paint the window

            LPCTSTR lpszMenuName;  // the name of the menu to attach

            LPCTSTR lpszClassName; // the name of the class itself

            HICON   hIconSm;       // the handle of the small icon

            } WNDCLASSEX;

So what you would do is create one of these structures and then fill in all the fields:

    WNDCLASSEX winclass; // a blank windows class

The first field, `cbSize`, is very important (even Petzold forgot this in Programming Windows 95). It is the size of the `WNDCLASSEX` structure itself. You might be wondering why the structure needs to know how big it is. That’s a good question. The reason is that if this structure is passed as a pointer, the receiver can always check the first field to decide how long the data chunk is at the very least. It’s like a precaution and a little helper info so other functions don’t have to compute the class size during runtime. Therefore, all you have to do is set it like this:

    winclass.cbSize = sizeof(WNDCLASSEX);

The next field contains the style information flags that describe the general properties of the window. There are a lot of these flags, so I’m not going to show them all. Suffice it to say that you can create any type of window with them. Table 2.6 shows a good working subset of the possible flags. You can logically `OR` these values together to derive the type of window you want.

Table 2.6 Style Flags for Window Classes

Flag | Description
--- | ---

`CS_HREDRAW` | Redraws the entire window if a movement or size adjustment changes the width of the window.
`CS_VREDRAW` | Redraws the entire window if a movement or size adjustment changes the height of the window.
`CS_OWNDC` | Allocates a unique device context for each window in the class (more on this later in the chapter).
`CS_DBLCLKS` | Sends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is in a window belonging to the class.
`CS_PARENTDC` | Sets the clipping region of the child window to that of the parent window so that the child can draw on the parent.
`CS_SAVEBITS` | Saves the client image in a window so you don’t have to redraw it every time the window is obscured, moved, etc. However, this takes up more memory and is slower that doing it yourself.
`CS_NOCLOSE` | Disables the Close command on the system menu.

Note: The most commonly used flags are highlighted.

Table 2.6 contains a lot of flags, and I can’t blame you if you’re confused. For now, though, just set the style flags to indicate that you want the window to be redrawn if it is moved or resized, and you want a static device context along with the ability to handle double-click events.

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.