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;
**********************************
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;
**********************************