现在的位置: 首页 > 综合 > 正文

直接把结果输出到打印机(C#)

2014年03月13日 ⁄ 综合 ⁄ 共 4031字 ⁄ 字号 评论关闭
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年6月15日 10点49分57秒


下面的代码可以通过调用WIN32 API让你直接输出到打印机,因而可以达到最快的速度进行打印,而不是等待Windows的打印系统。此外,示例代码还说明了如何向打印机发送PCL代码。

// PrintDirect.cs<br /> // 本文参考了Microsoft Support 文档号:Q298141<br /> // 本代码假定你在file://192.168.1.101/hpl存在共享打印机<br /> // 本代码示例了如何向打印机发送Hewlett Packard PCL5代码直接在页面中央打印出一个矩形。</p> <p>using System;<br /> using System.Text;<br /> using System.Runtime.InteropServices;</p> <p>[StructLayout( LayoutKind.Sequential)]<br /> public struct DOCINFO<br /> {<br /> [MarshalAs(UnmanagedType.LPWStr)]public string pDocName;<br /> [MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;<br /> [MarshalAs(UnmanagedType.LPWStr)]public string pDataType;<br /> }</p> <p>public class PrintDirect<br /> {<br /> [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,<br /> CallingConvention=CallingConvention.StdCall )]<br /> public static extern long OpenPrinter(string pPrinterName,ref IntPtr phPrinter,<br /> int pDefault);<br /> [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,<br /> CallingConvention=CallingConvention.StdCall )]<br /> public static extern long StartDocPrinter(IntPtr hPrinter, int Level,<br /> ref DOCINFO pDocInfo);</p> <p> [ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,<br /> CallingConvention=CallingConvention.StdCall)]<br /> public static extern long StartPagePrinter(IntPtr hPrinter);<br /> [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,<br /> CallingConvention=CallingConvention.StdCall)]<br /> public static extern long WritePrinter(IntPtr hPrinter,string data,<br /> int buf,ref int pcWritten);</p> <p> [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,<br /> CallingConvention=CallingConvention.StdCall)]<br /> public static extern long EndPagePrinter(IntPtr hPrinter);</p> <p> [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,<br /> CallingConvention=CallingConvention.StdCall)]<br /> public static extern long EndDocPrinter(IntPtr hPrinter);</p> <p> [ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,<br /> CallingConvention=CallingConvention.StdCall )]<br /> public static extern long ClosePrinter(IntPtr hPrinter);<br /> }</p> <p>public class App<br /> {<br /> public static void Main ()<br /> {<br /> System.IntPtr lhPrinter=new System.IntPtr();</p> <p> DOCINFO di = new DOCINFO();<br /> int pcWritten=0;<br /> string st1;</p> <p> // text to print with a form feed character<br /> st1="This is an example of printing directly to a printer/f";<br /> di.pDocName="my test document";<br /> di.pDataType="RAW";</p> <p> // the /x1b means an ascii escape character<br /> st1="/x1b*c600a6b0P/f";<br /> //lhPrinter contains the handle for the printer opened<br /> //If lhPrinter is 0 then an error has occured<br /> PrintDirect.OpenPrinter("////192.168.1.101//hpl",ref lhPrinter,0);<br /> PrintDirect.StartDocPrinter(lhPrinter,1,ref di);<br /> PrintDirect.StartPagePrinter(lhPrinter);<br /> try<br /> {<br /> // Moves the cursor 900 dots (3 inches at 300 dpi) in from the left margin, and<br /> // 600 dots (2 inches at 300 dpi) down from the top margin.<br /> st1="/x1b*p900x600Y";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);</p> <p> // Using the print model commands for rectangle dimensions, "600a" specifies a rectangle<br /> // with a horizontal size or width of 600 dots, and "6b" specifies a vertical<br /> // size or height of 6 dots. The 0P selects the solid black rectangular area fill.<br /> st1="/x1b*c600a6b0P";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);</p> <p> // Specifies a rectangle with width of 6 dots, height of 600 dots, and a<br /> // fill pattern of solid black.<br /> st1="/x1b*c6a600b0P";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);<br /> // Moves the current cursor position to 900 dots, from the left margin and<br /> // 1200 dots down from the top margin.<br /> st1="/x1b*p900x1200Y";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);<br /> // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a</p> <p> // fill pattern of solid black.<br /> st1="/x1b*c606a6b0P";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);</p> <p> // Moves the current cursor position to 1500 dots from the left margin and<br /> // 600 dots down from the top margin.<br /> st1="/x1b*p1500x600Y";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);</p> <p> // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a<br /> // fill pattern of solid black.<br /> st1="/x1b*c6a600b0P";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);<br /> // Send a form feed character to the printer<br /> st1="/f";<br /> PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);<br /> }<br /> catch (Exception e)<br /> {<br /> Console.WriteLine(e.Message);<br /> }</p> <p> PrintDirect.EndPagePrinter(lhPrinter);<br /> PrintDirect.EndDocPrinter(lhPrinter);<br /> PrintDirect.ClosePrinter(lhPrinter);<br /> }<br /> }<br />

抱歉!评论已关闭.