电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

直接把结果输出到打印机


发布日期:2020/4/11
 

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

// PrintDirectcs

// 本文参考了Microsoft Support 文档号Q

// 本代码假定你在file:///hpl存在共享打印机

// 本代码示例了如何向打印机发送Hewlett Packard PCL代码直接在页面中央打印出一个矩形

using System;

using SystemText;

using SystemRuntimeInteropServices;

[StructLayout( LayoutKindSequential)]

public struct DOCINFO

{

[MarshalAs(UnmanagedTypeLPWStr)]public string pDocName;

[MarshalAs(UnmanagedTypeLPWStr)]public string pOutputFile;

[MarshalAs(UnmanagedTypeLPWStr)]public string pDataType;

}

public class PrintDirect

{

[ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=false

CallingConvention=CallingConventionStdCall )]

public static extern long OpenPrinter(string pPrinterNameref IntPtr phPrinter

int pDefault);

[ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=false

CallingConvention=CallingConventionStdCall )]

public static extern long StartDocPrinter(IntPtr hPrinter int Level

ref DOCINFO pDocInfo);

[ DllImport(winspooldrvCharSet=CharSetUnicodeExactSpelling=true

CallingConvention=CallingConventionStdCall)]

public static extern long StartPagePrinter(IntPtr hPrinter);

[ DllImport( winspooldrvCharSet=CharSetAnsiExactSpelling=true

CallingConvention=CallingConventionStdCall)]

public static extern long WritePrinter(IntPtr hPrinterstring data

int bufref int pcWritten);

[ DllImport( winspooldrv CharSet=CharSetUnicodeExactSpelling=true

CallingConvention=CallingConventionStdCall)]

public static extern long EndPagePrinter(IntPtr hPrinter);

[ DllImport( winspooldrvCharSet=CharSetUnicodeExactSpelling=true

CallingConvention=CallingConventionStdCall)]

public static extern long EndDocPrinter(IntPtr hPrinter);

[ DllImport(winspooldrvCharSet=CharSetUnicodeExactSpelling=true

CallingConvention=CallingConventionStdCall )]

public static extern long ClosePrinter(IntPtr hPrinter);

}

public class App

{

public static void Main ()

{

SystemIntPtr lhPrinter=new SystemIntPtr();

DOCINFO di = new DOCINFO();

int pcWritten=;

string st;

// text to print with a form feed character

st=This is an example of printing directly to a printer\f;

dipDocName=my test document;

dipDataType=RAW;

// the \xb means an ascii escape character

st=\xb*cabP\f;

//lhPrinter contains the handle for the printer opened

//If lhPrinter is then an error has occured

PrintDirectOpenPrinter(\\\\\\hplref lhPrinter);

PrintDirectStartDocPrinter(lhPrinterref di);

PrintDirectStartPagePrinter(lhPrinter);

try

{

// Moves the cursor dots ( inches at dpi) in from the left margin and

// dots ( inches at dpi) down from the top margin

st=\xb*pxY;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Using the print model commands for rectangle dimensions a specifies a rectangle

// with a horizontal size or width of dots and b specifies a vertical

// size or height of dots The P selects the solid black rectangular area fill

st=\xb*cabP;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Specifies a rectangle with width of dots height of dots and a

// fill pattern of solid black

st=\xb*cabP;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Moves the current cursor position to dots from the left margin and

// dots down from the top margin

st=\xb*pxY;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Specifies a rectangle with a width of dots a height of dots and a

// fill pattern of solid black

st=\xb*cabP;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Moves the current cursor position to dots from the left margin and

// dots down from the top margin

st=\xb*pxY;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Specifies a rectangle with a width of dots a height of dots and a

// fill pattern of solid black

st=\xb*cabP;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

// Send a form feed character to the printer

st=\f;

PrintDirectWritePrinter(lhPrinterststLengthref pcWritten);

}

catch (Exception e)

{

ConsoleWriteLine(eMessage);

}

PrintDirectEndPagePrinter(lhPrinter);

PrintDirectEndDocPrinter(lhPrinter);

PrintDirectClosePrinter(lhPrinter);

}

}

上一篇:使用Stopwatch类进行速度测试

下一篇:session定义使用和丢失问题小结