当前位置:首页 > Windows程序 > 正文

C#后台挪用LPT1端口实现小票机打印要领。

2024-03-31 Windows程序

public class POSPrinter { const int OPEN_EXISTING = 3; string prnPort = "LPT1"; [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); public POSPrinter() { } public POSPrinter(string prnPort) { this.prnPort = prnPort;//打印机端口 } public string PrintLine(string str) { IntPtr iHandle = CreateFile(prnPort, 0x50000000, 0, 0, OPEN_EXISTING, 0, 0); if (iHandle.ToInt32() == -1) { Console.WriteLine(iHandle.ToString()); return "没有连接打印机或者打印机端口不是LPT1"; } else { Console.WriteLine(iHandle.ToString()); FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); sw.WriteLine(" 小票单"); sw.WriteLine(); sw.WriteLine(str); sw.WriteLine("打印内容"); sw.WriteLine("---------------------------"); sw.Close(); fs.Close(); return "打印告成!"; } } }

  直接挪用PrintLine();要领进行打印具体需要的参数和打印格局大家自行调解。

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/35860.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494590访问次数
  • 建站天数
  • 友情链接