fPRINTER


Quick Data

Header file:printer.h
Object name:fPRINTER
Object index:2 of 2 Objects (the other: fPRINTERDIALOG)

General Description

The fPRINTER object is an object that enables printing via a printer (as would seem strangely obvious). Printing involves selecting a printer and then drawing to it. Actually putting data out to a printer is just like drawing on any other graphics thing in Windows - and you can use the methods in _GRAPHICSBASE to draw to a printer.

Printing under Windows is one thing that is a thorough pain in the neck. Once you have it working, it's ok, but programming it to get it to work is a pain in the neck. This class is far from perfect, but it will do your average print job, and save you from some very nasty programming. I tested this class via a networked laser printer (my fathers Brother HL-1070 in the study, via some good old Coax network cable (must replace that sometime...)).

This component is derived from _GRAPHICSBASE, and inherits the properties from that.


Methods

The fPRINTER class has many, many methods, and directly documenting them would be detrimental to the purpose of this document - reference. So, for that reason, the methods are split up into seperate categories.

Getting the Names and information about the available printers

These following methods deal with obtaining information about the printers attached or available for use on this computer, and other information about the printers which may (or may not) be useful.


Prototype Description

int GetNumberPrinters(void); This function returns the number of printers installed on this computer.
char* GetDefaultPrinterName(void); This function returns the name of the default printer. A special note: this function will not work on Windows NT. Why not? Apparently Windows NT does not support the particular flag that returns the default printer. Still, you can use the name that this function returns later, and please do.
char* GetPrinterName(int Index); This function returns the name of a printer from the list of available printers. I would suggest that you first get the number of printers, and then go through and get all of the printer names. The first one is 0 (Zero), and the last one will be the number of printers minus 1.
int GetPrinterAttributes(int Index); This function returns extra attributes for the printer specified by Index. Index starts at 0 (Zero) and moves up to the number of printers minus 1. Attributes can be one or multiple values of the following (get the individual values by seperating them with the and (&) operator): (taken from the Win32 SDK Reference Manual)
  • PRINTER_ENUM_LOCAL - The function ignores the Name parameter, and enumerates the locally installed printers. Windows 95: The function will also enumerate network printers because they are handled by the local print provider.
  • PRINTER_ENUM_NAME - The function enumerates the printer identified by Name. This can be a server, a domain, or a print provider. If Name is NULL, the function enumerates available print providers.
  • PRINTER_ENUM_SHARED - The function enumerates printers that have the shared attribute. Cannot be used in isolation; use an OR operation to combine with another PRINTER_ENUM type.
  • PRINTER_ENUM_DEFAULT - Windows 95 only: The function returns information about the default printer.
  • PRINTER_ENUM_CONNECTIONS - Windows NT only: The function enumerates the list of printers to which the user has made previous connections.
  • PRINTER_ENUM_NETWORK - Windows NT only: The function enumerates network printers in the computer’s domain. This value is valid only if Level is 1.
  • PRINTER_ENUM_REMOTE - Windows NT only: The function enumerates network printers and print servers in the computer’s domain. This value is valid only if Level is 1.
Wherever there is a reference to levels of printers, please ignore it. The Windows development team seemed to think it was funny to make 4 'levels' of printers. Don't worry - you do not need to worry about this, as I have picked only the level that is relevant to anything anybody would want to do with a printer (which is level 5 - there is level 1, 2, 3, and 5. Don't look at me - I didn't develop Windows).
char* GetPrinterPortName(int Index); This function returns the name of the port that the printer is attached to. Again, this returns the port by index - 0 (Zero) is the first printer, and the last printer is the total number of printer, minus 1. The port returned will be something like "LPT1:".
int GetPrinterDataDeviceTimeout(int Index); This function returns the amount of time that Windows takes to attempt to select the printer before it times out and starts spitting chips. The returned time is in milliseconds. Index starts at 0 (Zero), and finishes at the number of printers minus 1.
int GetPrinterTransmissionRetryTimeout(int Index); This function returns the time that it will take for the communications on the printer to timeout. This means how long the printer can not communicate with the computer before the computer starts spitting chips and complaining about lack of communications. The relevance of this to you? Nothing, so far as I can see. The value returned is in milliseconds. Index starts at 0 (Zero), and finishes at the total number of printers minus 1.

