Partager via


Working with Pens (Windows CE 5.0)

Send Feedback

In Windows CE, a pen is a graphics object for drawing lines. Drawing applications use pens to draw freehand lines and straight lines. Computer-aided design (CAD) applications use pens to draw visible lines, section lines, center lines, and so on. Word processing and desktop publishing applications use pens to draw borders and rules. Spreadsheet applications use pens to designate trends in graphs and to outline bar graphs and pie charts.

Windows CE stock pens include the BLACK_PEN and the WHITE_PEN, which draw a solid, 1-pixel-wide line in their respective color, and the NULL_PEN, which does not draw. You obtain the stock pens with the GetStockObject function.

You call the CreatePen or CreatePenIndirect function to create a custom pen with a unique color, width, or pen style.

The following table shows the pen styles supported by Windows CE.

Pen style Description
PS_SOLID Draws a solid line.
PS_DASH Draws a dashed line.
PS_NULL Does not draw a line.

Windows CE supports wide pens and dashed pens, but does not support dotted pens, inside frame pens, geometric pens, pen endcap styles, or pens that are both wide and dashed.

You can create a pen with a unique color by storing the RGB value that specifies the color that you want in a COLORREF value and passing this value to the CreatePen or CreatePenIndirect function. In the case of CreatePenIndirect, the COLORREF value is incorporated into the LOGPEN structure, which is used by CreatePenIndirect.

**Note   **The wide pen requires significant GDI computation. To improve the performance of a handwriting application, use a standard size pen.

The following code example shows how to use pen functions.

Note   To make the following code example easier to read, error checking is not included. Do not use this code example in a release configuration unless you have modified it to include secure error handling.

#define NUMPT  200

HDC hDC;            // Handle to the display device context 
HPEN hPen,          // Handle to the new pen object  
     hOldPen;       // Handle to the old pen object 
RECT rect;          // A RECT structure that contains the window's 
                    // client area coordinates
int index,          
    iCBHeight;      // Command bar height
POINT ptAxis[2],    // Two dimensional POINT structure array
     ptSine[NUMPT]; // 200 dimensional POINT structure array
static COLORREF g_crColor[] = {
                        0x000000FF,0x0000FF00,0x00FF0000,0x0000FFFF,
                        0x00FF00FF,0x00FFFF00,0x00FFFFFF,0x00000080,
                        0x00008000,0x00800000,0x00008080,0x00800080,
                        0x00808000,0x00808080,0x000000FF,0x0000FF00,
                        0x00FF0000,0x0000FFFF,0x00FF00FF,0x00FFFF00};

// Retrieve a handle to a display device context for the client 
// area of the window (hwnd). 
if (!(hDC = GetDC (hwnd)))
  return;

// Retrieve the coordinates of the window's client area. 
GetClientRect (hwnd, &rect);

// Retrieve the height of the command bar in pixels. 
iCBHeight = CommandBar_Height (g_hwndCB);

// Assign the axis points coordinates in pixels.
ptAxis[0].x = 0;
ptAxis[0].y = iCBHeight + (rect.bottom - iCBHeight) / 2;
ptAxis[1].x = rect.right - 1;
ptAxis[1].y = ptAxis[0].y;

// Assign the sine wave points coordinates in pixels.
for (index = 0; index < NUMPT; ++index)
{
  ptSine[index].x = index * rect.right / NUMPT;
  ptSine[index].y = (long) (iCBHeight + \
                            (rect.bottom - iCBHeight) / 2 * \
                            (1 - sin (8 * 3.14159 * index / NUMPT)));
}

// Create a dash pen object and select it.
hPen = CreatePen (PS_DASH, 1, g_crColor[5]);
hOldPen = SelectObject (hDC, hPen);

// Draw a straight line connecting the two points.
Polyline (hDC, ptAxis, 2);

// Select the old pen back into the device context.
SelectObject (hDC, hOldPen);

// Delete the pen object and free all resources associated with it. 
DeleteObject (hPen);

// Create a solid pen object and select it.
hPen = CreatePen (PS_SOLID, 3, g_crColor[5]);
hOldPen = SelectObject (hDC, hPen);

// Draw a sine wave shaped polyline.
Polyline (hDC, ptSine, NUMPT);

// Select the old pen back into the device context.
SelectObject (hDC, hOldPen);

// Delete the pen object and free all resources associated with it. 
DeleteObject (hPen);

// Release the device context.
ReleaseDC (hwnd, hDC);

return;

See Also

Graphics Device Interface (GDI)

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.