代码语言
.
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
】
调用系统API函数直接连接pos打印机 打印
作者:
Dezai.CN
/ 发布于
2011/9/13
/
777
<div> using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Runtime.InteropServices; using System.Text; /// <summary> /// PrintPos 的摘要说明 /// </summary> public class PrintPos { private FileStream fs = null; [DllImport("kernel32.dll")]//调用系统API打印函数 public static extern IntPtr CreateFile ( string FileName, // file name uint DesiredAccess, // access mode uint ShareMode, // share mode uint SecurityAttributes, // Security Attributes uint CreationDisposition, // how to create uint FlagsAndAttributes, // file attributes int hTemplateFile // handle to template file ); public PrintPos() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 开始打印,本地打印机ltp1 端口打印 调用方法:PrintPos.PrintPage("dsdfdsfdsfsdfdsfsdfdsfs"); /// </summary> /// <param name="strPos"></param> /// <returns></returns> public string PrintPage(string strPos) { IntPtr iHandle = CreateFile("LPT1", 0x40000000, 0, 0, 3, 0, 0); //判断是否连接上打印机 -1为false if (iHandle.ToInt32() == -1) { return "没有连接到打印机"; } else { fs = new FileStream(iHandle, FileAccess.ReadWrite); //StreamReader sr = new StreamReader(fs); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.WriteLine(strPos, 0, 500); sw.Close(); fs.Close(); return "已经成功连接打印机"; } } } ******************************************************************************************************* using System.IO; using System.Runtime.InteropServices; using System.Text; namespace WindowsApplication2 { /// <summary> ///POSPrinter的摘要说明。 /// </summary> public class POSPrinter { const int OPEN_EXISTING=3; string prnPort="COM1"; [DllImport("kernel32.dll",CharSet=CharSet.Auto)] private static extern IntPtr CreateFile( string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); public POSPrinter() { // //TODO:在此处添加构造函数逻辑 // } public POSPrinter(string prnPort) { this.prnPort=prnPort;//打印机端口 } public string PrintLine(string str) { IntPtr iHandle=CreateFile(prnPort,0x40000000,0,0,OPEN_EXISTING,0,0); if(iHandle.ToInt32()==-1) { return"没有连接打印机"; } else { FileStream fs=new FileStream(iHandle,FileAccess.ReadWrite); StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.UTF8);//写数据 sw.WriteLine(str); //开钱箱 //sw.WriteLine(Chr(&H1B)&Chr(70)&Chr(0)&Chr(20)&Chr(&HA0)) sw.Close(); fs.Close(); return""; } } } } ********************************************************************************************************************* public class LPTControl { [StructLayout(LayoutKind.Sequential)] Private struct OVERLAPPED { int Internal; int InternalHigh; int Offset; int OffSetHigh; int hEvent; } [DllImport("kernel32.dll")] private static extern int CreateFile( string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile ); [DllImport("kernel32.dll")] private static extern bool WriteFile( int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, ref OVERLAPPED lpOverlapped ); [DllImport("kernel32.dll")] private static extern bool CloseHandle( int hObject ); private int iHandle; public bool Open() { iHandle=CreateFile("lpt1",0x40000000,0,0,3,0,0); if(iHandle !=-1) { return true; } else { return false; } } public bool Write(String Mystring) { if(iHandle !=-1) { int i; OVERLAPPED x; byte() mybyte=System.Text.Encoding.Default.GetBytes(Mystring); return WriteFile(iHandle,mybyte,mybyte.Length ref i,ref x); } else { Throw new Exception("端口未打开!"); } } public bool Close() { return CloseHandle(iHandle); } } ///////////////////////////////////////////////////////////////////////////// ========================================== 操作打印机端口的类 ======================================== using System; using System.Runtime.InteropServices; using System.IO; using System.Windows.Forms; namespace Printer { /// <summary> /// POSPrinter 的摘要说明。 /// </summary> public class POSPrinter { const int OPEN_EXISTING = 3; string prnPort ="LPT1"; [DllImport("kernel32.dll", CharSet=CharSet.Auto)] private static extern IntPtr CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition , int dwFlagsAndAttributes , int hTemplateFile); public POSPrinter() { // // TODO: 在此处添加构造函数逻辑 // } public POSPrinter(string prnPort) { this.prnPort=prnPort;//打印机端口 } /// <summary> /// 输出到打印机 /// </summary> /// <param name="str"></param> /// <returns></returns> /// <summary> /// 输出到打印机 /// </summary> /// <param name="str">要打印的内容</param> public void PrintLine(string str) { IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0); if(iHandle.ToInt32() == -1) { } else { FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据 sw.WriteLine(str); sw.Close(); fs.Close(); } } /// <summary> /// 打开钱箱 /// </summary> public void OpenCashBox() { IntPtr iHandle = CreateFile(prnPort, 0x40000000, 0, 0, OPEN_EXISTING, 0, 0); if(iHandle.ToInt32() == -1) { MessageBox.Show(iHandle.ToInt32().ToString()); } else { FileStream fs = new FileStream(iHandle, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据 sw.Close(); fs.Close(); } } } } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// ============================================ 调用代码 ============================================ private void PrinterPrint() { try { OleDbConnection OleConn=new OleDbConnection(function.OleDbconn_Str()); OleConn.Open();//打开数据库 Olesql="SELECT [TableStore].[StoreName] AS 商品名称, [TableStore].[SellNum] AS 数量, [TableStore].[GuestPrice] AS 单价, [TableStore].[SellNum]*[TableStore].[GuestPrice] AS 小记 FROM TableStore WHERE TableId=0 ORDER BY [TableStoreId] ASC"; OleDbDataAdapter da=new OleDbDataAdapter(Olesql,OleConn); DataTable dt=new DataTable(); DataSet ds=new DataSet(); da.Fill(ds); dt=ds.Tables[0]; OleConn.Close(); string[] s =new string [dt.DefaultView.Count]; posPrinter.PrintLine("**** 锦绣江南欢迎您的光临 ****"); posPrinter.PrintLine("交易时间:"+DateTime.Now.ToString()); posPrinter.PrintLine("电话:123456789 123456789"); posPrinter.PrintLine("********************************"); for(int i=0;i<dt.DefaultView.Count;i++) { posPrinter.PrintLine(dt.Rows[i]["商品名称"].ToString()+" "+dt.Rows[i]["数量"].ToString()+" "+dt.Rows[i]["单价"].ToString()+" "+dt.Rows[i]["小记"].ToString()); } posPrinter.PrintLine(PrintContent); posPrinter.PrintLine("********************************"); posPrinter.PrintLine("总计:"+SUMMoney.ToString("F")); posPrinter.PrintLine("现金:"+SKMoney.ToString("F")); posPrinter.PrintLine("找零:"+找零.Text); posPrinter.PrintLine(" "); textBox1.Text=PrintContent; posPrinter.OpenCashBox(); } catch { } } </div>
试试其它关键字
pos打印机
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3