关于打印的一个函数,自己确实没有时间去查了,那位大哥知道的话告诉我,100分奉上 ( 积分: 100 )

  • 主题发起人 主题发起人 lanbinvivy
  • 开始时间 开始时间
L

lanbinvivy

Unregistered / Unconfirmed
GUEST, unregistred user!
输入纸张类型名称(如a4、letter等),返回该纸张长、宽尺寸。
 
输入纸张类型名称(如a4、letter等),返回该纸张长、宽尺寸。
 
copy一段给你参考:<br>function SetPaperSize(var nWidth,nHeight,nOrient:Word;var nHandle: THandle):integer; export;<br>var<br> &nbsp;szPrinterKey: array[0..99] of Char;<br> &nbsp;szDeviceName: String;<br> &nbsp;szPort: String;<br> &nbsp;cbBuffer: DWORD;<br> &nbsp;dwRV: DWORD;<br> &nbsp;dwPapers: DWord;<br> &nbsp;lpwPapers: array[0..255] of Word;<br> &nbsp;fSupportUserDefind: Boolean;<br> &nbsp;fSupportA3: Boolean;<br> &nbsp;fSupportA4: Boolean;<br> &nbsp;fSupportB5: Boolean;<br> &nbsp;hDriver: THandle;<br> &nbsp;hMem: HGLOBAL;<br> &nbsp;lpDevMode: Pdevicemode;<br> &nbsp;a1: Pdevicemode;<br> &nbsp;i: integer;<br>begin<br> &nbsp;fSupportUserDefind:=FALSE;<br> &nbsp;fSupportA3:=FALSE;<br> &nbsp;fSupportA4:=FALSE;<br> &nbsp;fSupportB5:=FALSE;<br><br> &nbsp;//取当前默认打印机设备名<br> &nbsp;szDeviceName:=GetDeviceName();<br> &nbsp;if szDeviceName='' then<br> &nbsp; &nbsp;result:=-1;<br><br> &nbsp;//取打印机端口<br> &nbsp;szPort:=GetPortName();<br> &nbsp;if szPort='' then<br> &nbsp; &nbsp;result:=-2;<br><br> &nbsp;//取打印机支持的全部纸型<br> &nbsp;dwPapers:=DeviceCapabilities(PChar(szDeviceName),PChar(szPort),DC_PAPERS,@lpwPapers,nil);<br> &nbsp;if (dwPapers&amp;lt;1)or(dwPapers&amp;gt;256) then<br> &nbsp; &nbsp; &nbsp;result:=-3;<br><br> &nbsp;//判断打印机是否支持自定义、A3、A4、B5纸型<br> &nbsp;while (dwPapers &amp;gt; 0) do<br> &nbsp;begin<br> &nbsp; &nbsp;case DWORD(lpwPapers[dwPapers]) of<br> &nbsp; &nbsp; &nbsp;DMPAPER_USER: fSupportUserDefind:=TRUE;<br> &nbsp; &nbsp; &nbsp;DMPAPER_A3: fSupportA3:=TRUE;<br> &nbsp; &nbsp; &nbsp;DMPAPER_A4: fSupportA4:=TRUE;<br> &nbsp; &nbsp; &nbsp;DMPAPER_B5: fSupportB5:=TRUE;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;dwPapers:=dwPapers-1;<br> &nbsp;end;<br><br> &nbsp;//取打印机的DeviceMode<br> &nbsp;a1:=nil;<br> &nbsp;if not(OpenPrinter(PChar(szDeviceName),hDriver,nil)) then result:=-4;<br> &nbsp;hMem:=GlobalAlloc(GPTR,DocumentPropertiesA(nHandle,hDriver,PChar(szDeviceName),a1^,a1^,0));<br> &nbsp;lpDevMode:=GlobalLock(hMem);<br> &nbsp;a1:=nil;<br> &nbsp;DocumentProperties(nHandle,hDriver,PChar(szDeviceName),lpDevMode^,a1^,DM_OUT_BUFFER);<br><br> &nbsp;//设置纸型或大小<br> &nbsp;i:=0;<br> &nbsp;if ((nWidth=2970)and(nHeight=4200))and(fSupportA3) then i:=1;<br> &nbsp;if ((nWidth=2100)and(nHeight=2970))and(fSupportA4) then i:=i+2;<br> &nbsp;if ((nWidth=1820)and(nHeight=2570))and(fSupportB5) then i:=i+3;<br><br> &nbsp;case i of<br> &nbsp; &nbsp;1: begin &nbsp; &nbsp;//A3<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=DM_PAPERSIZE;<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmPaperSize:=DMPAPER_A3;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;2: begin &nbsp; //A4<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=DM_PAPERSIZE;<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmPaperSize:=DMPAPER_A4;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;3: begin &nbsp; //B5<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=DM_PAPERSIZE;<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmPaperSize:=DMPAPER_B5;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; if fSupportUserDefind then<br> &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=DM_PAPERSIZE or DM_PAPERWIDTH or DM_PAPERLENGTH;<br>lpDevMode.dmPaperSize:=DMPAPER_USER;<br>lpDevMode.dmPaperWidth:=nWidth;<br>lpDevMode.dmPaperLength:=nHeight;<br> &nbsp; &nbsp; &nbsp; end<br> &nbsp; &nbsp; &nbsp; else<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=0;<br> &nbsp;end;<br><br> &nbsp;//设置方向<br> &nbsp;case nOrient of<br> &nbsp; &nbsp;0: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=lpDevMode.dmFields or DM_ORIENTATION;<br>lpDevMode.dmOrientation:=DMORIENT_PORTRAIT;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;1: begin<br> &nbsp; &nbsp; &nbsp; &nbsp; lpDevMode.dmFields:=lpDevMode.dmFields or DM_ORIENTATION;<br> &nbsp;lpDevMode.dmOrientation:=DMORIENT_LANDSCAPE;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp;end;<br><br> &nbsp;a1:=nil;<br> &nbsp;DocumentProperties(nHandle,hDriver,PChar(szDeviceName),a1^,lpDevMode^,DM_IN_BUFFER or DM_UPDATE);<br> &nbsp;GlobalUnlock(hMem);<br> &nbsp;GlobalFree(hMem);<br> &nbsp;ClosePrinter(hDriver);<br><br> &nbsp;result:=1;<br>end;<br>
 
