摘要:本文主要向大家介绍了C#编程 winform小票打印(代码实例),通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程 winform小票打印(代码实例),通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
(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;
///
[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);
///
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;
}
}
///
///
要打印的字符串
private void PrintLine(string str)
{
sw.WriteLine(str); ;
}
///
private void PrintEnd()
{
sw.Close();
fs.Close();
}
///
///
tb_Temp 全部字段数据集合
///
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;
}
}
///
///
0:退纸命令 1:进纸命令 2:换行命令
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 = "<
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号