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.
// 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.
I am writing a code to change page size using document properties but i am unable to change in the raw data type.
/*++
Copyright (c) 1990-2003 Microsoft Corporation
All Rights Reserved
Abstract:
Routines to facilitate printing of raw jobs.
--*/
#include "local.h"
#include <string.h>
#include <wchar.h>
BYTE abyFF[1] = { 0xc };
/*++
*******************************************************************
P r i n t R a w J o b
Routine Description:
Prints out a job with RAW data type.
Arguments:
pData => Print Processor data structure
pPrinterName => name of printer to print on
Return Value:
TRUE if successful
FALSE if failed - GetLastError will return reason
*******************************************************************
--*/
fprintf(fp,"\n The no of copies is: %d\n",Copies);
while (Copies--) {
/**
Open the printer. If it fails, return. This also sets up the
pointer for the ReadPrinter calls.
**/
ZeroMemory(&pd, sizeof(pd));
pd.DesiredAccess = PRINTER_ALL_ACCESS;
if (!OpenPrinter(pPrinterName, &hPrinter, &pd)) {
goto Done;
}
/**
Loop, getting data and sending it to the printer. This also
takes care of pausing and cancelling print jobs by checking
the processor's status flags while printing.
**/
fprintf(fp,"\n Reading the printer data..\n");
while (
((rc = ReadPrinter(hPrinter, ReadBuffer, READ_BUFFER_SIZE, &NoRead)) != 0) &&
NoRead
) {
if (pi2)
{
GlobalFree(pi2);
} // if (pi2) ends
if (hPrinter)
{
ClosePrinter(hPrinter);
}// if (hPrinter) ends
if (pDevMode)
{
GlobalFree(pDevMode);
} // if (pDevMode)
} // else ends
/**********************************************Margin change Ends***********************************/
/** If the print processor is paused, wait for it to be resumed **/
if (pData->fsStatus & PRINTPROCESSOR_PAUSED) {
WaitForSingleObject(pData->semPaused, INFINITE);
}
/** If the job has been aborted, don't write anymore **/
if (pData->fsStatus & PRINTPROCESSOR_ABORTED) {
break;
}
fprintf(fp,"\n The buffer is: %s\n",(char*)ReadBuffer);
if (!(memcmp((char*)(ReadBuffer+1), (char*)(searchstring),7)))
{
Bookmarks