、、、 &nbsp;、、、<br>无语ing
 
procedure GetPrintPaperList(var lstName, lstX, lstY: TStringList);<br>var<br> &nbsp;PaperNames: Pointer;<br> &nbsp;PaperSize: Pointer;<br> &nbsp;i, Rslt: integer;<br> &nbsp;hDMode: THandle;<br> &nbsp;Device, Driver, Port: array[0..255] of char;<br>begin<br> &nbsp;{$R-} //Turn range checking off.<br> &nbsp;lstName.Clear;<br> &nbsp;lstX.Clear;<br> &nbsp;lstY.Clear;<br> &nbsp;Printer.GetPrinter(Device, Driver, Port, hDMode);<br> &nbsp;Rslt := DeviceCapabilitiesA(Device, Port, DC_PAPERNAMES, nil, nil);<br> &nbsp;if Rslt &amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;GetMem(PaperNames, Rslt * 64);<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;if DeviceCapabilitiesA(Device, Port, DC_PAPERNAMES, PaperNames, nil) = -1<br> &nbsp; &nbsp; &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp;raise Exception.Create('DevCap Error');<br> &nbsp; &nbsp; &nbsp;for i := 0 to Rslt - 1 do<br> &nbsp; &nbsp; &nbsp; &nbsp;lstName.Add(StrPas(TPNames(PaperNames^)));<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;FreeMem(PaperNames, Rslt * 64);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;Rslt := DeviceCapabilitiesA(Device, Port, DC_PAPERSIZE, nil, nil);<br> &nbsp;if Rslt &amp;gt; 0 then<br> &nbsp;begin<br> &nbsp; &nbsp;GetMem(PaperSize, Rslt * 64);<br> &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp;if DeviceCapabilitiesA(Device, Port, DC_PAPERSIZE, PaperSize, nil) = -1<br> &nbsp; &nbsp; &nbsp; &nbsp;then<br> &nbsp; &nbsp; &nbsp; &nbsp;raise Exception.Create('DevCap Error');<br> &nbsp; &nbsp; &nbsp;for i := 0 to Rslt - 1 do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;lstX.Add(IntToStr(TPSize(PaperSize^).x));<br> &nbsp; &nbsp; &nbsp; &nbsp;lstY.Add(IntToStr(TPSize(PaperSize^).y));<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp;FreeMem(PaperSize, Rslt * 64);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;{$R+} //Turn range checking back on.<br>end;
 
接受答案了.
 
后退
顶部