Tricks of the Windows Game Programming Gurus:Advanced Windows Programming

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

Let’s take a look at `WM_ACTIVATE`, `WM_CLOSE`, `WM_SIZE`, and `WM_MOVE` and what they do. For each one of these messages, I’m going to list the message, `wparam`, `lparam`, and some comments, along with a short example `WinProc()` handler for the event.

Message: `WM_ACTIVATE`

Parameterization:

    fActive      = LOWORD(wParam);       // activation flag

    fMinimized   = (BOOL)HIWORD(wParam); // minimized flag

    hwndPrevious = (HWND)lParam;         // window handle

The `fActive` parameter basically defines what is happening to the window—that is, is the window being activated or deactivated? This information is stored in the low-order word of `wparam` and can take on the values shown in Table 3.5.

Table 3.5 The Activation Flags for `WM_ACTIVATE`

Value | Description
--- | ---

`WA_CLICKACTIVE` | Activated by a mouse click.
`WA_ACTIVE` | The window has been activated by some means other than the mouse, such as the keyboard interface.
`WA_INACTIVE` | The window is being deactivated.

The `fMinimized` variable simply indicates if the window was minimized. This is true if the variable is nonzero. Lastly, the `hwndPrevious` value identifies the window being activated or deactivated, depending on the value of the `fActive` parameter. If the value of `fActive` is `WA_INACTIVE`, `hwndPrevious` is the handle of the window being activated. If the value of `fActive` is `WA_ACTIVE` or `WA_CLICKACTIVE`, `hwndPrevious` is the handle of the window being deactivated. This handle can be `NULL`. That makes sense, huh?

In essence, you use the `WM_ACTIVATE` message if you want to know when your application is being activated or deactivated. This might be useful if your application keeps track of every time the user Alt+Tabs away or selects another application with the mouse. On the other hand, when your application is reactivated, maybe you want to play a sound or do something. Whatever, it’s up to you.

Here’s how you code when your application is being activated in the main `WinProc()`:

    case WM_ACTIVATE:

    {

    // test if window is being activated

    if (LOWORD(wparam)!=WA_INACTIVE)

       {

       // application is being activated

       } // end if

    else

       {

       // application is being deactivated

       } // end else

    } break;

Message: `WM_CLOSE`

Parameterization: None

The `WM_CLOSE` message is very cool. It is sent right before a `WM_DESTROY` and the following `WM_QUIT` are sent. The `WM_CLOSE` indicates that the user is trying to close your window. If you simply `return(0)` in your `WinProc()`,nothing will happen and the user won’t be able to close your window! Take a look at `DEMO3_7.CPP` and the executable `DEMO3_7.EXE` to see this in action. Try killing the application—you won’t be able to!

> Warning: Don’t panic when you can’t kill `DEMO3_7.EXE`. Simply press Ctrl+Alt+Del, and the Task Manager will come up. Then select and terminate the `DEMO3_7.EXE` application. It will cease to exist—just like service at electronics stores starting with “F” in Silicon Valley.

Here’s the coding of the empty `WM_CLOSE` handler in the `WinProc()` as coded in `DEMO3_7.CPP`:

    case WM_CLOSE:

        {

        // kill message, so no further WM_DESTROY is sent

        return(0);

    } break;

If making the user mad is your goal, the preceding code will do it. However, a better use of trapping the `WM_CLOSE` message might be to include a message box that confirms that the application is going to close or maybe do some housework. `DEMO3_8.CPP` and the executable take this route. When you try to close the window, a message box asks if you’re certain. The logic flow for this is shown in Figure 3.20.

Figure 3.20 The logic flow for `WM_CLOSE`.

Here’s the code from `DEMO3_8.CPP` that processes the `WM_CLOSE` message:

    case WM_CLOSE:

    {

    // display message box

    int result = MessageBox(hwnd,

        "Are you sure you want to close this application?",

                  "WM_CLOSE Message Processor",

                   MB_YESNO | MB_ICONQUESTION);

    // does the user want to close?

    if (result == IDYES)

       {

       // call default handler

       return (DefWindowProc(hwnd, msg, wparam, lparam));

       } // end if

    else // throw message away

       return(0);

    } break;

Cool, huh? Notice the call to the default message handler, `DefWindowProc()`. This occurs when the user answers Yes and you want the standard shutdown process to continue. If you knew how to, you could have sent a `WM_DESTROY` message instead, but since you haven’t learned how to send messages yet, you just called the default handler. Either way is fine, though.

Next, let’s take a look at the `WM_SIZE` message, which is an important message to process if you’ve written a windowed game and the user keeps resizing the view window!

Message: `WM_SIZE`

Parameterization:

    fwSizeType = wParam;         // resizing flag

    nWidth     = LOWORD(lParam); // width of client area

    nHeight    = HIWORD(lParam); // height of client area

The `fwSizeType` flag indicates what kind of resizing just occurred, as shown in Table 3.6, and the low and high word of `lParam` indicate the new window client dimensions.

Table 3.6 Resizing Flags for `WM_SIZE`

Value | Description
--- | ---

`SIZE_MAXHIDE` | Message is sent to all pop-up windows when some other window is maximized.
`SIZE_MAXIMIZED` | Window has been maximized.
`SIZE_MAXSHOW` | Message is sent to all pop-up windows when some other window has been restored to its former size.
`SIZE_MINIMIZED` | Window has been minimized.
`SIZE_RESTORED` | Window has been resized, but neither the `SIZE_MINIMIZED` nor `SIZE_MAXIMIZED` value applies.

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.