|
-
January 23rd, 2004, 07:07 PM
#1
Using DEVMODE to set custom paper size.
I am having some problem setting the paper size for a printer using the DEVMODE structure. Here is what I am doing...
* I have defined an instance of the PRINTDLG structure in my class, and I am using this structure's hDC and hDevMode handles to set/change the printer settings. I am filling this structure using the PrintDlg function with the PrintDlgData.flags = PD_RETURNDEFAULT.
PRINTDLG PrintDlgData;
* When I have to set the paper size to custom, I am getting the DEVMODE structure from the current printer using the DocumentProperties function, and modify it.
HANDLE hPrinter;
LPDEVMODE pDevMode;
DWORD dwNeeded, dwRet;
SIZEL FormSize;
char printerName[512];
bool isFormAdded = false;
strcpy(printerName, d_printer_names[d_current_printer_i]);
// Start by opening the printer
if (!OpenPrinter(printerName, &hPrinter, NULL))
return (false);
// Step 1: Allocate a buffer of the correct size.
dwNeeded = DocumentProperties(NULL,
hPrinter, // Handle to our printer.
printerName, // Name of the printer.
NULL, // Asking for size, so
NULL, // these are not used.
0); // Zero returns buffer size.
pDevMode = (LPDEVMODE)malloc(dwNeeded);
// Step 2: Get the default DevMode for the printer and modify it
dwRet = DocumentProperties(NULL,
hPrinter,
printerName,
pDevMode, // The address of the buffer to fill.
NULL, // Not using the input buffer.
DM_OUT_BUFFER); // Have the output buffer filled.
if (dwRet != IDOK)
{
// If failure, cleanup and return failure.
free(pDevMode);
ClosePrinter(hPrinter);
return (false);
}
if (pDevMode->dmFields & DM_PAPERSIZE)
pDevMode->dmPaperSize = 0;
if (pDevMode->dmFields & DM_PAPERLENGTH)
pDevMode->dmPaperLength = in_length_mm*10;
if (pDevMode->dmFields & DM_PAPERWIDTH)
pDevMode->dmPaperWidth = in_width_mm*10;
* I am calling the DocumentProperties function again to make changes to the DEVMODE structure I just modified.
dwRet = DocumentProperties(NULL,
hPrinter,
printerName,
pDevMode, // The address of the buffer to fill.
pDevMode, // Pass the driver our changes.
DM_IN_BUFFER|DM_OUT_BUFFER);
if (dwRet != IDOK)
{
// If failure, cleanup and return failure.
free(pDevMode);
ClosePrinter(hPrinter);
return (false);
}
* I am calling the ResetDC function to convey these changes to the DEVMODE to the printer DC.
// Reset the DC
if (!ResetDC(PrintDlgData.hDC, pDevMode))
{
dwRet = GetLastError();
return (false);
}
* Am I using these functions correctly. Is there something else to be done to set the paper custom size. It seems that the ResetDC function does not update the global DEVMODE used by the printer.
Thanks for any help in advance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|