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

### Registering the Windows Class

Now that the Windows class is defined and stored in`winclass`, you must tell Windows about the new class. This is accomplished via the function `RegisterClassEx()`, which simply takes a pointer to the new class definition, like this:

    RegisterClassEx(&winclass);

> Warning: Notice that I’m not using the class name, which is `“WINCLASS1”` in the case of our example. For `RegisterClassEx()`, you must use the actual structure holding the class because at the point before the call to the function, Windows does not yet know of the existence of the new class. Get it?

Also, to be complete, there is the old `RegisterClass()` function, which is used to register a class based on the older structure `WNDCLASS`.

Once the class is registered, we are free to create the window with it. Let’s see how to do that, and then revisit the details of the event handler and main event loop to see what kind of processing needs to be done for a Windows application to work.

### Creating the Window

To create a window (or any window-like object), you use the `CreateWindow()` or `CreateWindowEx()` function. The latter is a little newer and supports an additional style parameter, so let’s use it. This is where the Windows class comes in, which we took so long to dissect piece by piece. When you create a window, you must supply the text name of the window class—which in this case is `“WINCLASS1”`. This is what identifies your Windows class and differentiates it from other classes, along with the built-in types like buttons, text boxes, etc.

Here’s the function prototype for `CreateWindowEx()`:

    HWND CreateWindowEx(

         DWORD dwExStyle,      // extended window style

         LPCTSTR lpClassName,  // pointer to registered class name

         LPCTSTR lpWindowName, // pointer to window name

         DWORD dwStyle,        // window style

         int x,                // horizontal position of window

         int y,                // vertical position of window

         int nWidth,           // window width

         int nHeight,          // window height

         HWND hWndParent,      // handle to parent or owner window

         HMENU hMenu,          // handle to menu, or child-window identifier

         HINSTANCE hInstance,  // handle to application instance

         LPVOID lpParam);      // pointer to window-creation data

If the function is successful, it returns a handle to the newly created window; otherwise, it returns `NULL`.

Most of the parameters are self-explanatory, but let’s cover them anyway:

  • `dwExStyle`—The extended styles flag is an advanced feature, and for most cases, you’ll set it to `NULL`. However, if you’re interested in all the possible values, take a look at the Win32 SDK Help—there are a lot of them. About the only one I ever use is `WS_EX_TOPMOST`, which makes the window stay on top.
  • `lpClassName`—This is the name of the class you want to create a window based on—for example, `“WINCLASS1”`.
  • `lpWindowName`—This is a null-terminated text string containing the title of the window—for example, `“My First Window”`.
  • `dwStyle`—This is the general window flag that describes what the window looks like and how it behaves—very important! See Table 2.10 for a list of some of the more popular values. Of course, you can logically `OR` these values together to get the various features you want.
  • `x,y`—This is the position of the upper left-hand corner of the window in pixel coordinates. If you don’t care, use `CW_USEDEFAULT` and Windows will decide.
  • `nWidth`, `nHeight`—This is the width and height of the window in pixels. If you don’t care, use `CW_USEDEFAULT` and Windows will decide.
  • `hWndParent`—This is the handle to the parent window, if there is one. Use `NULL` if there isn’t a parent, and then the desktop will be the parent.
  • `hMenu`—This is the handle to the menu to attach to the window. You’ll learn more on this in the next chapter. Use `NULL` for now.
  • `hInstance`—This is the instance of the application. Use `hinstance` from `WinMain()` here.
  • `lpParam`—Advanced. Set to `NULL`.

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.