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

Seem reasonable? The only mystery is the notification code. Notification codes describe what happened to the button control and begin with `BN_`. Table 4.6 lists all the possible notification codes and values.

Table 4.6 Notification Codes for Buttons

Code | Value
--- | ---

`BN_CLICKED` | 0
`BN_PAINT` | 1
`BN_HLITE` | 2
`BN_UNHILITE` | 3
`BN_DISABLE` | 4
`BN_DOUBLECLICKED` | 5

The most important of the notification codes are of course `BN_CLICKED` and `BN_DOUBLECLICKED`. To process a button child control like a simple push button, you might do something like this in the `WM_COMMAND` event handler:

    // assume a child button was created with id 100

    case WM_COMMAND:

         {

         // test for id

         if (LOWORD(wparam) == 100)

         {

         // do whatever

         } // end if

         // process all other child controls, menus, etc.

         // we handled it

         return(0);

         } break;

As an example, take a look at `DEMO4_8.CPP`; it creates a list of all button types and then displays all the messages along with the `wparam` and `lparam` for each message as you click and manipulate the buttons. Figure 4.12 shows the program in action. By experimenting with it, you will get a much better idea of how button child controls work.

Figure 4.12 The `DEMO4_8.EXE` child control program.

If you play with `DEMO4_8.EXE`, you’ll quickly realize that although your `WinProc()` is sending messages indicating what the user (you) is doing to the controls, you don’t know how to change or manipulate the controls programmatically. In essence, many of the controls don’t seem to do anything when you click them. This is important, so let’s briefly cover it.

#### Sending Messages to Child Controls

Because child controls are windows, they can receive messages just like any other window. But because they are children of a parent the messages are sent onto the parent in the case of the `WM_COMMAND` message. However, it’s possible to send a child control (like a button) a message and it will itself process the message with its own default `WinProc()`. This is exactly how you change the state of any control—by sending messages to it.

In the case of buttons, there are a number of messages you can send button controls, using `SendMessage()` to change the state of the button and/or retrieve the state of the button. Remember that `SendMessage()` returns a value too. Here’s a list of some of the more interesting messages for use with the parameterizations of `wparam` and `lparam`.

Purpose: To simulate clicking the button.

`msg`: | `BM_CLICK`
`wparam`: | 0
`lparam`: | 0

Example:

    // this would make the button look like it was pressed

    SendMessage(hwndbutton, BM_CLICK,0,0);

Purpose: Used to set the check on a check box or radio button.

`msg`: | `BM_SETCHECK`
`wparam`: | `fCheck`
`lparam`: | 0

`fCheck` can be one of the following:

Value | Description
--- | ---

`BST_CHECKED` | Sets the button state to checked.
`BST_INDETERMINATE` | Sets the button state to grayed, indicating an indeterminate state. Use this value only if the button has the `BS_3STATE` or `BS_AUTO3STATE` style.
`BST_UNCHECKED` | Sets the button state to unchecked.

Example:

    // this would check a check button

    SendMessage(hwndbutton, BM_SETCHECK, BST_CHECKED, 0);

Purpose: Used to retrieve the current state of the button check. Possible return values are shown here.

`msg`: | `BM_GETCHECK`
`wparam`: | 0
`lparam`: | 0

Value | Description
--- | ---

`BST_CHECKED` | Button is checked.
`BST_INDETERMINATE` | Button is grayed, indicating an indeterminate state (applies only if the button has the `BS_3STATE` or `BS_AUTO3STATE` style).
`BST_UNCHECKED` | Button is unchecked.

Example:

    // this would get the check state of a checkbox

    if (SendMessage(hwndbutton,BM_GETCHECK,0,0) == BST_CHECKED)

       {

       // button is checked

       } // end if

    else

       {

       // button is not checked

       } // end else

Purpose: Used to highlight the button as if it were selected by the user.

`msg`: | `BM_SETSTATE`
`wparam`: | `fState`
`lparam`: | 0

Where `fState` is a `TRUE` for highlighted and `FALSE` otherwise.

Example:

    // this would highlight the button control

    SendMessage(hwndbutton, BM_SETSTATE, 1, 0);

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.