转载(要uses winspool, printers
1, Escape( printer.handle, GETPHYSPAGESIZE, 0, nil, @physsize );可取得纸张的实际尺寸(点数,与打印机分辨率有关,把它转为
以Twips为单位就是实际尺寸)
2. pixelsperinchx := GetDeviceCaps( printer.handle, LOGPIXELSX );
Longint(printer.pagewidth) * 1440) div pixelsperinchx
可得到打印机可打印的范围尺寸(已转为Twips)。
type
PDevMode=^TDevMode;
TPrinterDev = class
Driver: string;
Device: string;
Port: string;
end;
implimentation
procedure TForm1.SetPrinter;
var
Devs: PDevMode;
DevsP: Pointer;
DevSize: integer;
PrinterObject: THandle;
DevName: string;
begin
PrinterObject:= Printer.Handle;
DevName:= TPrinterDev(Printer.Printers.Objects[Printer.PrinterIndex]).Device;
DevSize:=DocumentProperties(0,PrinterObject,PChar(DevName),Devs,Devs,0);
DevsP:=AllocMem(DevSize);
try
do
cumentProperties(0,PrinterObject,Pchar(DevName),nil,DevsP,DM_OUT_BUFFER);
Devs:=DevsP;
Devs^.dmFields:=DM_PAPERSIZE;
Devs^.dmPaperSize:=DMPAPER_FANFOLD_US;
// if use USER_DEFINE paper then
// this line change to
// Devs^.dmFields:= DM_PAPERSIZE or DM_PAPERLENGTH or DM_PAPERWIDTH;
// Devs^.dmPaperSize:=0;
// Devs^.dmPaperLength:= ur defined paper length value;
// Devs^.dmPaperWidth:= ur defined paper width value;
do
cumentProperties(0,PrinterObject,Pchar(DevName),DevsP,DevsP,DM_OUT_BUFFER or DM_IN_BUFFER);
finally
FreeMem(DevsP,DevSize);
end;
end;
end.
procedure ChangePaperType(value: Integer);
var
sDrv: Pchar;
sPrt: Pchar;
DevAdr: THandle;
p: PDeviceModeA;
begin
sdrv := allocmem(256);
sprt := allocmem(64);
devadr := 0;
try
printer.GetPrinter(sdrv, sdrv, sprt, devadr);
p := ptr(devadr);
p^.dmFields := DM_PAPERSIZE;
p^.dmPaperSize := value;
do
cumentProperties(0, printer.handle, sdrv, p^, p^,
DM_IN_BUFFER);
finally
freemem(sdrv, 1024);
freemem(sprt, 256);
end;
end;