Click here to Skip to main content
15,881,455 members
Articles / Desktop Programming / MFC

Setting the Default Printer Programmatically in an MFC Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
11 Jun 2002CPOL2 min read 241.4K   4.1K   50  
How to change the default printer in MFC and save/restore this setting to/from the registry
// EnumPrinters.h: interface for the CEnumPrinters class.
//
// Written By : R.I.Allen
// 3rd May 2002
// Roger.Allen@sirius-analytical.com
// You can use this source as you like, but without any warranties of any kind!
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ENUMPRINTERS_H__496315A4_5B64_11D6_AD8C_00B0D0652E95__INCLUDED_)
#define AFX_ENUMPRINTERS_H__496315A4_5B64_11D6_AD8C_00B0D0652E95__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CEnumPrinters  
{
public:
					CEnumPrinters();
	virtual			~CEnumPrinters();

	// returning gathered printer information
	int				GetPrinterCount() ;
	CString			GetPrinterName(int index) ;
	CString			GetPrinterLocation(int index) ;
	CString			GetPrinterShareName(int index) ;
	CString			GetPrinterPortName(int index) ;
	// enumerating printers on this and remote machines
	void			ReadLocalPrinters() ;
	// actually changing or configuring the printer
	bool			SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, const CString& PrinterName, const CString& PrinterSpooler, const CString& PrinterPort) ;
	bool			SetNewPrinter(HANDLE& hDevMode, HANDLE& hDevNames, int index) ;
	bool			SetPrintOrientation(HANDLE &hDevMode, int mode) ;
	// saving settings too/from the registry
	bool			SavePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;
	bool			RestorePrinterSelection(HANDLE &hDevMode, HANDLE& hDevNames) ;
	// debug options only
#ifdef _DEBUG
	void			DumpHandles(HANDLE& hDevMode, HANDLE& hDevNames) ;
#endif
private:
	int				m_NumPrinters ;
	CStringList		m_PrinterName ;
	CStringList		m_PrinterLocation ;
	CStringList		m_PrinterShareName ;
	CStringList		m_PrinterPort ;
};

#endif // !defined(AFX_ENUMPRINTERS_H__496315A4_5B64_11D6_AD8C_00B0D0652E95__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
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.

Comments and Discussions