Click to See Complete Forum and Search --> : Attaching Printer to CDC


Steve McNeese
April 14th, 1999, 07:43 AM
How can I attach a printer to a CDC object without calling CPrintDialog and the CDC.Attach(GetPrinterDC())? Normally, you would have something like this:

CDC dc;
CPrintDialog printDlg(FALSE);

// ask the user to select a printer
if (printDlg.DoModal() == IDCANCEL)
return;

// Attach a printer DC
dc.Attach(printDlg.GetPrinterDC());

I already know what the printer device name is and I just want to assign that and bypass the dialog asking the user to select the printer. I think I need to eliminate the DoModal() and call printDlg.CreatePrinterDC() but I am not sure how to initialize the DEVMODE and/or DEVNAMES structures and how they are passed or utilized.

Also, how do you change the print margins after you have the printer DC?

Any info would be greatly appreciated.


Thanks,

Steve


Steven M. McNeese
http://steven.mcneese@boeing.com

Alvaro
April 14th, 1999, 10:56 AM
Here's a function that should do what you want:

HDC CreatePrinterDC(LPCTSTR szPrinterName)
{
PDEVMODE pDevMode = NULL;

LONG lDevModeSize = ::DocumentProperties(NULL, NULL, (LPTSTR)szPrinterName, NULL, NULL, 0);
if (lDevModeSize > 0)
{
HANDLE hDevMode = ::GlobalAlloc(GHND, lDevModeSize);
pDevMode = (PDEVMODE) ::GlobalLock(hDevMode);
::DocumentProperties(GetDesktopWindow(), NULL, (LPTSTR)szPrinterName, pDevMode, NULL, DM_OUT_BUFFER);
}
else
return NULL;

// Here you can change the margins, orientation, etc. by manipulating pDevMode.

// Now get a DC for the printer
HDC hDC = ::CreateDC(NULL, szPrinterName, NULL, pDevMode);

::GlobalFree((HANDLE)pDevMode);
return hDC;
}



I don't know how to change the margins after the DC is created but you can certainly set them beforehand (via the DEVMODE pointer).

Good luck!

Alvaro