In a standard MFC application the system wide default printer will be setup as default each time your
application starts. This is done during your
CWinApp objects initialisation, when it calls the function
CWinApp::UpdatePrinterSelection(). This sets up the
global handles (as in global memory handles used with GlobalLock/Unlock) which
CWinApp uses to manage
the selected printer.
I have provided a wrapper class for
enumPrinters, which is used to gather information on the locally
installed printers which is needed when switching printers. It can also set the printers when called by a function in your
CWinApp derived class and passing through
m_hDevNames as references,
so they can be changed.
When switching printers programmatically, you need to know:
- The printers name e.g. HP Laserjet 4L
- The port the printer is installed on e.g. LPT1:
- The spooler that will dump the printout to the printer, usually winspool
CEnumPrinters encapsulates the required functionality needed to query for the locally
setup printers. This list can then be read/used to select new printers and set them as the new default
printer to be used by the application.
Restoring the selected printer
You can save the selected printer to the registry and restore it also. To do this, you need to place calls to
The information saved is:
- The printer name
- The spooling device
- The port name
- The page orientation (portrait or landscape)
The following functions are used in the class:
int GetPrinterCount() ;
CString GetPrinterName(int index) ;
CString GetPrinterLocation(int index) ;
CString GetPrinterShareName(int index) ;
CString GetPrinterPortName(int index) ;
void ReadLocalPrinters() ;
The above functions enumerate the list of locally installed printers - automatically called in class constructor.
bool SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, const CString& PrinterName, const CString& PrinterSpooler, const CString& PrinterPort) ;
Use the function above when switching to a printer by
Name, driver, port
bool SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, int index) ;
Use the function above when switching to a printer in the local list by index
bool SetPrintOrientation(HANDLE &hDevMode, int mode) ;
Use the above function switch between portrait and landscape page orientations.
bool SavePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;
bool RestorePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;
These procedures save/restore the selected printer from the registry (key
Future enhancements or coding projects
Add functionality to query network printers
Add functionality to add a new printer
12 Jun 2002 - Initial Revision
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.
I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)
I am also a keep fit fanatic, doing cross country running and am seriously into [url]http://www.ryushinkan.co.uk/[/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.