Windows Vista.NET 1.0Visual Studio .NET 2003Windows 2003.NET 1.1Windows 2000Windows XP中级开发Visual StudioWindows.NETC#
HP PCL PCL5 打印





3.00/5 (14投票s)
Jan 13, 2005

113228

1500
C# 中的 HP PCL PCL5 打印。
引言
通过这段代码,你可以使用 WIN32 API 调用直接打印到打印机,从而应该能够以最大速度打印。此外,代码还展示了如何将惠普 PCL5 代码发送到打印机。
Using the Code
- 在你的项目中添加一个窗体。
- 在窗体中添加一个按钮。
- 将按钮的
Name
属性更改为btnString
。 - 在窗体中添加另一个按钮。
- 将按钮的
Name
属性更改为btnSquare
。 - 为按钮编写以下代码
private void btnString_Click(object sender, System.EventArgs e) { //PCL5 Commands to print a string string st1 = "Printing a string test."; Printing.DirectPrinter.SendToPrinter ("Printing a string",st1, "\\\\192.168.1.101\\hpl"); } private void btnSquare_Click(object sender, System.EventArgs e) { // PCL5 Commands to print a square // Moves the cursor 900 dots (3 inches at 300 dpi) in from the // left margin, and 600 dots (2 inches at 300 dpi) down from // the top margin. string st1="*p900x600Y"; // Using the print model commands for rectangle dimensions, // "600a" specifies a rectangle with a horizontal size // or width of 600 dots, and "6b" specifies // a vertical size or height of 6 dots. The 0P selects the solid black // rectangular area fill. st1+="*c600a6b0P"; // Specifies a rectangle with width of 6 dots, height of 600 dots, and a // fill pattern of solid black. st1+="*c6a600b0P"; // Moves the current cursor position to 900 dots, from the left margin and // 1200 dots down from the top margin. st1+="*p900x1200Y"; // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a // fill pattern of solid black. st1+="*c606a6b0P"; // Moves the current cursor position to 1500 dots from the left margin and // 600 dots down from the top margin. st1+="*p1500x600Y"; // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a // fill pattern of solid black. st1+="*c6a600b0P"; // Send a form feed character to the printer st1+="\f"; Printing.DirectPrinter.SendToPrinter ("Printing a square",st1, "\\\\192.168.1.101\\hpl"); }
现在
- 在项目中添加一个名为 DirectPrinter.cs 的新类。
// This code is based on a sample Written 17th October 2002 By <a href="mailto:csharpconsulting@hotmail.com">J O'Donnell</a> // Adapted from Microsoft Support article Q298141 using System; //For PCL5 using System.Text; using System.Runtime.InteropServices; [StructLayout( LayoutKind.Sequential)] public struct DOCINFO { [MarshalAs(UnmanagedType.LPWStr)] public stringpDocName; [MarshalAs(UnmanagedType.LPWStr)] public stringpOutputFile; [MarshalAs(UnmanagedType.LPWStr)] public string pDataType; } namespace Printing { public class DirectPrinter { public DirectPrinter() { // // TODO: Add constructor logic here // } [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, CallingConvention=CallingConvention.StdCall )] public static extern long OpenPrinter(string pPrinterName, ref IntPtr phPrinter, int pDefault); [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false, CallingConvention=CallingConvention.StdCall )] public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo); [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] public static extern long StartPagePrinter(IntPtr hPrinter); [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] public static extern long WritePrinter(IntPtr hPrinter,string data, int buf,ref int pcWritten); [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] public static extern long EndPagePrinter(IntPtr hPrinter); [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] public static extern long EndDocPrinter(IntPtr hPrinter); [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true, CallingConvention=CallingConvention.StdCall )] public static extern long ClosePrinter(IntPtr hPrinter); public static void SendToPrinter(string jobName, string PCL5Commands, string printerName) { System.IntPtr lhPrinter=new System.IntPtr(); DOCINFO di = new DOCINFO(); int pcWritten=0; di.pDocName=jobName; di.pDataType="RAW"; //lhPrinter contains the handle for the printer opened //If lhPrinter is 0 then an error has occurred // This code assumes you have a printer at share \\192.168.1.101\hpl // This code sends Hewlett Packard PCL5 codes to the printer //OpenPrinter("\\\\192.168.1.101\\hpl",ref lhPrinter,0); OpenPrinter(printerName,ref lhPrinter,0); StartDocPrinter(lhPrinter,1,ref di); StartPagePrinter(lhPrinter); WritePrinter(lhPrinter,PCL5Commands,PCL5Commands.Length,ref pcWritten); EndPagePrinter(lhPrinter); EndDocPrinter(lhPrinter); ClosePrinter(lhPrinter); } } }
如果您有任何问题,请在下方留言。
历史
- 版本 1.0 - 初始发布
许可证
本文没有明确的许可证附加到它,但可能在文章正文或下载文件中包含使用条款。如有疑问,请通过下面的讨论区联系作者。作者可能使用的许可证列表可以在 这里 找到。