如何能得到当前打印机所支持的纸张类型?(50分)

X

xiao an

Unregistered / Unconfirmed
GUEST, unregistred user!
1、如何能得到当前打印机所支持的纸张类型(papernames),如何能在选择一种纸张类型后,得到该纸张的高度(paperheight) 和宽度(paperwidth)。
2、调用Windows API函数GetDeviceCaps获取打印机设备参数—打印机分辨率、可打印纸张大小、不可打印边界等参数,这些参数与上述纸张类型的关系是什么?
3、Printer.PageHeight和Printer.PageWidth与上述参数的关系是什么?
4、编写打印程序的正确步骤是怎样的?
 
1. 如果我猜得不错的话你想让用户选择纸张大小,并预览。
那么请用PageSetupDlg。
3. Printer.pageHeight,Printer.pageWidth是按象素统计当前纸张的高与宽
当然是按当前打印机分辨率
4. 先判断打印机存在否
然后打印输出
if Printer.Count > 0 then

begin

Printer.begin
Doc;
With Printer.Canvasdo

begin

输出内容;
end;

Printer.EndDoc;
end;
 
xiao an 是想知道打印机支持的所有纸张类型.
 
何为PageSetupDlg?如何调用?
调用PrinterSetupDialog可以显示出当前打印机类型及纸张类型,但如何得到纸张的
高度和宽度呢?
 
function GetDeviceName():String;
var MyReg: TRegistry;
szDeviceName: String;
begin

try
MyReg := TRegistry.Create;
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 GetPortName(): String;
var MyReg: TRegistry;
szPortName: String;
begin

try
MyReg := TRegistry.Create;
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;


function SetPaperSize(var nWidth,nHeight,nOrient:Word;var nHandle: THandle):integer;
export;
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;

 
Dialogs的PrinterSetupDialog1:
PrinterSetupDialog1.Execute;
 
我想从一个ComboBox中列出当前打印机所支持的纸张类型,并且当选择ComboBox中的不同纸张类型时,当前打印机的纸张类型随之改变(类似PrinterSetupDialog的功能),选择不同的纸张类型时能在另外两个Edit中显示以毫米为单位的该纸张的高度和宽度。谢谢!
 
若有了其他方法或答案例子,请E-Mail给我,谢谢大家!
 
要获得打印机支持的纸张,一般是利用Api函数DeviceCapabilitiesA取得。
下面的一段程序仅供参考。
var Rslt,i:integer;
PaperNames:pointer;
Device,Port:Array[0..255] of char;
begin

combobox1.Items.Clear;
Rslt := DeviceCapabilitiesA(Device, Port, DC_PAPERNAMES, nil, nil);
if Rslt > 0 then
begin

GetMem(PaperNames, Rslt*64);
try
if DeviceCapabilitiesA(Device, Port, DC_PAPERNAMES,
PaperNames, nil) = - 1 then

raise Exception.Create('DevCap Error');
for i := 0 to Rslt - 1do

combobox1.Items.Add(StrPas(TPNames(PaperNames^)));
finally
FreeMem(PaperNames, Rslt*64);
end;

end;

end;
 
请问,为何要用TRegistry类?其作用如何?烦请详述!!?
 
接受答案了.
 

Similar threads

I
回复
0
查看
606
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
2K
import
I
顶部