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

For an example of the preceding code in action, take a look at `DEMO3_14.CPP` and the executable `DEMO3_14.EXE` on the CD-ROM. The program implements the logic in the preceding code exactly. As an experiment, try changing the message to `WM_DESTROY` and using `PostMessage()`, too.

> Warning: Sending messages out of the main event loop can cause unforeseen problems. For example, in the preceding case, you’re killing the window out of the main event loop by sending a message directly to the `WinProc()` with `SendMessage()`. However, if you normally assume that the event handling is done in the main event loop, you might create an out-of-execution-order bug. This means that you assume that event B happens after event A, but in some cases event B happens before event A. Whammo! This is a typical problem when you’re sending messages, so make sure to think it out. `PostMessage()` is usually safer because it doesn’t leapfrog the event queue.

Finally, there is also a way to send your own custom messages called `WM_USER`. Simply send a message with `SendMessage()` or `PostMessage()`, using `WM_USER` as the message type. You can put whatever you want in the `wparam` and `lparam` values. For example, you might want to use the `WM_USER` message to create a number of virtual messages for a memory management system that you have. Take a look:

    // defines for memory manager

    #define ALLOC_MEM  0

    #define DEALLOC_MEM  1

    // send WM_USER message, use the lparam as amount of memory

    // and the wparam as the type of operation

    SendMessage(hwnd, WM_USER, ALLOC_MEM, 1000);

Then, in your `WinProc()`, you might have

    case WM_USER:

    {

    // what is the virtual message

    switch(wparam)

           {

           case ALLOC_MEM: { } break;

           case DEALLOC_MEM: { } break;

           // .. more messages

           } // end switch

    } break;

As you can see, you can encode whatever you want in the `wparam` and `lparam` and do something as stupid as I just did for this example, or something that is more interesting!

### Summary

Thank God! I never thought I would finish this chapter—did you? We covered resources, menus, input devices, GDIs, and messaging—wow! A good Windows treatise is about 3,000 pages, so you can see my dilemma. But I think we covered a lot of good material that’s useful. After the next chapter, you’ll know everything you need to work with Windows.

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.