Article View: borland.public.cppbuilder.nativeapi
Article #15431Re: Setting page size progmatically
From: Midnight Programmer
Date: Sun, 06 Jul 2003 03:36
Date: Sun, 06 Jul 2003 03:36
125 lines
5794 bytes
5794 bytes
"A Whitesell" <awhitesell@attbi.com> wrote in message
news:3f04d39f$1@newsgroups.borland.com...
> Hey Midnight Programmer,
>
> I am using the VCL, but TPrinter does not have a way to set the page
height
> and width, they are read only. Are you aware of a way to use the VCL and
> set the page height and width? I am having problems getting the example
> below to work but I still have a lot of things left to try, but using the
> VCL would be easier.
>
> TIA.
The VCL approach should be easier but I couldn't make the
TPrinter::GetPrinter() routine give me back the
DEVMODE Structure Nicely.
The Help said use the GlobalLock() function to convert the Handle into a
pointer to the structure but it didn't work..
(Somebody got an example?)
If we get a response to this, it will be similar to below... Use the DEVMODE
Structure to setup what you want and then
call TPrinter::SetPrinter() to do the job.
I did find out that both this stuff and the TPrinter stuff worked ok
together. If I set the Orientation using WINAPI, the TPrinter would print
like I said..... If I've got it wrong, I'm sure one of the TeamB Gods will
set me straight (thanks).
So Heres the WINAPI approach that seems to work ok with the printer (HP) I
have...
// Routine to Set Paper Size, Duplex, Orientation, Quality and
Scaling....
{
PRINTER_INFO_2 MyPrinterInfo;
HANDLE HPrinter;
unsigned long MyPrinterInfoSize = 0;
char *def_string = "no printer";
char buffer[MAX_PATH];
// get the default printer name from the WIN.INI file into 'buffer'
GetProfileString("windows", "device", def_string, buffer, MAX_PATH);
// parse the buffer to extract the printer name
AnsiString Abuffer(buffer);
AnsiString Device = "";
// Find the 1st Comma and Backup one.
int iComma = Abuffer.Pos(",") - 1;
// If it found the Comma, the Printer Name is that long.
if(iComma > 0)
Device = Abuffer.SubString(1, iComma);
else
return; // NO PRINTER NAME???
// open the printer
if (OpenPrinter(Device.c_str(), &HPrinter, NULL))
{
// Try and Get the Printer Info
// This call fails always with Error 122 (Data Size To Small)
GetPrinter(HPrinter, 2, (unsigned char *)&MyPrinterInfo,
sizeof(PRINTER_INFO_2), &MyPrinterInfoSize);
// Make it forget about the error which hangs around until cleared
SetLastError(0);
// allocate the memory using the Size the last call yielded
unsigned char *buffer = new unsigned char[MyPrinterInfoSize];
PPRINTER_INFO_2 MyInfo = (PPRINTER_INFO_2)buffer;
// Now that you have the real size, get the Printer Info
GetPrinter(HPrinter, 2, buffer, MyPrinterInfoSize,
&MyPrinterInfoSize);
if(GetLastError())MessageBox(NULL,
SysErrorMessage(GetLastError()).c_str(), NULL, MB_OK);
// Now play with the Orientation
//#define DMORIENT_PORTRAIT 1
//#define DMORIENT_LANDSCAPE 2
MyInfo->pDevMode->dmOrientation = DMORIENT_PORTRAIT;
MyInfo->pDevMode->dmFields = MyInfo->pDevMode->dmFields |
DM_ORIENTATION;
// Now play with the Paper Size
// Lots of choices here (see wingdi.h)
MyInfo->pDevMode->dmPaperSize = DMPAPER_LETTER;
MyInfo->pDevMode->dmFields = MyInfo->pDevMode->dmFields |
DM_PAPERSIZE;
// Now play with the Print Quality
//#define DMRES_DRAFT (-1) (Faster Printing)
//#define DMRES_LOW (-2)
//#define DMRES_MEDIUM (-3)
//#define DMRES_HIGH (-4) (Custom)
MyInfo->pDevMode->dmPrintQuality = DMRES_LOW;
MyInfo->pDevMode->dmFields = MyInfo->pDevMode->dmFields |
DM_PRINTQUALITY;
// Now play with the Duplexing
MyInfo->pDevMode->dmDuplex = DMDUP_SIMPLEX;
MyInfo->pDevMode->dmFields = MyInfo->pDevMode->dmFields | DM_DUPLEX;
// Now play with the Scaling
MyInfo->pDevMode->dmScale = 100;
MyInfo->pDevMode->dmFields = MyInfo->pDevMode->dmFields | DM_SCALE;
// Tell the Printer about the new settings.
if(SetPrinter(HPrinter, 2, buffer, 0))MessageBox(NULL,
SysErrorMessage(GetLastError()).c_str(), NULL, MB_OK);
// Shut it down
ClosePrinter(HPrinter);
// Clean up the trash
delete []buffer;
buffer = NULL;
MyInfo = NULL;
}
else // Printer Didn't Open???
{
MessageBox(NULL, SysErrorMessage(GetLastError()).c_str(), NULL,
MB_OK);
}
}
Technical Details
Message-ID:
<3f079969@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!diablo.theplanet.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f045222$1@newsgroups.borland.com> <3f0459d8@newsgroups.borland.com> <3f04aaa7@newsgroups.borland.com> <3f04acd9$1@newsgroups.borland.com> <3f04d39f$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,163c42dfba190846 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-07-05 20:39:33 PST From: "Midnight Programmer" <nospam@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f045222$1@newsgroups.borland.com> <3f0459d8@newsgroups.borland.com> <3f04aaa7@newsgroups.borland.com> <3f04acd9$1@newsgroups.borland.com> <3f04d39f$1@newsgroups.borland.com> Subject: Re: Setting page size progmatically Date: Sat, 5 Jul 2003 20:36:25 -0700 Lines: 128 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 216.192.13.7 Message-ID: <3f079969@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1057462633 216.192.13.7 (5 Jul 2003 20:37:13 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!diablo.theplanet.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail