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 transparency mode of the text dictates how the characters will be drawn. Will the characters be stamped down with rectangular regions or drawn pixel by pixel as an overlay? Figure 3.18 illustrates transparency as it relates to printing. As you can see, when text is printed with transparency, it looks as if it was drawn right on top of the graphics. Without transparency, you can actually see that there is an opaque block surrounding each character, which obscures everything—very ugly.

Figure 3.18 Opaque and transparent text printing.

> Tip: Rendering without transparency is faster, so if you’re printing on a monochrome background and you can get away with it, do it!

Let’s take a look at the functions to set the foreground and background colors of text:

    COLORREF SetTextColor(HDC hdc, // handle of device context

     COLORREF Color); // foreground character color

    COLORREF SetBkColor(HDC hdc, // handle of device context

     COLORREF color); // background color

Both functions take the graphics device context (from a call to `GetDC()` or `BeginPaint()`) along with the color to use in `COLORREF` format. Once you set these colors, they stay in flux until you change them. In addition, when you do set the colors, each function returns the current value so you can restore the old one when you’re done or when your application exits.

You’re almost ready to print, but this new `COLORREF` type has to be dealt with—don’t you think? Okay, then! Here’s the definition of `COLORREF`:

    typedef struct tagCOLORREF

            {

            BYTE bRed;   // the red component

            BYTE bGreen; // the green component

            BYTE bBlue;  // the blue component

            BYTE bDummy; // unused

            } COLORREF;

So in memory, a `COLORREF` looks like `0x00bbggrr`. Remember, PCs are Little Endian—that is, low `BYTE` to high `BYTE`. To create a valid `COLORREF`, you can use the `RGB()` macro, like this:

    COLORREF red    = RGB(255,0,0);

    COLORREF yellow = RGB(0,255,255);

And so forth. While we’re looking at color descriptor structures, we might as well look at `PALETTEENTRY` because it is absolutely identical:

    typedef struct tagPALETTEENTRY

            {

            BYTE peRed;    // red bits

            BYTE peGreen;  // green bits

            BYTE peBlue;   // blue bits

            BYTE peFlags;  // control flags

            } PALETTEENTRY;

`peFlags` can take on the values in Table 3.3. In most cases you will use `PC_NOCOLLAPSE` and `PC_RESERVED`, but for now just know they exist. The interesting thing that I wanted to point out, though, is the similarity between `COLORREF`s and `PALETTEENTRY`s. They are identical except for the interpretation of the last `BYTE`. Hence, in many cases they’re interchangeable.

Table 3.3 `PALLETEENTRY` Flags

Value | Description
--- | ---

`PC_EXPLICIT` | Specifies that the low-order word of the logical palette entry designates a hardware palette index. Advanced.
`PC_NOCOLLAPSE` | Specifies that the color be placed in an unused entry in the system palette instead of being matched to an existing color in the system palette.
`PC_RESERVED` | Specifies that the logical palette entry be used for palette animation. This flag prevents other windows from matching colors to the palette entry because the color frequently changes. If an unused system-palette entry is available, the color is placed in that entry. Otherwise, the color is not available for animation.

Now you’re almost ready to print, but remember that there was the issue of transparency and how to set it. The function used to set the transparency mode is `SetBkMode()`, and here’s its prototype:

    int SetBkMode(HDC hdc,      // handle to device context

                  int iBkMode); // transparency mode

The function takes the graphics device context along with the new transparency mode to switch to, which can be either `TRANSPARENT` or `OPAQUE`. The function returns the old mode so you can save it for later restoration.

Now you’re ready to kick the tires and light the fires, big daddy. Here’s how you would print some text:

    COLORREF old_fcolor, // old foreground text color

             old_bcolor; // old background text color

    int old_tmode; // old text transparency mode

    // first get a graphics device context

    HDC hdc = GetDC(hwnd);

    // set the foreground color to green and save old one

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

    // set the background color to black and save old one

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

    // finally set the transparency mode to transparent

    old_tmode = SetBkMode(hdc, TRANSPARENT);

    // draw some text at (20,30)

    TextOut(hdc, 20,30, "Hello",strlen("Hello"));

    // now restore everything

    SetTextColor(hwnd, old_fcolor);

    SetBkColor(hwnd, old_bcolor);

    SetBkMode(hwnd, old_tmode);

    // release the device context

    ReleaseDC(hwnd, hdc);

Of course, there is no law that you have to restore the old values, but I did it here just to show you how. Also, the color and transparency settings are valid as long as you have the handle to the device context. Let’s say you wanted to draw some blue text in addition to the green text. You’d only have to change the text color to blue and then draw the text. You wouldn’t have to set all three values again.

For an example of printing text using the preceding technique, take a look at `DEMO3_5.CPP` and the executable `DEMO3_5.EXE`. The demo creates a display of randomly positioned text strings in different colors all over the screen, as shown in Figure 3.19.

Figure 3.19 Random text output from `DEMO3_5.EXE`.

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.