如题。我找了一段设置打印机纸张大小的代码,为什么不管用?procedure SetPrinterSize(iWidth, iLength: Integer); // 单位 毫米
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 legal}
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
pDMode^.dmPaperSize := DMPAPER_LEGAL; {Set to custom size}
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth :=iWidth*10;
pDMode^.dmPaperLength :=iLength*10;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
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 legal}
pDMode^.dmFields := pDMode^.dmFields or dm_PaperSize;
pDMode^.dmPaperSize := DMPAPER_LEGAL; {Set to custom size}
pDMode^.dmFields := pDMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH;
pDMode^.dmPaperSize := DMPAPER_USER;
pDMode^.dmPaperWidth :=iWidth*10;
pDMode^.dmPaperLength :=iLength*10;
pDMode^.dmFields := pDMode^.dmFields or DMBIN_MANUAL;
pDMode^.dmDefaultSource := DMBIN_MANUAL;
GlobalUnlock(hDMode);
end;
end;
Printer.PrinterIndex := Printer.PrinterIndex;
end;
-------------------出自超级猛料2003版
一直用QR,Rave。
这个办法一直也没用过,我也去玩玩。
var
ADevice : array[0..255] of char;
ADriver : array[0..255] of char;
APort : array[0..255] of char;
hDevHandle : THandle;
PDevMode : PDeviceMode; //A Pointer to a TDeviceMode structure
begin
Printer.PrinterIndex:=Printer.PrinterIndex;
// GetPrinter() 首先获得TPrinter的DeviceMode结构的句柄
Printer.GetPrinter(ADevice, ADriver, APort, hDevHandle);
//如果句柄为0,表示打印机没有装载
if hDevHandle = 0 then
begin
Printer.PrinterIndex:=Printer.PrinterIndex;
Printer.GetPrinter(ADevice, ADriver, APort, hDevHandle);
end;
//如果句柄还是为0,表示有错误发生。否则,就调用
if hDevHandle = 0 then
raise Exception.Create('不能初始化打印结构,请察看打印机是否已加载')
else
if hDevHandle<>0 then
begin
PDevMode:=GlobalLock(hDevHandle);//GlobalLock()获取TDeviceMode结构的指针
if PDevMode <> nil then
try
//Set to legal
PDevMode^.dmFields := PDevMode^.dmFields or dm_PaperSize;
PDevMode^.dmPaperSize := DMPAPER_LEGAL;
//Set to custom size //设置定制的大小
PDevMode^.dmFields := PDevMode^.dmFields or
DM_PAPERSIZE or
DM_PAPERWIDTH or
DM_PAPERLENGTH ;
PDevMode^.dmPaperSize :=DMPAPER_USER;
PDevMode^.dmPaperWidth :=iaWith; //SomeValueIn Tenths Of A Millimeter;
PDevMode^.dmPaperLength :=iaLength; //SomeValueInTenthsOfAMillimeter;
{ //Set the bin to use
PDevMode^.dmFields := PDevMode^.dmFields or DMBIN_MANUAL;
PDevMode^.dmDefaultSource := DMBIN_MANUAL; }
finally
GlobalUnlock(hDevHandle);
end;
end
else
raise Exception.Create('不能设置打印纸张大小');
Printer.PrinterIndex := Printer.PrinterIndex;
end;