Methods to start a print job

The following set of methods deal with setting up a print job and getting it to the printer, and also with things like managing whether the job is paused or restarted.


Prototype Description

void StartPrintJob(char* Name, char* Printer, char* OutputFile); This function begins a print job, and quickly. All that you need to pass along is the name of the job (a name that will show up in the list of documents going to the printer), the printer (a name that you might have got from the functions above for getting the printer names), and the output file, if you are printing to file. Printer can be a plain printer name - in the case of my system, I would print to "Brother HL-1070". If you don't want to print to file, pass NULL for OutputFile.
bool StartPrintJobDialog(HWND Handle, char* Name); This function displays a print dialog (a common dialog - it uses the fPRINTERDIALOG class), and upon obtaining the go-ahead from the dialog, goes ahead and sets up the print job ready to go. It's a one stop shop. However, you may wish to call the PreparePrinterDialog() function first (documented next), or you might have a few interesting surprises later.
void PreparePrinterDialog(void); This function allocates the memory and creates an instance of the fPRINTERDIALOG ready for the StartPrintJobDialog() function. Why should you do this? 1. Because the StartPrintJobDialog() function doesn't, and 2. because then you can go ahead and change the properties by getting a pointer to that printer dialog object with the next function...
fPRINTERDIALOG* GetPrinterDialogBox(void); This function returns a pointer to the fPRINTERDIALOG object which is created for use with the StartPrintJobDialog() function. You can use this pointer to set the parameters for the print dialog, through the interface for fPRINTERDIALOG. If you want to see an easy way to manipulate the dialog without having to save the pointer, see the example at the end.
void CleaupDialog(void); This function destroys the instance of the printer dialog which you created previously, for use with the StartPrintJobDialog() function. If you do not destroy your object, then you'll leak that memory.
void EndPrintJob(void); This function ends the print job which you previously started. This job is then sent to the printer and printed. The printing will not start until you have called this function.
void StartNewPage(void); This function tells the printer to start a new page. You should end the last page first, with the EndCurrentPage() function, documented below.
void EndCurrentPage(void); This function ends the current page, and tells the printer to finish with the current page. You will need to start a new page with the StartNewPage() function, as documented above.

Fiddling with the job

The next set of functions deal with fiddling with the job. This means canceling the job, pausing the job, resuming the job, getting information about the job, or other semi-useful things.


Prototype Description

void JobCancel(void); This function cancels the current job, should you wish to do that.
void JobPause(void); This function pauses the current job.
void JobResume(void); This function resumes a job that you previously paused.
void JobRestart(void); This function restarts a job. From the beginning.
char* GetJobPrinterName(void); This function returns the name of the printer that is currently doing the job that you sent to the printer.
char* GetJobUsername(void); This function returns the name of the user who has control of the job (typically the person logged onto the computer to which the printer is attached).
char* GetJobMachineName(void); This function returns the name of the machine that that created the print job.
char* GetJobDataType(void); This function returns the type of data being used to send the print job to the printer.
char* GetJobDocumentName(void); This function returns the name of the Job - the name that you set when you originally created the print job.
int GetJobID(void); This function returns the ID of the print job, which can be useful for extra API calls.
int GetPagesPrinted(void); This function returns the number of pages that the printer has physically printed.
int GetTotalPages(void); This function returns the number of pages that the printer has currently in it's printing queue.
int GetIntegerJobStatus(void); This function returns a number, which can mean one of several different things. According to the Win32 SDK Reference, the following values are returned by this function:
  • JOB_STATUS_DELETING
  • JOB_STATUS_ERROR
  • JOB_STATUS_OFFLINE
  • JOB_STATUS_PAPEROUT
  • JOB_STATUS_PAUSED
  • JOB_STATUS_PRINTED
  • JOB_STATUS_PRINTING
  • JOB_STATUS_SPOOLING
