C# winform小票打印
(1)自定义纸张设置
控制面板->打印机和传真->右键->服务器属性->创建新的格式
(2)自定义纸张使用
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight);
NewPrint:制定一纸张名称。 iWidth:纸张使用宽度。 iHeight:纸张使用高度。
iWidth,iHeight 可以在使用过程中调整。
例如:iWidth=923,iHeight=480
(3)ESC/P指令使用
using System;
using System.Runtime.InteropServices;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace PrintDome
{
class ClsPrintLPT
{
private IntPtr iHandle;
private FileStream fs;
private StreamWriter sw;
private string prnPort = "LPT1"; //打印机端口
public ClsPrintLPT()
{
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
/// <summary>
/// 打开一个vxd(设备)
/// </summary>
[DllImport("kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
/// <summary>
/// 开始连接打印机
/// </summary>
private bool PrintOpen()
{
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
MessageBox.Show("没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
sw = new StreamWriter(fs, System.Text.Encoding.Default); //写数据
return true;
}
}
/// <summary>
/// 打印字符串
/// </summary>
/// <param name="str">要打印的字符串</param>
private void PrintLine(string str)
{
sw.WriteLine(str); ;
}
/// <summary>
/// 关闭打印连接
/// </summary>
private void PrintEnd()
{
sw.Close();
fs.Close();
}
/// <summary>
/// 打印票据
/// </summary>
/// <param name="ds">tb_Temp 全部字段数据集合</param>
/// <returns>true:打印成功 false:打印失败</returns>
public bool PrintDataSet(DataSet dsPrint)
{
try
{
if (PrintOpen())
{
PrintLine(" ");
PrintLine("[XXXXXXXXXXXXXXXXXX超市]");
PrintLine("NO : " + dsPrint.Tables[0].Rows[0][1].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][2].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][3].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][4].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][5].ToString());
PrintLine("操 作 员: " + dsPrint.Tables[0].Rows[0][6].ToString() + " " + dsPrint.Tables[0].Rows[0][7].ToString());
PrintLine("-------------------------------------------");
}
PrintEnd();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// ESC/P 指令
/// </summary>
/// <param name="iSelect">0:退纸命令 1:进纸命令 2:换行命令</param>
public void PrintESC(int iSelect)
{
string send;
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
MessageBox.Show("没有连接打印机或者打印机端口不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
}
byte[] buf = new byte[80];
switch (iSelect)
{
case 0:
send = "" + (char)(27) + (char)(64) + (char)(27) + 'j' + (char)(255); //退纸1 255 为半张纸长
send = send + (char)(27) + 'j' + (char)(125); //退纸2
break;
case 1:
send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(255); //进纸
break;
case 2:
send = "" + (char)(27) + (char)(64) + (char)(12); //换行
break;
default:
send = "" + (char)(27) + (char)(64) + (char)(12); //换行
break;
}
for (int i = 0; i < send.Length; i++)
{
buf[i] = (byte)send[i];
}
fs.Write(buf, 0, buf.Length);
fs.Close();
}
}
}
---------------------------------------------------------------------------------------------------------------
使用例1(LPT打印):
printLPT.PrintESC(0); //打印前退纸
printLPT.PrintDataSet(dsPrint);
printLPT.PrintESC(1); //打印后进纸
使用例2(水晶报表打印):
this.reportDocument1.Load(Application.StartupPath + "\\Temp.rpt");
PageMargins pMaargins;
pMaargins = reportDocument1.PrintOptions.PageMargins;
pMaargins.topMargin = 5;
pMaargins.bottomMargin = 0;
pMaargins.leftMargin = 5;
pMaargins.rightMargin = 0;
reportDocument1.PrintOptions.ApplyPageMargins(pMaargins);
reportDocument1.Refresh();
reportDocument1.SetDataSource(dsPrint);
//reportDocument1.PrintOptions.PrinterName = "Microsoft Office Document Image Writer";
printLPT.PrintESC(0); //打印前退纸
reportDocument1.PrintToPrinter(1, false, 0, 0);
timer1.Enabled = true;
使用例3(printDocument 打印):
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight);
printLPT.PrintESC(0); //打印前退纸
this.printDocument1.Print();
-----------------------------------PrintPage()----------------------------------------------------------
int iX;
int iY;
int iTopMargin = 35; //顶边距
int iLeftMargin = 70;//左边距
int iButtomMargin = 40;//底边距
int iMarginX = 25; //文字间距
int iMarginY = 25; //文字行距
int icellTopMargin = 12; //单元格顶边距
int icellLeftMargin = 12; //单元格左边距
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font titleFont = new Font("宋体", 16, FontStyle.Bold);//标题字体
Font fntTxt = new Font("宋体", 11, FontStyle.Regular); //正文文字
Brush brush = new SolidBrush(Color.Black);//画刷
Pen pen = new Pen(Color.Black); //线条颜色
try
{
string sTitle = "<<XXXXXXXXXXXXXXXXXXXXXXXXX>>";
//string sDataTitle = "No: " + dsPrint.Tables[0].Rows[0][1].ToString() + " " +
// "XXXXX: " + dsPrint.Tables[0].Rows[0][17].ToString() + " " +
// "XXXXX: " + dsPrint.Tables[0].Rows[0][19].ToString() + " " + dsPrint.Tables[0].Rows[0][18].ToString();
string sDataTitle = "No: " + dsPrint.Tables[0].Rows[0][1].ToString() + " " +
"XXXXX: " + dsPrint.Tables[0].Rows[0][17].ToString() + " " +
"XXXXX: " + dsPrint.Tables[0].Rows[0][19].ToString();
int width = e.PageBounds.Width;
int height = e.PageBounds.Height;
//int xoffset = (int)((width - e.Graphics.MeasureString(sTitle, titleFont).Width) / 2);
//int xoffset2 = (int)((width - e.Graphics.MeasureString(sDataTitle, fntTxt).Width) / 2);
//e.Graphics.DrawString(sTitle, titleFont, brush, xoffset, iTopMargin); //标题
//e.Graphics.DrawString(sDataTitle, fntTxt, brush, xoffset2, iTopMargin += iTopMargin + 5); //副标题数据
e.Graphics.DrawString(sTitle, titleFont, brush, iLeftMargin + 140, iTopMargin); //标题
e.Graphics.DrawString(sDataTitle, fntTxt, brush, iLeftMargin + 60, iTopMargin += 35); //副标题数据
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 25 + icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 25;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX, iY + 110)); //最左边的竖线
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最上边的竖线
string sCell = "XXXXX: ";
int iCellWidth = (int)((e.Graphics.MeasureString(sCell, fntTxt).Width));
int iCellHeight = (int)((e.Graphics.MeasureString(sCell, fntTxt).Height));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //1
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][2].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //2
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //3
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][3].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //4
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //5
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][4].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //6
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 20 + iCellHeight + 3 * icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 2 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下边的竖线
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][13].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][14].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][15].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 20 + iCellHeight + 6 * icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 5 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下边的竖线
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][8].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][10].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][12].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 8 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下边的竖线
e.Graphics.DrawLine(pen, new Point(0, iY += iButtomMargin), new Point(e.PageBounds.Width, iY)); //最下边的竖线
}
catch
{
MessageBox.Show(this, "数据库连接错误,打印失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

C# winform小票打印的更多相关文章
- 更好的小票打印体验,huanent.printer2.0发布
huanent.printer2.0是一个专注消费小票打印的类库.拥有许多先进的特性例如居中打印.自动换行等特性,可以通过简洁的代码来打印出复杂的消费小票.huanent.printer通过MIT方式 ...
- Android 小票打印USB
第一步USB通信: Usb设备有两种,Host与Accessory 简单来说是主模式与从模式,主模式则android设备给外设供电,反之,外设给android设备充电,对于小票打印,使用的是Host模 ...
- 关于一体机打印新加菜按钮更改为下单小票打印设置FAQ(适用正餐6.0.1.0+,轻餐4.0.6.2+)
适用版本:正餐6.0.1.0+,轻餐4.0.6.2+ 实际场景:更新后小票设置中的打印新加菜按钮更换为下单小票打印设置,更换后,设置中,有3个选项: 1.仅打印新加菜 (选中后,订单加菜后前台小 ...
- linux下使用小票打印
linux下使用小票打印 打印机: Xprinter XP-58IIH指令支持: ESC/POS接口: USB, 蓝牙 Linux系统: Centos7 蓝牙配对很快, 配对好后就是连接状态. 但很快 ...
- Delphi 10 Seattle 小票打印控件TQ_Printer
TQ_Printrer控件,是一个为方便需要控制打印命令而设计的跨平台专用控件,已包含标准ESC/POS打印控制的基本指令在内(这些基本指令已能很好的满足多数项目使用). TQ_Printrer控件让 ...
- c# 小票打印
c# 在进行小票打印时大致有三种方法. 1. 使用水晶报表进行打印.可以参考:https://www.cnblogs.com/aitong/p/10717786.html 2. 在 PrintDocu ...
- Android打印机--小票打印格式及模板设置
小票打印就是向打印设备发送控制打印格式的指令集,而这些打印格式须要去查询相应打印机的API文档,这里我把经常使用的api给封装了一下 文字对齐方式 打印字体大小 字体是否加粗 打印二维码 打印条形码 ...
- Atitit.收银机小票打印功能的设计 java php c#.net版本
Atitit.收银机小票打印功能的设计 java php c#.net版本 1. 1. 打印方式有4种:1 1.1. 1.1. 一是不经过任何修改,直接调用javascript中的window.pr ...
- 重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印
重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印 一.引言 桌面端系统经常需要对接各种硬件设备,比如扫描器.读卡器.打印机等. 这里介绍下桌面端 ...
随机推荐
- C#后台调用浏览器打开下载连接地址的三种方法
一.从注册表中读取到本地计算机默认浏览器,然后调用下载. private void button1_Click(object sender, EventArgs e) { //从注册表 ...
- mysql添加外键约束变为索引
今天有位自己填上一坑:mysql储存引擎 原因就是数据库表引擎为:MyISAM,建立主外键关系需要是InnoDB: 解决方案:alter table table_name1 engine=inno ...
- 201521123090 《Java程序设计》第4周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 继承与多态的概念与实现 父类与之类的关系 解决代码复用的办法 2. 书面作业 注释的应用 使 ...
- java web SSO单点登录
第一篇: Web应用系统的演化总是从简单到复杂,从单功能到多功能模块再到多子系统方向发展. .当前的大中型Web互联网应用基本都是多系统组成的应用群,由多个web系统协同为用户提供服务. 多系统应用群 ...
- Java SVN管理工具的使用
1.svn环境搭建 在应用myEclips 8.5做项目时,svn会成为团队项目的一个非常好的工具,苦苦在网上寻求了一下午,终于整合好了这个环境,在这里简单介绍下,希望能为刚开始用svn的朋友一点点帮 ...
- Ningx集群环境搭建
Ningx集群环境搭建 Nginx是什么? Nginx ("engine x") 是⼀个⾼性能的 HTTP 和 反向代理 服务器,也是⼀个 IMAP/ POP3/SMTP 代理服务 ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- JVM菜鸟进阶高手之路四
转载请注明原创出处,谢谢! 由于很多的jvm分析最好是基于gc日志的,所以添加参数如下即可: -verbose:gc -XX:+HeapDumpOnOutOfMemoryError -XX:+Prin ...
- [转载]iOS开发之手势识别
感觉有必要把iOS开发中的手势识别做一个小小的总结.在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi ...
- AngularJS的$rootScope和$scope联系和区别
scope是html和单个controller之间的桥梁,数据绑定就靠他了. rootscope是各个controller中scope的桥梁.用rootscope定义的值,可以在各个controlle ...