(This is only a very brief overview of printing)

Printing is basically the same as drawing on screen: both are handled through the GDI (Graphic Device Interface) who translates your high level drawing commands to low level instructions according to the appropriate device drivers. This driver can be a printer driver, a display driver, fax driver or whatever.

Before you can start to draw anything you must first define on which device you will draw, ie you must get a handle to a device context (HDC). When you want to draw on screen you use the function hdc=GetDC(hwnd) and when you want to draw on a printer you use the function hdc=CreateDC(0,printername,0,lpInitData).
Nearly every GDI function has hdc as its first parameter so it is a very important handle. When you are finished drawing you must use ReleaseDC(hdc) because Windows can only create a limited amount of context handles simultaniously.

In **pseudo-code** your program will have a structure like this:

    TextOut(hdc,10,10,'this is page 1',14).
    TextOut(hdc,10,10,'this is page 2',14).

Everything between StartDoc and EndDoc is one spooler job, this is important for a multipage document because it will prevent other jobs from printing pages in between.
As I said, this is just pseudo-code. Click here for a working code example.


You will need to know the size of the paper, in pixels. This and other important data can be obtained from GetDeviceCaps and DeviceCapabilities.

Drawing functions

GDI has a huge amount of drawing functions. The most commonly used are:
* MoveTo(hdc,x,y) : moves the dc cursor to coordinate x,y
* LineTo(hdc,x,y) : draws a line from current position to x,y
* TextOut(hdc,x,y,text,lenght(text))
There are also several functions to manipulate text placement, see SetTextAlign. To write text in columns it is convenient to use the ExtTextOut function because it also has a rectangle parameter: the text will be clipped to the rectangle when needed and you can also easily ensure that text is centered or right-aligned to the rectangle.


You will need objects like pens, fonts, brushes, bitmaps. For example when you want to draw a red dotted line you will have to create a red dotted pen first using hRedpen=CreatePen(PS_DOT,1,RGB(255,0,0)).
But when you want to create a black solid line, you don't have to create one because it is already 'in stock'. GDI has several stock objects.
After you created one or more objects you must select one of them to make it the current object using SelectObject(hRedpen). Every created object must be manually destroyed too or else you will run out of GDI resources, using DeleteObject(hRedpen). An object can not be destroyed when it is currently selected so you may have to select a stock object first.