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

The following is an excerpt from the program’s `WinMain()`, where all the action takes place:

    // get the dc and hold it

    HDC hdc = GetDC(hwnd);

    // enter main event loop, but this time we use PeekMessage()

    // instead of GetMessage() to retrieve messages

    while(TRUE)

        {

        // test if there is a message in queue, if so get it

        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))

           {

           // test if this is a quit

           if (msg.message == WM_QUIT)

               break;

           // translate any accelerator keys

           TranslateMessage(&msg);

           // send the message to the window proc

           DispatchMessage(&msg);

           } // end if

        // main game processing goes here

        // set the foreground color to random

        SetTextColor(hdc, RGB(rand()%256,rand()%256,rand()%256));

        // set the background color to black

        SetBkColor(hdc, RGB(0,0,0));

        // finally set the transparency mode to transparent

        SetBkMode(hdc, TRANSPARENT);

        // draw some text at a random location

        TextOut(hdc,rand()%400,rand()%400,

       "GDI Text Demo!", strlen("GDI Text Demo!"));

        } // end while

    // release the dc

    ReleaseDC(hwnd,hdc);

As a second example of printing text, let’s try doing something like updating a counter in response to the `WM_PAINT` message. Here’s the code to do that:

    char buffer[80] // used to print string

    static int wm_paint_count = 0; // track number of msg’s

        case WM_PAINT:

        {

        // simply validate the window

        hdc = BeginPaint(hwnd,&ps);

           // set the foreground color to blue

           SetTextColor(hdc, RGB(0,0,255));

           // set the background color to black

           SetBkColor(hdc, RGB(0,0,0));

           // finally set the transparency mode to transparent

           SetBkMode(hdc, OPAQUE);

           // draw some text at (0,0) reflecting number of times

           // wm_paint has been called

           sprintf(buffer,"WM_PAINT called %d times ", ++wm_paint_count);

           TextOut(hdc, 0,0, buffer, strlen(buffer));

           EndPaint(hwnd,&ps);

           // return success

        return(0);

        } break;

Take a look at `DEMO3_6.CPP` and the executable `DEMO3_6.EXE` on the CD-ROM to see the program in action. Notice that nothing will print until you move or overwrite the window. This is because `WM_PAINT` is generated only when there is some reason to restore or redraw the window, such as a movement or resize.

That’s about it for basic printing. Of course, the `DrawText()` function does a lot more, but that’s up to you. Also, you might want to look into fonts and that whole can of worms, but stuff like that is normally for full Windows GUI programming and is not really what we’re trying to do in this book.

### Handling Important Events

As you’ve been painfully learning, Windows is an event-based operating system. Responding to events is one of the most important aspects of a standard Windows program. This next section covers some of the more important events that have to do with window manipulation, input devices, and timing. If you can handle these basic events, you’ll have more than you need in your Windows arsenal to handle anything that might come up as part of a DirectX game, which itself relies very little on events and the Windows operating system.

#### Window Manipulation

There are a number of messages that Windows sends to notify you that the user has manipulated your window. Table 3.4 contains a small list of some of the more interesting manipulation messages that Windows generates.

Table 3.4 Window Manipulation Messages

Value | Description
--- | ---

`WM_ACTIVATE` | Sent when a window is being activated or deactivated. This message is sent first to the window procedure of the top-level window being deactivated. It is then sent to the window procedure of the top-level window being activated.
`WM_ACTIVATEAPP` | Sent when a window belonging to an application other than the active window is about to be activated. The message is sent both to the application whose window is being activated and to the application whose window is being deactivated.
`WM_CLOSE` | Sent as a signal that a window or an application should terminate.
`WM_MOVE` | Sent after a window has been moved.
`WM_MOVING` | Sent to a window that the user is moving. By processing this message, an application can monitor the size and position of the drag rectangle and, if needed, change its size or position.
`WM_SIZE` | Sent to a window after its size has changed.
`WM_SIZING` | Sent to a window that the user is resizing. By processing this message, an application can monitor the size and position of the resizing rectangle and, if needed, change its size or position.

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.