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

If it isn’t in Table 4.7, you don’t need to know! As an example, here’s a cool way to create a window that’s as large as the screen display:

    // create the window

    if (!(hwnd = CreateWindowEx(NULL,     // extended style

                    WINDOW_CLASS_NAME,   // class

                    "Button Demo",       // title

                    WS_POPUP | WS_VISIBLE,

                    0,0,      // initial x,y

                    GetSystemMetrics(SM_CXSCREEN), // initial width

                    GetSystemMetrics(SM_CYSCREEN), // initial height

                    NULL,      // handle to parent

                    NULL,      // handle to menu

                    hinstance,    // instance of this application

                    NULL)))      // extra creation parms

                    return(0);

> Note: Notice the use of the `WS_POPUP` window style rather than the `WM_OVERLAPPEDWINDOW`. This creates a window without any borders or controls, resulting in a blank screen—the effect you would want for a full-screen game application.

As another example, you could use the following code to test for a mouse:

    if (GetSystemMetrics(SM_MOUSEPRESENT))

       {

       // there’s a mouse

       } // end if

    else

       {

       // no mouse

       } // end else

Finally, when you’re drawing text, you might want to know about the font that GDI is using—for example, how wide each character is and other related metrics. If you write some code to draw text and you know the font, you can position the text with some reasonable accuracy. The name of the function that retrieves text metrics is called `GetTextMetrics()`:

    BOOL GetTextMetrics(HDC hdc,  // handle of device context

       LPTEXTMETRIC lptm ); // address of text metrics structure

You may be wondering why the `hdc` is needed—it’s because you may have multiple dc’s with different fonts selected, so you have to tell the function which one to compute the metrics on. Smart little function! Anyway, `lptm` is a pointer to a `TEXTMETRIC` structure that is filled with the information. It looks like this:

    typedef struct tagTEXTMETRIC {

      LONG tmHeight;          // the height of the font

      LONG tmAscent;          // the ascent of the font

      LONG tmDescent;         // the descent of the font

      LONG tmInternalLeading; // the internal leading

      LONG tmExternalLeading; // the external leading

      LONG tmAveCharWidth;    // the average width

      LONG tmMaxCharWidth;    // the maximum width

      LONG tmWeight;          // the weight of the font

      LONG tmOverhang;        // the overhang of the font

      LONG tmDigitizedAspectX; // the designed for x-aspect

      LONG tmDigitizedAspectY; // the designed for y-aspect

      BCHAR tmFirstChar;   // first character font defines

      BCHAR tmLastChar;    // last character font defines

      BCHAR tmDefaultChar; // char used when desired not in set

      BCHAR tmBreakChar;   // the break character

      BYTE tmItalic;       // is this an italic font

      BYTE tmUnderlined;   // is this an underlined font

      BYTE tmStruckOut;    // is this a strikeout font

      BYTE tmPitchAndFamily; //family and tech,truetype..

      BYTE tmCharSet;        // what is the character set

    } TEXTMETRIC;

Because most of us haven’t worked with a printing press our whole lives, a number of these fields are meaningless, but I have highlighted the ones that should make some sense. Take a look at the following list of terms and refer to Figure 4.13; it might help with some of the terminology.

  • Height—This is the total height in pixels of the character.
  • Baseline—This is a reference point, usually the bottom of an uppercase character.
  • Ascent—This is the number of pixels from the baseline to the top of where an accent mark might be.
  • Descent—This is the number of pixels from the baseline to the bottom of lower case extensions.
  • Internal leading—This is the number of pixels to allow for accent marks.
  • External leading—This is the number of pixels to allow for other characters above the character, so they don’t run on top of each other.

Figure 4.13 The makeup of a character.

Here’s an example of how you would center some text:

    TEXTMETRIC tm; // holds the textmetric data

    // get the textmetrics

    GetTextMetrics(hdc,&tm);

    // used tm data to center a string given the horizontal width

    // assume width of window is WINDOW_WIDTH

    int x_pos = WINDOW_WIDTH -

        strlen("Center This String")*tm.tmAveCharWidth/2;

    // print the text at the centered position

    TextOut(hdc,x_pos,0,"Center This String",

            strlen("Center This String"));

No matter what the font size is, this code will always center it.

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.