char* GetJobStatus(void); This function returns a string representation of the current job's status, which may be more friendly than the last function - well, more friendly to the user, not the programmer, anyway.
int GetJobPosition(void); This function returns the job's position in the print queue.
int GetJobPriority(void); This function returns the priority of the current print job. The return value can be one of the following (taken from the Win32 SDK Reference Manual):
  • MIN_PRIORITY - Minimum priority.
  • MAX_PRIORITY - Maximum priority.
  • DEF_PRIORITY - Default priority.
void SetJobDocumentName(char* DocumentName); This function sets the name of the current job.
void SetJobPosition(int Position); This function sets the position of the job in the print queue. Whilst it might be nice to bump your document to the top, it's not always a nice thing to do.
void SetJobPriority(int Priority); This function sets the priority of the current job (for whatever good it does). You can pass any of the following values for Priority (taken from the Win32 SDK Reference Manual):
  • MIN_PRIORITY - Minimum priority.
  • MAX_PRIORITY - Maximum priority.
  • DEF_PRIORITY - Default priority.

Getting and Setting the printer settings

This category of functions deal with getting the settings for the printer, and maybe even setting them. DPI, Page orientation, and others can be set here.

Please note that not all of these functions are 100% reliable. You can use a function until you are blue in the face, and maybe the effect intended by that function will not occur. If you do find a function that does not work, then please contact me, and bother me about it. I will then attempt to get it to work.


Prototype Description

int GetDriverVersion(void); This function returns the version of the printer driver that is currently being used.
char* GetDeviceName(void); This function returns the name of the device that is currently being used.
bool IsPortrait(void); This function returns TRUE if the paper is set to print in a portrait style, or FALSE if otherwise.
int GetPaperSize(void); This function returns the the size of the paper. The value returned corresponds to a type of paper, and the possible return values are listed below (taken from the Win32 SDK Reference Manual):
  • DMPAPER_LETTER - Letter, 8 1/2- by 11-inches
  • DMPAPER_LEGAL - Legal, 8 1/2- by 14-inches
  • DMPAPER_A4 - A4 Sheet, 210- by 297-millimeters
  • DMPAPER_CSHEET - C Sheet, 17- by 22-inches
  • DMPAPER_DSHEET - D Sheet, 22- by 34-inches
  • DMPAPER_ESHEET - E Sheet, 34- by 44-inches
  • DMPAPER_LETTERSMALL - Letter Small, 8 1/2- by 11-inches
  • DMPAPER_TABLOID - Tabloid, 11- by 17-inches
  • DMPAPER_LEDGER - Ledger, 17- by 11-inches
  • DMPAPER_STATEMENT - Statement, 5 1/2- by 8 1/2-inches
  • DMPAPER_EXECUTIVE - Executive, 7 1/4- by 10 1/2-inches
  • DMPAPER_A3 - A3 sheet, 297- by 420-millimeters
  • DMPAPER_A4SMALL - A4 small sheet, 210- by 297-millimeters
  • DMPAPER_A5 - A5 sheet, 148- by 210-millimeters
  • DMPAPER_B4 - B4 sheet, 250- by 354-millimeters
  • DMPAPER_B5 - B5 sheet, 182- by 257-millimeter paper
  • DMPAPER_FOLIO - Folio, 8 1/2- by 13-inch paper
  • DMPAPER_QUARTO - Quarto, 215- by 275-millimeter paper
  • DMPAPER_10X14 - 10- by 14-inch sheet
  • DMPAPER_11X17 - 11- by 17-inch sheet
  • DMPAPER_NOTE - Note, 8 1/2- by 11-inches
  • DMPAPER_ENV_9 - #9 Envelope, 3 7/8- by 8 7/8-inches
  • DMPAPER_ENV_10 - #10 Envelope, 4 1/8- by 9 1/2-inches
  • DMPAPER_ENV_11 - #11 Envelope, 4 1/2- by 10 3/8-inches
  • DMPAPER_ENV_12 - #12 Envelope, 4 3/4- by 11-inches
  • DMPAPER_ENV_14 - #14 Envelope, 5- by 11 1/2-inches
  • DMPAPER_ENV_DL - DL Envelope, 110- by 220-millimeters
  • DMPAPER_ENV_C5 - C5 Envelope, 162- by 229-millimeters
  • DMPAPER_ENV_C3 - C3 Envelope, 324- by 458-millimeters
  • DMPAPER_ENV_C4 - C4 Envelope, 229- by 324-millimeters
  • DMPAPER_ENV_C6 - C6 Envelope, 114- by 162-millimeters
  • DMPAPER_ENV_C65 - C65 Envelope, 114- by 229-millimeters
  • DMPAPER_ENV_B4 - B4 Envelope, 250- by 353-millimeters
  • DMPAPER_ENV_B5 - B5 Envelope, 176- by 250-millimeters
  • DMPAPER_ENV_B6 - B6 Envelope, 176- by 125-millimeters
  • DMPAPER_ENV_ITALY - Italy Envelope, 110- by 230-millimeters
  • DMPAPER_ENV_MONARCH - Monarch Envelope, 3 7/8- by 7 1/2-inches
  • DMPAPER_ENV_PERSONAL - 6 3/4 Envelope, 3 5/8- by 6 1/2-inches
  • DMPAPER_FANFOLD_US - US Std Fanfold, 14 7/8- by 11-inches
  • DMPAPER_FANFOLD_STD_GERMAN - German Std Fanfold, 8 1/2- by 12-inches
  • DMPAPER_FANFOLD_LGL_GERMAN - German Legal Fanfold, 8 1/2- by 13-inches
