博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ESC/P打印:程序控制打印机自动进退纸
阅读量:6115 次
发布时间:2019-06-21

本文共 5054 字,大约阅读时间需要 16 分钟。

打印连续纸张的时候,经常会遇到需要自动进退纸的功能,譬如下列场景:

1. 打印完一个小标;(自动进纸)
2. 用户撕断打印出来的小标;
3. (自动退纸,纸张归位) 接着打印下一个小标;

 

下列代码在爱普生LQ 730k打印机上测试通过:

1: //使用示例
2: new PrintDirect("PrinterName").PrintESC(1);//打印后进纸
3: new PrintDirect("PrinterName").PrintESC(0);//打印前退纸

 

源代码:

1: 
2:     [StructLayout(LayoutKind.Sequential)]
3:     public struct DOCINFO
4:     {
5:         [MarshalAs(UnmanagedType.LPWStr)]
6:         public string pDocName;
7:         [MarshalAs(UnmanagedType.LPWStr)]
8:         public string pOutputFile;
9:         [MarshalAs(UnmanagedType.LPWStr)]
10:         public string pDataType;
11:     }
12: 
13:     public class PrintDirect
14:     {
15:         private string PrintPort { get; set; }
16:         public PrintDirect(string port)
17:         {
18:             this.PrintPort = port;
19:         }
20: 
21:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
22:         public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault);
23: 
24:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
25:         public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);
26: 
27:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
28:         public static extern long StartPagePrinter(IntPtr hPrinter);
29: 
30:         [DllImport("winspool.drv", CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
31:         public static extern long WritePrinter(IntPtr hPrinter, string data, int buf, ref int pcWritten);
32: 
33:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
34:         public static extern long EndPagePrinter(IntPtr hPrinter);
35: 
36:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
37:         public static extern long EndDocPrinter(IntPtr hPrinter);
38: 
39:         [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
40:         public static extern long ClosePrinter(IntPtr hPrinter);
41: 
42:         public void PrintESC(int type)
43:         {
44:             System.IntPtr lhPrinter = new System.IntPtr();
45:             DOCINFO di = new DOCINFO();
46:             int pcWritten = 0;
47:             di.pDocName = "进退纸Document";
48:             di.pDataType = "RAW";
49: 
50:             try
51:             {
52:                 PrintDirect.OpenPrinter(this.PrintPort, ref lhPrinter, 0);
53:                 if (lhPrinter == IntPtr.Zero)
54:                 {
55:                     MessageBox.Show(string.Format("没有连接打印机或者打印机端口不是{0}!", this.PrintPort),
56:                         "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
57:                     return;
58:                 }
59:                 PrintDirect.StartDocPrinter(lhPrinter, 1, ref di);
60:                 PrintDirect.StartPagePrinter(lhPrinter);
61:                 string send = string.Empty;
62:                 for (int j = 0; j < 3; j++)
63:                 {
64:                     switch (type)
65:                     {
66:                         case 0:
67:                             send = "" + (char)(27) + (char)(64) + (char)(27) + 'j' + (char)(180);//退纸
68:                             break;
69:                         case 1:
70:                             send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(180);//进纸
71:                             break;
72:                         case 2:
73:                             send = "" + (char)(27) + (char)(64) + (char)(12);   //换行(未测试)
74:                             break;
75:                         default:
76:                             send = "" + (char)(27) + (char)(64) + (char)(12);   //换行(未测试)
77:                             break;
78:                     }
79:                     byte[] buf = new byte[80];
80:                     for (int i = 0; i < send.Length; i++)
81:                     {
82:                         buf[i] = (byte)send[i];
83:                     }
84:                     PrintDirect.WritePrinter(lhPrinter, send, send.Length, ref pcWritten);
85:                 }
86:             }
87:             catch (Exception e)
88:             {
89:                 MessageBox.Show(e.Message);
90:             }
91:             finally
92:             {
93:                 if (lhPrinter != IntPtr.Zero)
94:                 {
95:                     PrintDirect.EndPagePrinter(lhPrinter);
96:                     PrintDirect.EndDocPrinter(lhPrinter);
97:                     PrintDirect.ClosePrinter(lhPrinter);
98:                 }
99:             }
100:         }
101:     }

 

标签: , , ,

转载于:https://www.cnblogs.com/happyhippy/archive/2011/06/26/2090468.html

你可能感兴趣的文章
jqgrid滚动条宽度/列显示不全问题
查看>>
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
angularjs表达式中的HTML内容,如何不转义,直接表现为html元素
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
[Usaco2015 dec]Max Flow
查看>>
javascript性能优化
查看>>
多路归并排序之败者树
查看>>
java连接MySql数据库
查看>>
转:Vue keep-alive实践总结
查看>>
android studio修改新项目package名称
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Hadoop2.5.0 搭建实录
查看>>
实验吧 recursive write up
查看>>
High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件
查看>>
go test命令參数问题
查看>>
linux 搜索文本
查看>>
超实用Mac软件分享(二)
查看>>
Android JSON数据解析
查看>>