请教用DELPHI怎样设置和获得打印机的纸张大小(50分)

  • 主题发起人 主题发起人 无用书生
  • 开始时间 开始时间

无用书生

Unregistered / Unconfirmed
GUEST, unregistred user!
请问在Delphi5中如果不通过TPrinterSetupDialog而是直接通过代码设置打印机的纸张大小
怎么办?比如如果我要把纸张大小不通过提供对话框让用户干予的形式而直接设为宽200mm,
高300mm。另外可以通过什么办法得知当前打印设置中的纸张大小?

 
用Printer全局变量的PageWidth和PageHeight,不过要注意象素和毫米之间的转换。
 
老兄这分可轮到我来拿了。
var
ADevice, ADriver, APort: array[0..255] of Char;
DeviceHandle: THandle;
DevMode: PDeviceMode;
begin

//初始化打印机
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
if DeviceHandle = 0 then

begin

Printer.PrinterIndex := Printer.PrinterIndex;
Printer.GetPrinter(ADevice, ADriver, APort, DeviceHandle);
end;

if DeviceHandle = 0 then

Raise Exception.Create('不能初始化打印机!')
else

DevMode := GlobalLock(DeviceHandle);
if DeviceHandle <> 0 then

begin

GlobalUnLock(DeviceHandle);
with DevMode^do

begin

dmFields := dmFields or DM_PAPERWIDTH or DM_PAPERLENGTH;
dmPaperSize := DMPAPER_USER;
dmPaperWidth := 2940;
dmPaperLength := 3500;
Printer.SetPrinter(ADevice, ADriver, APort, DeviceHandle);
end;

if GetPaperWidth < 2939 then
//在此省略GetPaperWidth函数
MessageBox(Handle,'系统默认打印机的打印尺寸不能满足本打印任务的需要!'+#13+#13
+'请使用符合要求的打印机, 并安装正确的打印驱动程序。','打印机不兼容',
MB_ICONWARNING+MB_OK);
end;

end;


 
uses
Windows,
WinSpool, //一定要 uses 这玩意
SysUtils,
Registry,
Classes;


//=================================
function TForm1.GetDeviceName():String;
var MyReg: TRegistry;
szDeviceName: String;
begin

MyReg := TRegistry.Create;
try
MyReg.RootKey := HKEY_CURRENT_CONFIG;
if MyReg.OpenKey('/System/CurrentControlSet/Control/Print/Printers',False) then

szDeviceName:=MyReg.ReadString('Default')
else

szDeviceName:='';
except
MyReg.Free;
end;

result:=szDeviceName;
end;


//===================================
function TForm1.GetPortName(): String;
var MyReg: TRegistry;
szPortName: String;
begin

MyReg := TRegistry.Create;
try
MyReg.RootKey := HKEY_LOCAL_MACHINE;
if MyReg.OpenKey('/System/CurrentControlSet/Control/Print/Printers/'+GetDeviceName(),False) then

szPortName:=MyReg.ReadString('Port')
else

szPortName:='';
Except
MyReg.Free;
end;

result:=szPortName;
end;



//================================
//nOrient 打印方向
//nHandle 打印机的Handle
// 调用 SetPaperSize(2000,3000,1,printer.handle);
// 调用后,修改系统的打印机的纸张大小的设置。
function TForm1.SetPaperSize(var nWidth,nHeight,nOrient:Word;var nHandle: THandle):integer;

var
// szPrinterKey: array[0..99] of Char;
szDeviceName: String;
szPort: String;
// cbBuffer: DWORD;
// dwRV: DWORD;
dwPapers: DWord;
lpwPapers: array[0..255] of Word;
fSupportUserDefind: Boolean;
fSupportA3: Boolean;
fSupportA4: Boolean;
fSupportB5: Boolean;
hDriver: THandle;
hMem: HGLOBAL;
lpDevMode: Pdevicemode;
a1: Pdevicemode;
i: integer;
begin

fSupportUserDefind:=FALSE;
fSupportA3:=FALSE;
fSupportA4:=FALSE;
fSupportB5:=FALSE;

//取当前默认打印机设备名
szDeviceName:=GetDeviceName();
if szDeviceName='' then

result:=-1;

//取打印机端口
szPort:=GetPortName();
if szPort='' then

result:=-2;

//取打印机支持的全部纸型
dwPapers:=DeviceCapabilities(PChar(szDeviceName),PChar(szPort),DC_PAPERS,@lpwPapers,nil);
if (dwPapers<1)or(dwPapers>256) then

result:=-3;

//判断打印机是否支持自定义、A3、A4、B5纸型
while (dwPapers > 0)do

begin

case DWORD(lpwPapers[dwPapers]) of
DMPAPER_USER: fSupportUserDefind:=TRUE;
DMPAPER_A3: fSupportA3:=TRUE;
DMPAPER_A4: fSupportA4:=TRUE;
DMPAPER_B5: fSupportB5:=TRUE;
end;

dwPapers:=dwPapers-1;
end;


//取打印机的DeviceMode
a1:=nil;
if not(OpenPrinter(PChar(szDeviceName),hDriver,nil)) then
result:=-4;
hMem:=GlobalAlloc(GPTR,DocumentPropertiesA(nHandle,hDriver,PChar(szDeviceName),a1^,a1^,0));
lpDevMode:=GlobalLock(hMem);
a1:=nil;
do
cumentProperties(nHandle,hDriver,PChar(szDeviceName),lpDevMode^,a1^,DM_OUT_BUFFER);

//设置纸型或大小
i:=0;
if ((nWidth=2970)and(nHeight=4200))and(fSupportA3) then
i:=1;
if ((nWidth=2100)and(nHeight=2970))and(fSupportA4) then
i:=i+2;
if ((nWidth=1820)and(nHeight=2570))and(fSupportB5) then
i:=i+3;

case i of
1: begin
//A3
lpDevMode.dmFields:=DM_PAPERSIZE;
lpDevMode.dmPaperSize:=DMPAPER_A3;
end;

2: begin
//A4
lpDevMode.dmFields:=DM_PAPERSIZE;
lpDevMode.dmPaperSize:=DMPAPER_A4;
end;

3: begin
//B5
lpDevMode.dmFields:=DM_PAPERSIZE;
lpDevMode.dmPaperSize:=DMPAPER_B5;
end;

else

if fSupportUserDefind then

begin

lpDevMode.dmFields:=DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH;
lpDevMode.dmPaperSize:=DMPAPER_USER;
lpDevMode.dmPaperWidth:=nWidth;
lpDevMode.dmPaperLength:=nHeight;
end
else

lpDevMode.dmFields:=0;
end;


//设置方向
case nOrient of
0: begin

lpDevMode.dmFields:=lpDevMode.dmFields or DM_ORIENTATION;
lpDevMode.dmOrientation:=DMORIENT_PORTRAIT;
end;

1: begin

lpDevMode.dmFields:=lpDevMode.dmFields or DM_ORIENTATION;
lpDevMode.dmOrientation:=DMORIENT_LANDSCAPE;
end;

end;


a1:=nil;
do
cumentProperties(nHandle,hDriver,PChar(szDeviceName),a1^,lpDevMode^,DM_IN_BUFFER or DM_UPDATE);
GlobalUnlock(hMem);
GlobalFree(hMem);
ClosePrinter(hDriver);

result:=1;
end;


 
多人接受答案了。
 
cqsssco老兄的方法好像不行,建议用Win32函数的DocumentProperties()来做.
 
后退
顶部