int GetPaperLength(void); This function returns the length of the current paper, in tenths of millimetres.
void GetPaperWidth(void); This function returns the width of the current paper, in tenths of millimetres.
int GetPrintScale(void); This function returns the scale of the printed data. You can set a scale for the printed data, which will scale up or down whatever you send to the printer. The actual scale will be the value returned divided by 100, to make a percentage. For example, it might be that 50 is returned - 50 divided by 100 makes 0.5, or 50%.
int GetNumberCopies(void); This function returns the number of copies that are currently set to print.
int GetPrintQuality(void); This function returns the Quality of the job that is currently printing. The value returned can either be a constant, or a DPI value. If it's a constant, it will be one of the following: (Taken from the Win32 SDK Reference Manual)
  • DMRES_HIGH
  • DMRES_MEDIUM
  • DMRES_LOW
  • DMRES_DRAFT
According to the SDK Reference, if the value returned is positive, then it is a DPI value for the current document. The constants defined above are all negative numbers, which should help you in using this function.
int GetYDPI(void); This function returns the Y-DPI of the printer. You can just accept this as being the plain old DPI, just this is the 'Y-Resolution', according to Windows.
int GetXDPI(void); This function returns the X-DPI of the printer. This function returns the same value as the GetPrintQuality() function, documented two above. If you want to see what this function returns, then check that out.
int GetPrinterPixelWidth(void); This function returns the Pixel or Unit width of the current page. This is the maximum number of units from one edge to the next edge of the page. Use this to determine where to draw things.
int GetPrinterPixelHeight(void); This function returns the height of the sheet of paper in whatever units that your paper is set up for. This is the number of units from the top of the page to the bottom of the page. I would suggest that you use this to obtain the size of the page for your uses.
int GetActualXDPI(void); This function returns the true X-DPI of the current device - yes, the real true one. You can beleive this value, because it is correct. Well, maybe not - it's only as correct as Windows allows...
int GetActualYDPI(void); This function is just like the above, except that it returns the Y-DPI of the device in question.
void SetPortrait(bool Set); Passing TRUE will make the page portrait, or passing FALSE will make the page Landscape. Anymore description needed?
void SetPaperSize(int Type); This function sets the size of the paper. There are a large set of constants defined just for this purpose, and they are: (taken from the Win32 SDK Reference Manual)
  • DMPAPER_LETTER - Letter, 8 1/2- by 11-inches
  • DMPAPER_LEGAL - Legal, 8 1/2- by 14-inches
  • DMPAPER_A4 - A4 Sheet, 210- by 297-millimeters
  • DMPAPER_CSHEET - C Sheet, 17- by 22-inches
  • DMPAPER_DSHEET - D Sheet, 22- by 34-inches
  • DMPAPER_ESHEET - E Sheet, 34- by 44-inches
  • DMPAPER_LETTERSMALL - Letter Small, 8 1/2- by 11-inches
  • DMPAPER_TABLOID - Tabloid, 11- by 17-inches
  • DMPAPER_LEDGER - Ledger, 17- by 11-inches
  • DMPAPER_STATEMENT - Statement, 5 1/2- by 8 1/2-inches
  • DMPAPER_EXECUTIVE - Executive, 7 1/4- by 10 1/2-inches
  • DMPAPER_A3 - A3 sheet, 297- by 420-millimeters
  • DMPAPER_A4SMALL - A4 small sheet, 210- by 297-millimeters
  • DMPAPER_A5 - A5 sheet, 148- by 210-millimeters
  • DMPAPER_B4 - B4 sheet, 250- by 354-millimeters
  • DMPAPER_B5 - B5 sheet, 182- by 257-millimeter paper
  • DMPAPER_FOLIO - Folio, 8 1/2- by 13-inch paper
  • DMPAPER_QUARTO - Quarto, 215- by 275-millimeter paper
  • DMPAPER_10X14 - 10- by 14-inch sheet
  • DMPAPER_11X17 - 11- by 17-inch sheet
  • DMPAPER_NOTE - Note, 8 1/2- by 11-inches
  • DMPAPER_ENV_9 - #9 Envelope, 3 7/8- by 8 7/8-inches
  • DMPAPER_ENV_10 - #10 Envelope, 4 1/8- by 9 1/2-inches
  • DMPAPER_ENV_11 - #11 Envelope, 4 1/2- by 10 3/8-inches
  • DMPAPER_ENV_12 - #12 Envelope, 4 3/4- by 11-inches
  • DMPAPER_ENV_14 - #14 Envelope, 5- by 11 1/2-inches
  • DMPAPER_ENV_DL - DL Envelope, 110- by 220-millimeters
  • DMPAPER_ENV_C5 - C5 Envelope, 162- by 229-millimeters
  • DMPAPER_ENV_C3 - C3 Envelope, 324- by 458-millimeters
  • DMPAPER_ENV_C4 - C4 Envelope, 229- by 324-millimeters
  • DMPAPER_ENV_C6 - C6 Envelope, 114- by 162-millimeters
  • DMPAPER_ENV_C65 - C65 Envelope, 114- by 229-millimeters
  • DMPAPER_ENV_B4 - B4 Envelope, 250- by 353-millimeters
  • DMPAPER_ENV_B5 - B5 Envelope, 176- by 250-millimeters
  • DMPAPER_ENV_B6 - B6 Envelope, 176- by 125-millimeters
  • DMPAPER_ENV_ITALY - Italy Envelope, 110- by 230-millimeters
  • DMPAPER_ENV_MONARCH - Monarch Envelope, 3 7/8- by 7 1/2-inches
  • DMPAPER_ENV_PERSONAL - 6 3/4 Envelope, 3 5/8- by 6 1/2-inches
  • DMPAPER_FANFOLD_US - US Std Fanfold, 14 7/8- by 11-inches
  • DMPAPER_FANFOLD_STD_GERMAN - German Std Fanfold, 8 1/2- by 12-inches
  • DMPAPER_FANFOLD_LGL_GERMAN - German Legal Fanfold, 8 1/2- by 13-inches
