C#小票打印机
使用的佳博GP-5890XIII
http://www.cnblogs.com/lovenan/p/3217448.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JBPrinter
{
public partial class Form1 : Form
{
private IntPtr Gp_IntPtr; //驱动打印句柄
private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
private string iPaddress;
private System.Windows.Forms.PrintPreviewDialog printv_pos = null;
private System.Drawing.Printing.PrintDocument printd_pos = null;
public Form1()
{
InitializeComponent();
iPaddress = txt_Ip1.Text.Trim() + "." + txt_Ip2.Text.Trim() + "." + txt_Ip3.Text.Trim() + "." + txt_Ip4.Text.Trim();
}
private void btn_dk_Click(object sender, EventArgs e)
{
LoadPOSDll PosPrint = new LoadPOSDll();
//POS_COM_DTR_DSR 0x00 流控制为DTR/DST
//POS_COM_RTS_CTS 0x01 流控制为RTS/CTS
//POS_COM_XON_XOFF 0x02 流控制为XON/OFF
//POS_COM_NO_HANDSHAKE 0x03 无握手
//POS_OPEN_PARALLEL_PORT 0x12 打开并口通讯端口
//POS_OPEN_BYUSB_PORT 0x13 打开USB通讯端口
//POS_OPEN_PRINTNAME 0X14 打开打印机驱动程序
//POS_OPEN_NETPORT 0x15 打开网络接口
if (PosPrint.OpenNetPort(iPaddress))//当参数nParam的值为POS_OPEN_NETPORT时,表示打开指定的网络接口,如“192.168.10.251”表示网络接口IP地址,打印时参考
{
Gp_IntPtr = PosPrint.POS_IntPtr;
txt_OPenState.Text = "打开网口成功!";
}
else
{
txt_OPenState.Text = "失败!";
}
}
#region 端口地址修改
private void txt_Ip1_TextChanged(object sender, EventArgs e)
{
iPaddress = txt_Ip1.Text.Trim() + "." + txt_Ip2.Text.Trim() + "." + txt_Ip3.Text.Trim() + "." + txt_Ip4.Text.Trim();
}
private void txt_Ip2_TextChanged(object sender, EventArgs e)
{
iPaddress = txt_Ip1.Text.Trim() + "." + txt_Ip2.Text.Trim() + "." + txt_Ip3.Text.Trim() + "." + txt_Ip4.Text.Trim();
}
private void txt_Ip3_TextChanged(object sender, EventArgs e)
{
iPaddress = txt_Ip1.Text.Trim() + "." + txt_Ip2.Text.Trim() + "." + txt_Ip3.Text.Trim() + "." + txt_Ip4.Text.Trim();
}
private void txt_Ip4_TextChanged(object sender, EventArgs e)
{
iPaddress = txt_Ip1.Text.Trim() + "." + txt_Ip2.Text.Trim() + "." + txt_Ip3.Text.Trim() + "." + txt_Ip4.Text.Trim();
}
#endregion
#region 打印机状态检测(请先检网关)
private void btn_QueryStatus_Click(object sender, EventArgs e)
{
Byte res = new Byte();
int ret = LoadPOSDll.POS_NETQueryStatus(iPaddress, out res);
StringBuilder sb = new StringBuilder();
if ((res & 0x10) == 0x10)
sb.AppendLine("打印机出错!");
if ((res & 0x02) == 0x02)
sb.AppendLine("打印机脱机!");
if ((res & 0x04) == 0x04)
sb.AppendLine("上盖打开!");
if ((res & 0x20) == 0x20)
sb.AppendLine("切刀出错!");
if ((res & 0x40) == 0x40)
sb.AppendLine("纸将尽!");
if ((res & 0x80) == 0x80)
sb.AppendLine("缺纸!");
if (sb.Length > 0)
MessageBox.Show(sb.ToString());
}
#endregion
#region 简单打印1
private void btn_Print1_Click(object sender, EventArgs e)
{
//LoadPOSDll PosPrint = new LoadPOSDll();
//POS_COM_DTR_DSR 0x00 流控制为DTR/DST
//POS_COM_RTS_CTS 0x01 流控制为RTS/CTS
//POS_COM_XON_XOFF 0x02 流控制为XON/OFF
//POS_COM_NO_HANDSHAKE 0x03 无握手
//POS_OPEN_PARALLEL_PORT 0x12 打开并口通讯端口
//POS_OPEN_BYUSB_PORT 0x13 打开USB通讯端口
//POS_OPEN_PRINTNAME 0X14 打开打印机驱动程序
//POS_OPEN_NETPORT 0x15 打开网络接口
// if (LoadPOSDll.POS_StartDoc())
// {
// byte[] by_SendData = System.Text.Encoding.Default.GetBytes("今天到此一游~~\r\n");
// LoadPOSDll.POS_WriteFile(PosPrint.POS_IntPtr, by_SendData, (uint)by_SendData.Length);
// LoadPOSDll.POS_WriteFile(PosPrint.POS_IntPtr, new byte[] { 0x0a }, 1);
// LoadPOSDll.POS_EndDoc();
// }
print();
}
#endregion
#region 先预览后打印
private void button1_Click(object sender, EventArgs e)
{
//打印预览
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
//设置边距
Margins margin = new Margins(15, 15, 15, 15);
pd.DefaultPageSettings.Margins = margin;
////纸张设置默认
PaperSize pageSize = new PaperSize("First custom size", getYc(70), 420);
pd.DefaultPageSettings.PaperSize = pageSize;
//打印事件设置
pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
ppd.Document = pd;
ppd.ShowDialog();
try
{
//print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
pd.PrintController.OnEndPrint(pd, new PrintEventArgs());
}
}
#endregion
private int getYc(double cm)
{
return (int)(cm / 25.4) * 100;
}
#region 打印的内容
#region 标题
public string GetPrintHead()
{
StringBuilder sb = new StringBuilder();
string tou = "重庆洞子老火锅";
sb.Append(" " + tou + " \n");
return sb.ToString();
}
#endregion
#region 内容
public string GetPrintStr()
{
StringBuilder sb = new StringBuilder();
string address = "重庆涪陵XXX号";
string saleID = "2010930233330";
string item = "商品";
decimal price = 25.00M;
int count = 1;
decimal total = 0.00M;
decimal fukuan = 500.00M;
//sb.Append(" " + tou + " \n");
sb.Append("--------------------------------------------------\n");
sb.Append("日期:" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss") + "\n" + "单号:" + saleID + "\n");
sb.Append("--------------------------------------------------\n");
sb.Append("项目" + "\t" + "数量" + "\t" + "单价" + "\t" + "小计" + "\n");
for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;
sb.Append(item + (i + 1) + "\t" + (i + 1) + "\t" + price + "\t" + xiaoji);
total += xiaoji;
if (i != (count))
sb.Append("\n");
}
sb.Append("--------------------------------------------------\n");
sb.Append("数量: " + count + " 合计: " + total + "\n");
sb.Append("付款: " + fukuan);
sb.Append(" 现金找零: " + (fukuan - total) + "\n");
sb.Append("--------------------------------------------------\n");
sb.Append("地址:" + address + "\n");
sb.Append("电话:123456789 123456789\n");
sb.Append(" 谢谢惠顾欢迎下次光临 ");
return sb.ToString();
}
#endregion
#endregion
/// POS打印
/// </summary>
public void print()
{
this.printv_pos = new System.Windows.Forms.PrintPreviewDialog();
this.printd_pos = new System.Drawing.Printing.PrintDocument();
this.printv_pos.AutoScrollMargin = new System.Drawing.Size(0, 0); //获取或设置自动滚动边距的大小
this.printv_pos.AutoScrollMinSize = new System.Drawing.Size(0, 0); //获取或设置自动滚动条的最小大小
this.printv_pos.ClientSize = new System.Drawing.Size(400, 300); //获取或设置窗体工作区的大小
this.printv_pos.Document = this.printd_pos; //获取或设置要预览的文档
this.printv_pos.Enabled = true;
this.printv_pos.Name = "printPreviewDialog1";
this.printv_pos.Visible = false;
this.printd_pos.PrintController = new System.Drawing.Printing.StandardPrintController();
this.printd_pos.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
//设置边距
System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(5, 5, 5, 5);
this.printd_pos.DefaultPageSettings.Margins = margins;
this.printd_pos.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("First custom size", getYc(70), 420);
//this.printDocument1.PrinterSettings.PrinterName = "";
//Margins margins = new Margins(
//this.printv_pos.Document = this.printd_pos;
printv_pos.PrintPreviewControl.AutoZoom = false;
printv_pos.PrintPreviewControl.Zoom = 1;
// this.printv_pos.ShowDialog(win);
try
{
printd_pos.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
printd_pos.PrintController.OnEndPrint(printd_pos, new PrintEventArgs());
}
}
#region (页面设置事件)字体设置
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//头部
string strHeader = GetPrintHead();
Font ft = new Font("微软雅黑", 12.5F, FontStyle.Bold); //字体
Point pt = new Point(0, 0); //顶部
g.DrawString(strHeader, ft, new SolidBrush(Color.Black), pt);
//Graphics.MeasureString 方法 (String, Font)
//MeasureString测量用指定的 System.Drawing.Font 绘制的指定字符串
Size headSize = Size.Round(g.MeasureString(strHeader, ft)); //头部的Size(宽和高)
headSize.Width = 0; //设置宽为0,是因为只需要向y移动,不需要x移动
//内容
string strFile = GetPrintStr();
ft = new Font("宋体", 8.5F, FontStyle.Regular);
pt = new Point(headSize); //Point(Size size)
g.DrawString(strFile, ft, new SolidBrush(Color.Black), pt);
SizeF strSize = g.MeasureString(strHeader, ft);
}
#endregion
}
}
C#小票打印机的更多相关文章
- 小票打印机指令集封装(支持EPSON指令)
最近写了一些关于小票打印机的程序,不难,但是记录下来,作为足迹吧. 现在市场上的小票机基本都支持EPSON指令.指令集文档 对指令集进行了自己的封装,方便以后调用: package aheiziUti ...
- 让 Odoo POS 支持廉价小票打印机
为了测试 Odoo 在实际业务中的实施,我们开了一家(马上要开第二家分店)猪肉店.由于预算有限,在实施 Odoo PoS 的时候采购了一台价格为 85 元的爱宝热敏打印机,结果连上 Odoo Posb ...
- C#并口热敏小票打印机打印位图包括芯片的写入
下面是打印所需要调用的代码: class LptControl { private string LptStr = "lpt1"; public LptControl(string ...
- C# Lpt 并口热敏小票打印机打印位图
class LptControl { private string LptStr = "lpt1"; public LptControl(string l_LPT_Str) { L ...
- 转:C#并口热敏小票打印机打印位图
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class LptControl { private s ...
- C# 热敏打印机 小票打印机 打印图片
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: 因为我们这里主要是打印条形码和二维码,所以以条形码和二维 ...
- C#并口热敏小票打印机打印位图
原文:C#并口热敏小票打印机打印位图 最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class ...
- c#调用刀片小票打印机
public static bool Print(int orderId, string orderTime) { bool b = true; string cut = ((char)29).ToS ...
- C#调用小票打印机
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
随机推荐
- PowerExchange实时抽取架构介绍
工作原理 准实时抽取架构图: 以上共有核心业务系统数据库服务器.ETL服务器.BI数据库服务器[目标数据库服务器],三台服务器和ETL客户端(PowerCenter客户端).其中核心业务系统上有核心系 ...
- 【设计模式】—— 策略模式Strategy
前言:[模式总览]——————————by xingoo 模式意图 定义一系列的算法,把他们封装起来,使得算法独立于适用对象. 比如,一个系统有很多的排序算法,但是使用哪个排序算法是客户对象的自有.因 ...
- 洛谷P3380 【模板】二逼平衡树(树套树,树状数组,线段树)
洛谷题目传送门 emm...题目名写了个平衡树,但是这道题的理论复杂度最优解应该还是树状数组套值域线段树吧. 就像dynamic ranking那样(蒟蒻的Sol,放一个link骗访问量233) 所有 ...
- 【拓扑 字符串还原 + 线段树维护】奇洛金卡达(father)
奇洛金卡达(father) Description 阿良良木历将要迎来人生(不,是吸血鬼生涯)的第三次战斗——与身为人类的奇洛金卡达在直江津高中的操场solo,以取回Heartunderblade 的 ...
- C++并发编程实战---阅读笔记
1. 当把函数对象传入到线程构造函数中时,需要避免“最令人头痛的语法解析”.如果传递了一个临时变量,而不是一个命名的变量:C++编译器会将其解析为函数声明,而不是类型对象的定义. 例如: class ...
- JS控制form表单action去向
http://blog.csdn.net/w709854369/article/details/6261624 不知道大家遇没遇到这种情况,当我们提交一个表单的时候,可能因为相关的参数不同而需提交给不 ...
- Mask-RCNN数据集制作
转自https://blog.csdn.net/pingushen2100/article/details/80513043 一.Mask-RCNN数据集 1.1 训练Mask-RCNN用的到的文件有 ...
- sql查询结果存入DataTable,然后从DataTable取数据
public static DataTable SqlConnectionInformation() { string connstr = ConfigurationManager.Connectio ...
- Linux makefile讲解
博客不会讲解的太细致深入,推荐先看视频再看博客 视频链接,推荐去B站观看,B站比较清晰... 土豆网:http://www.tudou.com/programs/view/19VZ0f3b_I0 B站 ...
- 给radio加自己的样式(图片)
$('.choice').click(function() { var display1 = $("#check").prop("checked"); cons ...