使用的佳博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#小票打印机的更多相关文章

  1. 小票打印机指令集封装(支持EPSON指令)

    最近写了一些关于小票打印机的程序,不难,但是记录下来,作为足迹吧. 现在市场上的小票机基本都支持EPSON指令.指令集文档 对指令集进行了自己的封装,方便以后调用: package aheiziUti ...

  2. 让 Odoo POS 支持廉价小票打印机

    为了测试 Odoo 在实际业务中的实施,我们开了一家(马上要开第二家分店)猪肉店.由于预算有限,在实施 Odoo PoS 的时候采购了一台价格为 85 元的爱宝热敏打印机,结果连上 Odoo Posb ...

  3. C#并口热敏小票打印机打印位图包括芯片的写入

    下面是打印所需要调用的代码: class LptControl { private string LptStr = "lpt1"; public LptControl(string ...

  4. C# Lpt 并口热敏小票打印机打印位图

    class LptControl { private string LptStr = "lpt1"; public LptControl(string l_LPT_Str) { L ...

  5. 转:C#并口热敏小票打印机打印位图

    最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class LptControl { private s ...

  6. C# 热敏打印机 小票打印机 打印图片

    最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: 因为我们这里主要是打印条形码和二维码,所以以条形码和二维 ...

  7. C#并口热敏小票打印机打印位图

    原文:C#并口热敏小票打印机打印位图 最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了.   这是DEMO的窗体:   下面是打印所需要调用的代码: class ...

  8. c#调用刀片小票打印机

    public static bool Print(int orderId, string orderTime) { bool b = true; string cut = ((char)29).ToS ...

  9. C#调用小票打印机

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

随机推荐

  1. 安装及调试 Mavem Web

    一  使用Mavem eclipse菜单栏,找到file-->new -->other 然后找到Maven Project 然后next. 接着,选择maven-archetype-web ...

  2. Codeforces Round #374 (Div. 2) C(DAG上的DP)

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  3. 洛谷P3960 列队(NOIP2017)(Splay)

    洛谷题目传送门 最弱的Splay...... 暴力模拟30分(NOIP2017实际得分,因为那时连Splay都不会)...... 发现只是一个点从序列里搬到了另一个位置,其它点的相对位置都没变,可以想 ...

  4. 【BZOJ1293】[SCOI2009]生日礼物(单调队列)

    [BZOJ1293][SCOI2009]生日礼物(单调队列) 题面 BZOJ 洛谷 题解 离散之后随便拿单调队列维护一下就好了. #include<iostream> #include&l ...

  5. 【转】Example of using the --info linker option

    5.3 Example of using the --info linker option This is an example of the output generated by the --in ...

  6. 【uoj228】 基础数据结构练习题

    http://uoj.ac/problem/228 (题目链接) 题意 给出一个序列,维护区间加法,区间开根,区间求和 Solution 线段树.考虑区间开根怎么做.当区间的最大值与最小值相等时,我们 ...

  7. Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流)

    Luogu 2469 [SDOI2010]星际竞速 / HYSBZ 1927 [Sdoi2010]星际竞速 (网络流,最小费用流) Description 10年一度的银河系赛车大赛又要开始了.作为全 ...

  8. redis 中用正则找key

    获取 redis 中所有的 key 可用使用 *. redis 127.0.0.1:6379> KEYS * 1) "w3c3" 2) "w3c1" 3) ...

  9. python爬虫 抓取一个网站的所有网址链接

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  10. Hadoop生态圈-Kafka的旧API实现生产者-消费者

    Hadoop生态圈-Kafka的旧API实现生产者-消费者 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.旧API实现生产者-消费者 1>.开启kafka集群 [yinz ...