void SetPaperLength(int Length); Should you not wish to have a standard paper size, use this function to set the length to what you want. This size is in tenths of millimetres.
void SetPaperWidth(int Width); This function sets the width of the paper, if you don't want a standard size. The measurements for this should be in tenths of millimetres.
void SetPrintScale(int Scale); This function sets the scale of the printout. Ordinarily, the data placed on the printer will be printed out 1:1. However, through the wonders of Windows, you can scale that up or down without needing to manually perform the calculations for it. How do you do this? Simply pass a value to this function. A value of 100 would be normal size - a value of 50 would be half size - and a value of 200 would be twice the ordinary size. I think you can see a pattern emerging here...
void SetNumberCopies(int Copies); This function sets the number of copies that the printer is to print. Please, don't be nasty and set this too high...
void SetPrintQuality(int Quality); This function sets the quality of the printout. Before you just go passing numbers to this function, consider a few things. Firstly, this value will not always be positive, depending on the printer that you have. The value can be one of a few predefined constants: (taken from the Win32 SDK Reference)
  • DMRES_HIGH
  • DMRES_MEDIUM
  • DMRES_LOW
  • DMRES_DRAFT
Now, these constants are all negative. So what I'd do first is check what the quality is by calling GetPrinterQuality(), and then seeing what the value returned was. If the value returned is negative, then use the constants listed above. However, if the value is positive, then you will need to use a value that is a DPI value. Have fun.
void SetXDPI(int DPI); This function sets the X-DPI for the device. It is the same as the above function, except that it... has a different name, to keep some people happy...
void ShowPrinterSettingsDialog(HWND Handle); This function shows the proper printer settings dialog for the current printer. Handle is the window that is the parent to the dialog. Through this dialog, the user can set the number of copies, the quality, the page orientation, the paper size, and any other settings which I've failed to mention.

