代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
直接把结果输出到打印机
作者:
孟宪会
/ 发布于
2011/5/3
/
939
下面的代码可以通过调用WIN32 API让你直接输出到打印机,因而可以达到最快的速度进行打印,而不是等待Windows的打印系统。此外,示例代码还说明了如何向打印机发送PCL代码。
<div><!--StartFragment-->// PrintDirect.cs // 本文参考了Microsoft Support 文档号:Q298141 // 本代码假定你在file://192.168.1.101/hpl存在共享打印机 // 本代码示例了如何向打印机发送Hewlett Packard PCL5代码直接在页面中央打印出一个矩形。 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,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 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 character st1="This is an example of printing directly to a printer\f"; di.pDocName="my test document"; di.pDataType="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 at 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"; PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten); // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a // fill pattern of solid black. st1="\x1b*c606a6b0P"; PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten); // Moves the current cursor position to 1500 dots from the left margin and // 600 dots down from the top margin. st1="\x1b*p1500x600Y"; PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten); // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a // fill pattern of solid black. st1="\x1b*c6a600b0P"; PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten); // Send a form feed character to the printer st1="\f"; PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten); } catch (Exception e) { Console.WriteLine(e.Message); } PrintDirect.EndPagePrinter(lhPrinter); PrintDirect.EndDocPrinter(lhPrinter); PrintDirect.ClosePrinter(lhPrinter); } } </div>
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
孟宪会
贡献的其它代码
(
15
)
.
得到计算机硬件信息
.
从Window系统托盘控制Windows服务
.
遍历主机的所有IP地址
.
把图象文件转换成XML格式文件
.
编写随Windows启动运行的程序
.
遍历驱动器
.
利用WebClient类向服务器上载文件
.
改变显示器的分辨率
.
用C#实时获取CPU利用率
.
直接把结果输出到打印机
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3