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

Table 2.10 lists the various window flags settings.

Table 2.10 General Style Values for `dwStyle`

Style | Creates
--- | ---

`WS_POPUP` | A pop-up window.
`WS_OVERLAPPED` | An overlapped window, which has a title bar and a border. Same as the `WS_TILED` style.
`WS_OVERLAPPEDWINDOW` | An overlapped window with the `WS_OVERLAPPED`, `WS_CAPTION`, `WS_SYSMENU`, `WS_THICKFRAME`, `WS_MINIMIZEBOX`, and `WS_MAXIMIZEBOX` styles.
`WS_VISIBLE` | A window that is initially visible.
`WS_SYSMENU` | A window that has a window menu on its title bar. The `WS_CAPTION` style must also be specified.
`WS_BORDER` | A window that has a thin-line border.
`WS_CAPTION` | A window that has a title bar (includes the `WS_BORDER` style).
`WS_ICONIC` | A window that is initially minimized. Same as the `WS_MINIMIZE` style.
`WS_MAXIMIZE` | A window that is initially maximized.
`WS_MAXIMIZEBOX` | A window that has a Maximize button. Cannot be combined with the `WS_EX_CONTEXTHELP` style. The `WS_SYSMENU` style must also be specified.
`WS_MINIMIZE` | A window that is initially minimized. Same as the `WS_ICONIC` style.
`WS_MINIMIZEBOX` | A window that has a Minimize button. Cannot be combined with the `WS_EX_CONTEXTHELP` style. The `WS_SYSMENU` style must also be specified.
`WS_POPUPWINDOW` | A pop-up window with `WS_BORDER`, `WS_POPUP`, and `WS_SYSMENU` styles. The `WS_CAPTION` and `WS_POPUPWINDOW` styles must be combined to make the window menu visible.
`WS_SIZEBOX` | A window that has a sizing border. Same as the `WS_THICKFRAME` style.
`WS_HSCROLL` | A window that has a horizontal scrollbar.
`WS_VSCROLL` | A window that has a vertical scrollbar.

Note: I have highlighted commonly used values.

And here’s how you would create a basic overlapped window with the standard controls at position 0,0 with a size of 400,400 pixels:

    HWND hwnd; // window handle

    // create the window, bail if problem

    if (!(hwnd = CreateWindowEx(NULL, // extended style

                      "WINCLASS1",          // class

                      "Your Basic Window", // title

                      WS_OVERLAPPEDWINDOW | WS_VISIBLE,

                      0,0,     // initial x,y

                      400,400,  // initial width, height

                      NULL,     // handle to parent

                      NULL,     // handle to menu

                      hinstance,// instance of this application

                      NULL)))   // extra creation parms

    return(0);

Once the window has been created, it may or may not be visible. However, in this case, we added the style flag `WS_VISIBLE`, which does this automatically. If this flag isn’t added, use the following function call to manually display the window:

    // this shows the window

    ShowWindow(hwnd, ncmdshow);

Remember the `ncmdshow` parameter of `WinMain()`? This is where it comes in handy. Although here you’ve overridden it by adding `WS_VISIBLE`, you would normally send it as the parameter to `ShowWindow()`. The next thing that you might want to do is force Windows to update your window’s contents and generate a `WM_PAINT` message. This is accomplished with a call to `UpdateWindow()`:

    // this sends a WM_PAINT message to window and makes

    // sure the contents are refreshed

    UpdateWindow();

### The Event Handler

I don’t know about you, but I’m starting to get the hang of this Windows stuff! It’s not that bad. It’s like a mystery novel—except the mystery is figuring out what language the novel is written in! With that in mind, let’s tackle the main event handler, or at least take a first look at it. Remember, I mentioned that the event handler is a callback function called by Windows from the main event loop whenever an event occurs that your window must handle. Take a look at Figure 2.6 again to refresh your memory about the general data flow.

This event handler is written by you, and it handles as many (or as few) events as you want to take care of. The rest you can pass on to Windows and let it deal with them. Of course, keep that in mind that the more events and messages your application handles, the more functionality it will have.

Before we get into some code, though, let’s talk about some of the details of the event handler, exactly what it does, and how it works. First, for each Windows class that you create, you can have a separate event handler that I will refer to as Windows’ Procedure or simply WinProc from now on. The WinProc is sent messages from the main event loop as messages are received from the user or Windows and placed in the main event queue. That’s a mental tongue twister, so I’ll say it in another way…

As the user and Windows perform tasks, events and messages are generated that are for your window and/or other applications’ windows. All of these messages go into a queue, but the ones for your window are sent to your window’s own private queue. Then the main event loop retrieves these messages and sends them to your window’s WinProc to be processed.

There are literally hundreds of possible messages and variations, so we aren’t going to cover them all. Luckily, you only have to handle very few of them to get a Windows application up and running.

So in a nutshell, the main event loop feeds the WinProc with messages and events, and the WinProc does something with them. Hence, not only do you have to worry about the WinProc, but also the main event loop. We will get to this shortly; for now, assume that the WinProc is simply going to be fed messages.

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.