INF: Creating Lines with a Nonstandard Pattern

ID Number: Q34614

2.03 2.10 3.00 3.10

WINDOWS

Summary:

The Microsoft Windows graphical environment provides six predefined

pens for drawing dotted, dashed, and solid lines. However, an

application cannot draw fine gray lines, such as those on a Microsoft

Excel spreadsheet, with these pens. This article describes how to

create such lines.

More Information:

An application can use the LineDDA function to produce any type of

patterned line. Based on the endpoints of a line, LineDDA calculates

each point on the line and calls an application-defined callback

function for each point. The callback function is free to use the

calculated points in any manner desired. An application can draw a

gray line similar to those used in Excel by calling the the SetPixel

function in the callback function to draw every other point.

For example, the following code calculates all points on the line from

coordinates (30, 40) to (100, 100). Then it calls the function pointed

to by the lpfnLineProc variable with the points and the handle to a

device context (hDC) as parameters:

LineDDA(30, 40, 100, 100, lpfnLineProc, (LPSTR)hDC);

For more information on this function, see pages 4-272 and 4-273 of

the "Microsoft Windows Software Development Kit Reference, Volume 1"

for Windows 3.0 or pages 568 and 569 of the "Microsoft Windows

Software Development Kit: Programmer's Reference, Volume 2: Functions"

for Windows 3.1. Charles Petzold's book "Programming Windows 3"

(Microsoft Press, 1990) demonstrates using the LineDDA function in a

programming example on pages 593 through 598.

The following code fragment draws 50 random Excel-style lines. Note

that the LineProc function must be listed as an EXPORT in the module

definition (DEF) file:

case WM_PAINT:

{

HDC hDC;

int nIndex;

PAINTSTRUCT ps;

hDC = BeginPaint(hWnd, &ps);

for (nIndex = 0; nIndex < 50; nIndex++)

LineDDA(rand() / 110, rand() / 110, rand() / 110,

rand() / 110, lpfnLineProc, (LPSTR)hDC);

EndPaint(hWnd, &ps);

break;

}

void FAR PASCAL LineProc(x, y, lpData)

short x, y;

LPSTR lpData;

{

static short nTemp = 0;

if (nTemp == 1)

SetPixel((HDC)lpData, x, y, 0L);

nTemp = (nTemp + 1) % 2;

}

Additional reference words: 2.03 2.10 3.00 3.10 2.x