Tricks of the Windows Game Programming Gurus:Windows GDI, Controls, and Last-Minute Gift Ideas

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

### Playing with Controls

Normally I wouldn’t cover Window controls in a game programming book, but because you might need to know how to make them for a tool and I got a lot of email begging me to add them to my new book, here ya go! But just a few!

Window child controls are really little windows themselves. Here’s a short list of some of the more popular child controls:

  • Static text boxes
  • Edit boxes
  • Buttons
  • List boxes
  • Scroll bars

In addition, there are a number of sub-button types, such as

  • Push buttons
  • Check boxes
  • Radio buttons

There are even further sub-types of each. Nevertheless, most complex window controls that you see are conglomerations of these basic types. For example, a file directory control is just a few list boxes, some text edit boxes, and some buttons. If you can work with the basic controls listed here, you can handle anything. Once you have mastered one, they’re all roughly the same, give or take a few details, so I’m just going to show you how to work with a few of the child controls, including buttons.

#### Buttons

There are a number of button types that Windows supports. If you’re reading this book, hopefully you have used Windows and are at least familiar with push buttons, check boxes, and radio buttons, so I’m not go into the details of each. Rather, I’m going to show you how to create any type of button you want and respond to messages sent from it. The rest is up to you. Let’s begin by taking a look at Table 4.5, which lists all the available button types.

Table 4.5 Button Styles

Value | Description
--- | ---

`BS_PUSHBUTTON` | Creates a push button that posts a `WM_COMMAND` message to the owner window when the user selects the button.
`BS_RADIOBUTTON` | Creates a small circle with text. By default, the text is displayed to the right of the circle.
`BS_CHECKBOX` | Creates a small empty check box with text. By default, the text is displayed to the right of the check box.
`BS_3STATE` | Creates a button that is the same as a check box, except that the box can be grayed as well as checked or unchecked.
`BS_AUTO3STATE` | Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it. The state cycles through checked, grayed, and unchecked.
`BS_AUTOCHECKBOX` | Creates a button that is the same as a check box, except that the check state automatically toggles between checked and unchecked each time the user selects the check box.
`BS_AUTORADIOBUTTON` | Creates a button that is the same as a radio button, except that when the user selects it, Windows automatically sets the button’s check state to checked and automatically sets the check state for all other buttons in the same group to unchecked.
`BS_OWNERDRAW` | Creates an owner-drawn button. The owner window receives a `WM_MEASUREITEM` message when the button is created and a `WM_DRAWITEM` message when a visual aspect of the button has changed.

To create a child control button you simply create a window using `“button”` as the class string along with one of the button styles in Table 4.5. Then, when the button is manipulated, it sends `WM_COMMAND` messages to your window, as shown in Figure 4.11. You process the `wparam` and `lparam` as usual to see what child control sent the message and what the message was.

Figure 4.11 Child window message passing.

Let’s begin by seeing the exact parameters you’ll need to send to `CreateWindowEx()` to create a child button control. First, you need to set the class name to `“button”`. Then you need to set the style flags to `WS_CHILD | WS_VISIBLE` logically `ORed` with a button style from Table 4.5. Then in the place where you would normally put the handle to the menu or `HMENU`, you send the ID you want to refer to the button with (of course you must cast it to a `HMENU`). That’s about it.

As an example, here’s how you would create a push button with ID equal to 100 and the text “Push Me” on it:

    CreateWindowEx(NULL,       // extended style

                  "button",  // class

                  "Push Me",  // text on button

                 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,

                10,10,       // initial x,y

                100,24,    // initial width, height

                main_window_handle,    // handle to parent

               (HMENU)(100),        // id of button, notice cast to HMENU

               hinstance,  // instance of this application

               NULL);      // extra creation parms

Simple, isn’t it? When you press the button, a `WM_COMMAND` message is sent to the `WinProc()` of the parent window with the following paramaterization:

`msg`: | `WM_COMMAND`
`LOWORD(wparam)`: | Child Window id
`HIWORD(wparam)`: | Notification Code
`lparam`: | Child Window Handle

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.