INFO: Creating Lines with a Nonstandard Pattern

ID: Q34614


The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) 3.1
  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows NT, versions 3.5, 3.51, 4.0
    • Microsoft Windows 95


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 query words: 3.00 3.10 3.50 4.00 win16sdk

Keywords : kbNTOS350 kbNTOS351 kbNTOS400 kbWinOS95 kbSDKWin16
Version : WINDOWS:3.1,95; winnt:3.5,3.51,4.0
Platform : WINDOWS winnt
Issue type : kbinfo


Last Reviewed: June 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.