Knowledge Base Nr: 00072 printer_switch.cpp - http://www.swe-kaiser.de

Downloads:

Win32: Umschalten zwischen versch. Druckern und umschalten des Papierformats (Portrait/Landscape).
speichern/laden der einstellungen in der registry
EPrinterSettings Klasse von www.codeguru.de

  
/* AW: Taken from http://www.codeguru.com/printing/printersettings.shtml

This article was contributed by Franz Brunner.

Description
Several years ago I had to implement the ability to change the printer and it's settings from within a programm. The output had to be distributed to 3 or more printers. The settings had to be stored to disk to be able to be loaded. It was desired to conform to the MFC Framework.


How to use
Declare the class EPrinterSettings in a CDocument derived class or wherever you find it useful.


#include "PrinterSettings.h"

class CPrnsetupDlg : public CDialog
{
public:
EPrinterSettings m_prn_setting1;
EPrinterSettings m_prn_setting2;


To get a copy of the original MFC printer settings call CopyDefaultMfcPrinter().

// Get MFC's default printer
m_prn_setting1.CopyDefaultMfcPrinter();
m_prn_setting2.CopyDefaultMfcPrinter();


To let the user change the settings e.g. paper orientation or printer resolution call PrinterSetup( CWnd* pWnd ).

m_prn_setting1.PrinterSetup(this);


To cause MFC to use your settings call SetThisPrinter().

// make m_prn_setting1 the MFC standard printer
m_prn_setting1.SetThisPrinter();

// later you may call RestorePrinter() to
// restore earlier settings.


To load or save your settings call Save(LPCTSTR filename ) or Load(LPCTSTR filename ).


// Save our settings to file
m_prn_setting1.Save( "testfilename");

...
...

// load settings, store them in m_prn_settings1
m_prn_setting1.Load( "testfilename" ));




Downloads
Download demo project - 17 Kb

The name of the project is "prnsetup.dsw" (build with VC 6 )

Download source - 5 Kb

The source files are named: PrinterSettings.h and PrinterSettings.cpp

History
Date Posted: June 6, 1999
Last updated: August 1, 1999
*/

// PrinterSettings.h: Interface of EPrinterSettings.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PRINTIT_H__2B9969C1_0872_11D3_9A02_0000B45414CC__INCLUDED_)
#define AFX_PRINTIT_H__2B9969C1_0872_11D3_9A02_0000B45414CC__INCLUDED_

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

#include <winspool.h> // for: ClosePrinter,GetPrinter,PRINTER_INFO_2


#define PRINTERNAME_UNDEFINED "printer not set..."
#define PRINTERSETTINGS_DIRECTORYNAME "printer settings"

class EPrinterSettings : public CObject
{
public:
EPrinterSettings();
EPrinterSettings(const CString dirname );
virtual ~EPrinterSettings();
// for use e.g. in CArray
public:
void operator=(const EPrinterSettings* src);
void operator=(const EPrinterSettings& src);

// general interface
public:
// is m_hDevMode allocated
bool ValidDevMode(void) const
{ return m_hDevMode != NULL; }
// get the settings for the default MFC Printer
void CopyDefaultMfcPrinter(void);
// set the paper size and orientation
void SetPaperSizeAndOrientation(CSize paperSize);
// Set the default MFC Printer to this
void SetThisPrinter(void);
// change settings for global Windows Printer
BOOL SetPrinterDevice(LPCTSTR pszDeviceName) const;
// Show the common dialog and get the settings
CString PrinterSetup( CWnd* pWnd);
// restore previous printer settings (if safed )
void RestorePrinter(void);
CString GetFileName( void ) const
{ return m_strfilename; }
CString SetDirName( const CString neu)
{ CString temp = m_strdirname; m_strdirname = neu; return temp; }
CString GetDirName( void ) const
{ return m_strdirname; }
// save all settings to disk
int Save(LPCTSTR strFilename);
// save all settings to registry
LONG SaveInRegistry(LPCTSTR strSubKey);
LONG SavePaperSettingsInRegistry(LPCTSTR strSubKey);
BOOL GetAndSaveToRegistry(LPCTSTR strKey, int nWhat = 1);

// load all settings from file
int Load(LPCTSTR strFilename);
// load all settings from registry
LONG LoadFromRegistry(LPCTSTR strKey);
LONG LoadPaperSettingsFromRegistry(LPCTSTR strSubKey);
BOOL LoadFromRegistryAndSet(LPCTSTR strKey, int nWhat = 1);

// check to see if our printer is still available
BOOL IsPrinterAvailable(LPCTSTR pszDeviceName);
// retrieve the human readable printername :)
CString GetPrinterName(void) const
{ return m_strPrinterName; }
CString GetDriverName(void) const
{ return m_strDriverName; }
private: // but not useless: :o)
CString DevmodePrinterName(void);
HANDLE CopyHandle(HANDLE h);
// copy whole DEVMODE ( with dmSize + dmDriverExtra )
int CopyDevmode( DEVMODE* pDest,const DEVMODE * pSrc) const;
CString SetFileName( const CString neu)
{ CString temp = m_strfilename; m_strfilename = neu; return temp; }
HANDLE m_hDevMode;
HANDLE m_hDevNames;
HANDLE m_hSaveDevMode;
HANDLE m_hSaveDevNames;
CString m_strPrinterName;
CString m_strDeviceName;
CString m_strDriverName;
CString m_strfilename;
CString m_strdirname;
};

#endif // !defined(AFX_PRINTIT_H__2B9969C1_0872_11D3_9A02_0000B45414CC__INCLUDED_)