Sample Code

Before I get any further, the question that is probably lingering is, "How do I actually put something on paper?" Well, this class inherits the _GRAPHICSBASE class, and thus all of the drawing things that you can do with the _GRAPHICSBASE class, you can do with the printer. The only thing that you should not do is call the StartDrawing() and StopDrawing() functions - you will only stuff things up.

Anyway, onto the sample code...


//Create a printer object...
fPRINTER Printer;

//If you start a print job with the dialog, then you must
//call this function first:
Printer.PreparePrinterDialog();
//You can fiddle with the printer dialog settings by
//getting a pointer to the printer dialog
//(If you want to know how to fiddle with the printer dialog,
//check out the documentation for the fPRINTERDIALOG class)
(Printer.GetPrinterDialogBox())->CallThisMemberOffPRINTERDIALOG();

//Now start a print job, showing a dialog...
Printer.StartPrintJobDialog(Window.GetHandle(), "F3C - Test Document Name");
//Or just start a print job, no dialog...
Printer.StartPrintJob("F3C - Test Document Name, "Brother HL-1070", NULL);
//In the above function, you MUST supply the name of a printer
//that is available on the system. You can get the names of the printers
//with the GetPrinter...() functions. Hey, if you just want to print
//directly to the default printer, then try this:
Printer.StartPrintJob("Document Name", Printer.GetDefaultPrinterName(), NULL);

//A new page is already started for you. You can start writing to the
//first page immediately.

//For example, let's draw a border right around the edge of the page.
Printer.Line(0,0, Printer.GetPrinterPixelWidth(), 0);
Printer.Line(0,0, 0, Printer.GetPrinterPixelHeight());
Printer.Line(Printer.GetPrinterPixelWidth(),
             Printer.GetPrinterPixelHeight(),
             Printer.GetPrinterPixelWidth(), 0);
Printer.Line(Printer.GetPrinterPixelWidth(),
             Printer.GetPrinterPixelHeight(),
             0, Printer.GetPrinterPixelWidth());

//When you are finished with a page, start a new one...
Printer.EndCurrentPage();
Printer.StartNewPage();

//And draw the same again...
Printer.Line(0,0, Printer.GetPrinterPixelWidth(), 0);
Printer.Line(0,0, 0, Printer.GetPrinterPixelHeight());
Printer.Line(Printer.GetPrinterPixelWidth(),
             Printer.GetPrinterPixelHeight(),
             Printer.GetPrinterPixelWidth(), 0);
Printer.Line(Printer.GetPrinterPixelWidth(),
             Printer.GetPrinterPixelHeight(),
             0, Printer.GetPrinterPixelWidth());

//Now we are finished. You don't have to stop the page; that's
//done automatically when you call this function...
Printer.EndPrintJob();

//And that's it! You've printed to a printer!
//As soon as you call the EndPrintJob() function, the document
//will be uploaded to the printer and should start printing.

//If you used a dialog when starting the print job, you should call
//the following function when you are all done:
Printer.CleaupDialog();


Back to indexThe FreeFoote Foundation Classes Documentation