Directly output the result to the printer (C #)

xiaoxiao2021-03-06  128

Author: Meng will come from: [it] would be a wonderful world Meng Date: 2003 at 10:49:57 on on June 15

The following code can make you directly output to the printer by calling the Win32 API, thus achieving the fastest speed, rather than waiting for the Windows printing system. In addition, sample code also illustrates how to send PCL code to the printer.

//Printdirect.cs // This article refers to the Microsoft Support document number: Q298141 / / This code assumes that you have a shared printer in File: //192.168.1.101/HPL, how to send the Hewlett Packard PCL5 code to the printer Print a rectangle in the center of the page.

using System; using System.Text; using System.Runtime.InteropServices; [StructLayout (LayoutKind.Sequential)] public struct DOCINFO {[MarshalAs (UnmanagedType.LPWStr)] public string pDocName; [MarshalAs (UnmanagedType.LPWStr)] public string pOutputFile ; [MarshalAs (UnmanagedType.LPWStr)] public string pDataType;} public class PrintDirect {[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, Exactsp elling = 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 class App {public static void Main () {System.IntPtr lhPrinter = new System.IntPtr (); DOCINFO di = new DOCINFO (); int pcWritten = 0; string st1; // text to print with a form feed transamples; di.pdocname = "my test document"; di.pdattype = "rAW"; // the / x1b means an ascii escape character st1 = "/ x1b * c600a6b0P / f"; // lhPrinter contains the handle for the printer opened // If lhPrinter is 0 then an error has occured PrintDirect.OpenPrinter ( "192.168.1.101 // HPL ", Ref lhprinter, 0); PrintDirect.startDocprinter (lhprinter, 1, ref di); printdirect.startPageprinter (lhprinter); try {// moves the cursor 900 Dots (3 inches 300 dpi) in from the left Margin, And // 600 DOTS (2 INCHES AT 300 DPI) Down from the top margin. ST1 = "/ x1b * p900x600y"; PrintDirect.writeprinter (Lhprinter, ST1, ST1.LENGTH, REF PCWRITTEN); // 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 = "/ x1b * c600a6b0P "; PrintDirect.WritePrinter (lhPrinter, st1, st1.Length, ref pcWritten);. // Specifies a rectangle with width of 6 dots, height of 600 dots, and a // fill pattern of solid black st1 =" / x1b * c6a600b0P "; PrintDirect.WritePrinter (lhPrinter, st1, st1.Length, ref pcWritten); // Moves the current cursor position to 900 dots, from the left margin and // 1200 dots down from the top margin st1 =. "/ x1b * p900x1200y";

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

New Post(0)