Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on setting the printer settings

Status
Not open for further replies.

carlo20

Programmer
Nov 2, 2001
3
US
I would like to ask some help on how to change the papersize of the default printer. I've got some code from the internet and I tried to run it but I wasn't successful. There was 2 code but both codes didn't solve my problem.

Here's the first code I've got but it can't identify the setprinter and getprinter function. I wasn't successfull on running this because it wasn't able to identify some function.

procedure TForm1.Button1Click(Sender: TObject);
Var
hPrinter : LongWORD;
pDATA : Pointer;
nRequired : PDWORD;
prnDefaults : TPrinterDefaults; // for NT
begin
Printer.PrinterIndex := -1;
with prnDefaults do begin // for NT
pDatatype := Nil;
pDevMode := Nil;
DesiredAccess := PRINTER_ALL_ACCESS;
end;
OpenPrinter(PChar(Printer.Printers[Printer.PrinterIndex]),
hPrinter, @prnDefaults);
New(nRequired);
// 2 is information level. See GetPrinter in Win32 Help
// See also PRINTER_INFO_2 structure description in Win32 help
GetPrinter(hPrinter, 2, Nil, 0, nRequired);
GetMem(pDATA, nRequired^);
if GetPrinter(hPrinter, 2, pDATA, nRequired^, nRequired) then begin
with PRINTER_INFO_2(pDATA^).pDevMode^ do begin
PRINTER_INFO_2(pDATA^).pSecurityDescriptor := Nil; // for NT
dmFields := dmFields OR
DM_PAPERSIZE OR
DM_PAPERLENGTH OR
DM_PAPERWIDTH;
dmPaperSize := 0;
dmPaperLength := 3000;
dmPaperWidth := 1000;
end;
if NOT SetPrinter(hPrinter, 2, pDATA, 0) then
ShowMessage(SysErrorMessage(GEtLastError));
end else ShowMessage('GetPrinter 2 fails: ' + SysErrorMessage(GEtLastError));
FreeMem(pDATA);

Dispose(nRequired);
end;

*************************************

Here's the second code and I was able to run this code but there nothing happen when I tried to press the button. The printer settings wasn't change.

procedure TForm1.Button1Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
hDMode : THandle;
pDMode : PDEVMODE;
begin
Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(Device, Driver, Port, hDMode);
if hDMode <> 0 then begin
pDMode := GlobalLock(hDMode);
if pDMode <> nil then begin

{Set to custom size}

pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;

pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmOrientation:= 1;
pDMode^.dmPaperWidth := 3560 {SomeValueInTenthsOfAMillimeter};
pDMode^.dmPaperLength := 2700 {SomeValueInTenthsOfAMillimeter};

Printer.SetPrinter(Device,Driver,port,hdmode);

end;

GlobalUnlock(hDMode);

end;

end;

**********************************
 
Not all printers will support changing papersize. If you can change the printer size in the print dialog advanced settings then you should also be able to change it programmatically. Also, it won't change your global settings - I believe it only changes the printer settings temporarily.



TealWren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top