Win32 GDI Plotting Points & Lines

How To Draw Basic Elements Using LineTo, MoveToEx and Arc Functions

© Guy Lecky-Thompson

Dec 8, 2008
Win32 GDI API tutorial article introducing LineTo, MoveToEx, ArcTo and the Arc function for drawing various lines on the device context in the currently selected pen.

Drawing lines is very useful in Win32 GDI programming, as it is the basis for many graphical representations - diagrams, pictures, and so on. Once a device context has been selected, along with a pen, the various functions can be invoked to build up the picture.

In many cases, the functions are available as either _To or _Ex, while the _To functions, like LineTo draw from the current point to a new one, and the _Ex functions return information about the last point used.

The To and Ex Functions

The _To and _Ex functions in Win32 GDI programming help the programmer in a variety of ways. They were introduced, in addition to the basic GDI programming API, and replace existing functions. In general, they do the following:

  • _To - i.e. LineTo uses the current point in the device context as the starting point for the line or shape;
  • _Ex - i.e. MoveToEx returns information about the previous position in the device context after having drawn the line or shape.

The reader will note that, in some cases, the _To and _Ex functions are combined, to the exclusion of one or the other. For example, there is only a LineTo function, and no LineToEx, but on the other hand, there is no MoveTo function, but there is a MoveToEx function.

The LineTo Function

This function simply draws a line from the current position (as set by MoveToEx for example) to the specified position. To draw a complete line, then, two function calls are required:

MoveToEx(hdc, 50, 50, NULL);

LineTo(hdc, 100, 200);

The MoveToEx function contains a NULL parameter to indicate that the x,y co-ordinates of the original location of the cursor should not be returned. If they are needed, then the last parameter must be a pointer to a POINT structure. The line is drawn using the currently selected pen. By changing the pen, different styles of line can be drawn.

Drawing straight lines is useful, but Arcs are even more so.

The Arc & ArcTo Functions

These are the most complex line drawing functions to understand, and the best way to do so is by writing code that uses them. However, the following should help the programmer understand how to use Win32 GDI API curve functions.

An Arc is a segment of an ellipse. It has a starting and ending point around the outside of the ellipse that are defined as the places at which imaginary lines stretching from the center of the ellipse intersect with the ellipse itself. So, understanding how to build an Arc requires that the programmer carries out three steps:

  • Define the bounding box for the ellipse
  • Determine the starting X,Y co-ordinate (outside the ellipse)
  • Determine the ending X,Y co-ordinate (outside the ellipse)

The two points for the starting and ending points of the arc must be outside the ellipse (i.e. on the bounding rectangle), otherwise the imaginary line stretching from the center to that point will not intersect the arc and the function will fail.

All of these are wrapped up into a single function call:

Arc( hdc, nTopLeftX, nTopLeftY, nBottomRightX, nBottomRightY, nStartX, nStartY, nEndX, nEndY);

Most of the above should be self-explanatory, with the additional note that if nStartX/Y and nEndX/Y are the same, then the result is an ellipse. Ellipses and Arcs are drawn counter-clockwise.

The ArcTo function is identical, except that the current position in the device context is updated, ready for other drawing commands.

With these basic functions in hand, almost any kind of graphical representation can be created. However, they are not fast enough for game programming, but have the added advantage that the same calls can be made for any GDI device context : be it a printer, meta-file, or the screen.


The copyright of the article Win32 GDI Plotting Points & Lines in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Win32 GDI Plotting Points & Lines in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo