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

#### Working with Pens

The handle to a pen is called `HPEN`. Here’s how you would create a `NULL` pen.

    HPEN pen_1 = NULL;

`pen_1` is just a handle to a pen, but `pen_1` hasn’t been filled in or defined yet with the desired information. This operation is accomplished in one of two ways:

  • Using a stock object
  • Creating a user-defined pen

Remember, stock objects, or stock anything, are just objects that Windows has a few default types for to get you started. In the case of pens, there are a couple of pen types already defined, but they are very limited. You can use the `GetStockObject()` function shown in the following line to retrieve a number of different object handles, including pen handles, brushes, and fonts.

    HGDIOBJ GetStockObject(int fnObject); // type of stock object

The function simply takes the type of stock object you desire and returns a handle to it. The types of pens that are pre-defined stock objects are shown in Table 4.1.

Table 4.1 Stock Object Types

Value | Description
--- | ---

`BLACK_PEN` | Black pen.
`NULL_PEN` | Null pen.
`WHITE_PEN` | White pen.
`BLACK_BRUSH` | Black brush.
`DKGRAY_BRUSH` | Dark gray brush.
`GRAY_BRUSH` | Gray brush.
`HOLLOW_BRUSH` | Hollow brush (equivalent to `NULL_BRUSH`).
`LTGRAY_BRUSH` | Light gray brush.
`NULL_BRUSH` | Null brush (equivalent to `HOLLOW_BRUSH`).
`WHITE_BRUSH` | White brush.
`ANSI_FIXED_FONT` | Standard Windows fixed-pitch (monospace) system font.
`ANSI_VAR_FONT` | Standard Windows variable-pitch (proportional space) system font.
`DEFAULT_GUI_FONT` | Windows 95 only: Default font for user interface objects such as menus and dialog boxes.
`OEM_FIXED_FONT` | Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.
`SYSTEM_FONT` | The system font. By default, Windows uses the system font to draw menus, dialog box controls, and text. In Windows versions 3.0 and later, the system font is a proportionally spaced font; earlier versions of Windows used a monospace system font.
`SYSTEM_FIXED_FONT` | Fixed-pitch (monospace) system font used in Windows versions earlier than 3.0. This stock object is provided for compatibility with earlier versions of Windows.

As you can see from Table 4.1 there aren’t a whole lot of pens to select from (that’s a little GDI humor—get it?). Anyway, here’s an example of how you would create a white pen:

    HPEN white_pen = NULL;

    white_pen = GetStockObject(WHITE_PEN);

Of course, GDI knows nothing about `white_pen` because it hasn’t been selected into the graphics device context, but we’re getting there.

A more interesting method of creating pens is to create them yourself by defining their color, line style, and width in pixels. The function used to create a pen is called `CreatePen()` and is shown here:

    HPEN CreatePen(int fnPenStyle,    // style of the pen

                   int nWidth,        // width of pen in pixels

                   COLORREF crColor); // color of pen

The `nWidth` and `crColor` parameters are easy enough to understand, but the `fnPenStyle` needs a little explanation.

In most cases you probably want to draw solid lines, but in some cases you might need a dashed line to represent something in a charting program. You could draw a number of lines all separated by a little space to make a dashed line, but why not let GDI do it for you? The line style facilitates this functionality. GDI logically `AND`s or masks a line style filter as it’s rendering lines. This way, you can draw lines that are composed of dots and dashes, or solid pixels, or whatever one-dimensional entity you want. Table 4.2 contains the valid line styles that you can choose from.

Table 4.2 Line Styles for `CreatePen()`

Style | Description
--- | ---

`PS_NULL` | Pen is invisible.
`PS_SOLID` | Pen is solid.
`PS_DASH` | Pen is dashed.
`PS_DOT` | Pen is dotted.
`PS_DASHDOT` | Pen has alternating dashes and dots.
`PS_DASHDOTDOT` | Pen has alternating dashes and double dots.

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.