Microsoft Windows CE 3.0  

Working with Pens

This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

In Windows CE, a penis 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_PENand 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 GetStockObjectfunction.

You call the CreatePenor CreatePenIndirectfunction 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 COLORREFvalue and passing this value to the CreatePenor CreatePenIndirectfunction. In the case of CreatePenIndirect, the COLORREFvalue is incorporated into the LOGPENstructure, 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. This code example should not be used in a release configuration unless it has been modified 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[] = {
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;

 Last updated on Tuesday, May 18, 2004

© 2004 Microsoft Corporation. All rights reserved.