打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
在winnt以上版本中对打印机纸张的动态设置方法

在winnt以上版本中对打印机纸张的动态设置方法

在一些票据打印程序中,经常需要动态调整打印机纸张的设置,而网上能够找到的dll只能在98下修改自定义纸张,
在nt以上版本中还无相应的dll。所以自己动手通过一些比较笨的办法实现动态修改纸张设置的功能。
  
  
  libraryprint;

  

  {ImportantnoteaboutDLLmemorymanagement:ShareMemmustbethe
  firstunitinyourlibrary'sUSESclauseANDyourproject's(select
  Project-ViewSource)USESclauseifyourDLLexportsanyproceduresor
  functionsthatpassstringsasparametersorfunctionresults.This
  appliestoallstringspassedtoandfromyourDLL--eventhosethat
  arenestedinrecordsandclasses.ShareMemistheinterfaceunitto
  theBORLNDMM.DLLsharedmemorymanager,whichmustbedeployedalong
  withyourDLL.ToavoidusingBORLNDMM.DLL,passstringinformation
  usingPCharorShortStringparameters.}

  uses
  SysUtils,
  WinSpool,
  Windows,
  Messages,
  Classes;
  var
  hPrinter:THandle;

  {$R*.res}
  FunctionGetDefaultPrinterName():PChar;//得到默认的打印机名称
  var
  sIniFile,sSection,sKeyName,p,q:PChar;
  begin
  sIniFile:='win.ini';
  sSection:='windows';
  sKeyName:='device';
  p:=StrAlloc(80);
  q:=StrAlloc(80);
  GetPrivateProfileString(sSection,sKeyName,nil,p,80,sIniFile);
  StrLCopy(q,p,(strscan(p,',')-p));
  Result:=q;
  end;

  Functionaddpage():Boolean;//添加一打印纸张类型名称为300k
  var
  FormInfo:TFormInfo1;
  PaperSize:TSize;
  PaperRect:TRect;
  begin
  FormInfo.Flags:=FORM_USER;
  FormInfo.pName:=PChar('300k');
  PaperSize.cx:=100000;
  PaperSize.cy:=100000;//300k纸张的默认大小1000mm*1000mm
  PaperRect.Left:=1;
  PaperRect.Top:=1;
  PaperRect.Right:=100000;
  PaperRect.Bottom:=100000;//纸张的边距
  FormInfo.Size:=PaperSize;
  FormInfo.ImageableArea:=PaperRect;
  AddForm(hPrinter,1,@FormInfo);
  Result:=True;
  end;

  Functionchangepape():Boolean;//改变当前的打印纸张设置
  var
  FormInfos:Array[1..1024]ofForm_Info_1;
  cbNeeded,cReturned,neededsize:DWORD;
  i,j:integer;
  ppo:PRINTER_INFO_2;
  dev:PDeviceMode;
  begin
  EnumForms(hPrinter,1,Nil,0,cbNeeded,cReturned);
  EnumForms(hPrinter,1,@FormInfos,cbNeeded,cbNeeded,cReturned);
  j:=0;
  fori:=1tocReturneddo通过轮寻找到300k的纸张的纸张类型排序号
  ifFormInfos[i].pName='300k'thenj:=i;
  ifj=0then
  begin
  addpage();
  j:=cReturned 1;
  end;//如无300k纸张则添加
  GetPrinter(hPrinter,2,Nil,0,@NeededSize);
  GetPrinter(hPrinter,2,@ppo,NeededSize,@NeededSize);
  dev:=ppo.pDevMode;
  dev.dmPaperSize:=j;
  ppo.pDevMode:=dev;
  SetPrinter(hPrinter,2,@ppo,0);//定义打印机列表并选择300k纸张
  Result:=True;
  end;

  FunctionMySetPrtInfo(P:Pchar;W:Integer;L:Integer):Boolean;stdcall;
  //设置打印机的纸张类型为300k,并动态修改300k的大小和边距
  var
  FormInfo:TFormInfo1;
  PaperSize:TSize;
  PaperRect:TRect;
  begin
  OpenPrinter(GetDefaultPrinterName,hPrinter,nil);
  changepape();//改变纸张类型定义
  FormInfo.Flags:=FORM_USER;
  FormInfo.pName:='300K';
  PaperSize.cx:=W*100;
  PaperSize.cy:=L*100;
  PaperRect.Left:=0;
  PaperRect.Top:=0;
  PaperRect.Right:=W*100;
  PaperRect.Bottom:=L*100;
  FormInfo.Size:=PaperSize;
  FormInfo.ImageableArea:=PaperRect;
  SetForm(hPrinter,'300k',1,@FormInfo);//设置纸张大小
  ClosePrinter(hPrinter);
  Result:=True;
  end;

  exports
  MySetPrtInfo;
  begin
  end.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi 在Windows环境下自定义打印纸张的尺寸(自定义页面)
C#获取打印机状态的问题
VB 报表页面设置
A4 纸张创造的神奇/组图
使用ComponenetOne C1Report实现报表打印的页面大小自定制
c# PrintDocument 设置自定义纸张大小的示例
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服