Delphi dynamic adjustment printer paper

xiaoxiao2021-03-06  14

In some ticket print printers, it is often necessary to dynamically adjust the settings of the printer paper, and the DLL that can be found online can only modify custom paper at 98, and there is no corresponding DLL in NT or later. So I do my own than some stupid way to realize the function of dynamically modify the paper settings.

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; // Get the default printer name 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; // Add a printing sheet type name is 300kvar FormInfo: TFormInfo1; PaperSize: TSize; PaperRect: TRect; begin FormInfo.Flags: = FORM_USER; FormInfo.pName: = PChar ( '300k'); PaperSize .cx: = 100000; PAPERSIZE.CY: = 100000; // 300K paper default size 1000mm * 1000mm PaperRect.Left: = 1; paperRect.top: = 1; paperRect.right: = 100000; paperRect.Bottom: = 100000 // paper margin Forminfo.size: = papersize; Forminfo.imageableArea: = PaperRect; addform (Hprinter, 1, @formInfo; result: = true; end; function change;): boolean; // Change the current printing paper setting 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 IFOS type sequence number if forminfos [i] .pname = '300K' THEN J: = i; if j = 0 THEN Begin address (); j: = creturned 1; end; // Add GetPrinter if there is no 300K paper (HPR Inter, 2, NIL, 0, @needededsize; getprinter (Hprinter, 2, @ppo, needededsize, @needededsize); dev: = ppo.pdevmode; dev.dmpapersize: = j; ppo.pdevmode: = dev; setprinter Hprinter, 2, @ ppo, 0); // Define the list of printers and select 300K Paper Result: = true;

Function MySetPRTITINFO (P: PCHAR; W: INTEGER; L: Integer): Boolean; stdcall; // Set the printer's paper type 300K, and dynamically modify the size of 300K and the margin Var Forminfo: TFormInfo1; Papersize: Tsize; PaperRect: TRECT; begin openprinter (getDefaultprintername, hprinter, nil); changePape (); // change the paper type definition formfo.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); // Set paper size Closeprinter (hprinter); result: = true; end; exports mysetprtinfo; beginend.

转载请注明原文地址:https://www.9cbs.com/read-50197.html

New Post(0)