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

2016-02-19 18:26 2 1 收藏

下面图老师小编要向大家介绍下在winnt以上版本中对打印机纸张的动态设置方法,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

【 tulaoshi.com - 编程语言 】

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

  library print;

  

  { Important note about DLL memory management: ShareMem must be the
    first unit in your library's USES clause AND your project's (select
    Project-View Source) USES clause if your DLL exports any procedures or
    functions that pass strings as parameters or function results. This
    applies to all strings passed to and from your DLL--even those that
    are nested in records and classes. ShareMem is the interface unit to
    the BORLNDMM.DLL shared memory manager, which must be deployed along
    with your DLL. To avoid using BORLNDMM.DLL, pass string information
    using PChar or ShortString parameters. }

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

  {$R *.res}
  Function GetDefaultPrinterName():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;

  Function addpage():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;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  Function changepape():Boolean; //改变当前的打印纸张设置
  var
    FormInfos : Array [1..1024] of Form_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;
    for i:= 1 to cReturned  do 通过轮寻找到300k的纸张的纸张类型排序号
      if FormInfos[i].pName = '300k' then j:=i;
    if j=0 then 
    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;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  Function MySetPrtInfo(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.
  

来源:https://www.tulaoshi.com/n/20160219/1617667.html

延伸阅读
通常我们办公一般都会使用到打印机这个外设,那么在一个局域网里面,我们怎样设置才能达到打印机资源共享呢?本文以现在主流系统win7为例,讲述怎样设置打印机共享。 取消禁用Guest用户,因为别人要访问安装打印机的按个电脑就是以guest账户访问的。 点击按钮,在上右键,选择,如下图所示: 在弹出的窗口中找到用户 双击,打开窗口,...
  XP系统共享打印机设置 一:将本地打印机设置成共享打印机,让局域网内其他用户也能够使用。 (1):选择开始菜单—打印机和传真。   (2):在弹出的窗口中选择共享这台打印机,然后点击确定。     (3):接着在弹出的打印机和传真窗口中右键单击打印机图标,选择共享,接着右...
标签: 电脑入门
我们在编写完文档后经常会想预览一下排版效果,但是如果你没有安装打印机就无法使用,那一定要买一台打印机吗?其实是不需要的,我们只要安装一下虚拟打印机就可以了,本教程为大家介绍一下虚拟打印机的安装方法。 那么如何安装虚拟打印机呢,下面介绍操作步骤。 步骤一: 单击开始设置打印机和传真添加打印机,弹出添加打印机向导对话框,单...
Airprint打印机设置教程   在使用Mac办公的时候难免会用到打印机打印相应的文件,那么Airprint一定会经常见到,那么Airprint打印机到底是什么呢?PC6苹果网小编为大家详细的介绍一下Airprint打印机设置教程。 Airprint打印机是什么: AirPrint 是可以让应用软件通过 Apple 的无驱动程序打印体系结构,创建无损打印输出的 App...
标签: 电脑入门
这是共享问题,以下方法做: 第一条,最好用:安装NWlink IPX/SPX/NetBIOS Compatible Transport Protocol协议; 第二步,运行一次向导,右击 网上邻居,属性,网络安装向导。 为什么运行了网络安装向导的Windows XP的共享资源就能被工作组中的其他计算机访问呢?其实运行网络安装向导只是一个表面现象,重要的是运行了它之后就修改了本地安...

经验教程

301

收藏

51
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部