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

冰淇淋唯爱2ne1

冰淇淋唯爱2ne1

2016-02-19 18:26

下面图老师小编要向大家介绍下在winnt以上版本中对打印机纸张的动态设置方法,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!
在一些票据打印程序中,常常需要动态调整打印机纸张的设置,而网上能够找到的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. }

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

  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;

  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;

  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://m.tulaoshi.com/bianchengyuyan/)
展开更多 50%)
分享

猜你喜欢

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

编程语言 网络编程
在winnt以上版本中对打印机纸张的动态设置方法

WPS在不同系统中打印机纸张设置技巧

办公软件
WPS在不同系统中打印机纸张设置技巧

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

打印机设置16开纸张

电脑入门
打印机设置16开纸张

打印机纸张大小怎么设置

打印机 电脑 计算机基础
打印机纸张大小怎么设置

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

北洋打印机如何设置纸张规格大小?

生活常识
北洋打印机如何设置纸张规格大小?

打印机共享设置教程 打印机怎么设置共享

打印机 电脑
打印机共享设置教程  打印机怎么设置共享

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

WPS在平板上如何编辑Word图片

WPS在平板上如何编辑Word图片

delphi的接口的意义是不是跟C++和Java的不同呢

delphi的接口的意义是不是跟C++和Java的不同呢
下拉加载更多内容 ↓