Need to Create Printer DC from Printer Name

Need to Create Printer DC from Printer Name

Post by Ian Semme » Sun, 26 Oct 2003 16:46:40


// There might be a smarter way but .......

CString strPrinterName = "Whatever";

LPTSTR pszPrinterName = strPrinterName.GetBuffer ( 0 );
strPrinterName.ReleaseBuffer ( -1 );

LONG nLen = DocumentProperties ( 0, 0, pszPrinterName, 0, 0, 0 );

DEVMODE* pDEVMODE = (DEVMODE*) ( new char [nLen ] );
memset ( pDEVMODE, 0, sizeof (DEVMODE) );

LONG rc = DocumentProperties ( 0,0, pszPrinterName, pDEVMODE, 0,DM_OUT_BUFFER );

// You can fill in options to the DEVMODE here eg

if ( bWeWantLandscape )
pDEVMODE ->dmOrientation = DMORIENT_LANDSCAPE;
else
pDEVMODE ->dmOrientation = DMORIENT_PORTRAIT;
pDEVMODE ->dmFields |= DM_ORIENTATION;
rc = DocumentProperties ( 0, 0, pszPrinterName, pDEVMODE, pDEVMODE,
DM_OUT_BUFFER | DM_IN_BUFFER );
// and then

CDC dcPrint;
HDC hDC = CreateDC ( 0, strPrinterName, 0, pDEVMODE ); dcPrint.Attach ( hDC );

// do your stuff .. //x

// and eventually

dcPrint.Detach();
DeleteDC ( hDC );