using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace WindowsFormsApplication25
{
class ClsPrintBill
{
struct PrintDataSturct
{
public string Name;
public Font Font;
public int X;
public int Y;
public PrintDataSturct(int x, int y, string name, Font font)
{
this.X = x;
this.Y = y;
this.Name = name;
this.Font = font;
}
}
PrintDocument printDocument;
List<PrintDataSturct> printData;
public static ClsPrintBill clsPb;
/// <summary>
/// 得到打印实例
/// </summary>
/// <returns></returns>
public static ClsPrintBill GetInstance()
{
if (clsPb == null)
{
clsPb = new ClsPrintBill();
}
return clsPb;
}
/// <summary>
/// 初始化
/// </summary>
public ClsPrintBill()
{
//IniPrint();
}
/// <summary>
/// 初始化打印数据
/// </summary>
public void IniPrint(string flno,string regn,string psn,string flop,string jflno,string stx,string etx,string stot,string etot,string enm,string tknu)
{
printDocument = new PrintDocument();
printDocument.DefaultPageSettings.Margins = new Margins(44, 44, 52, 27);
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
printData = new List<PrintDataSturct>();
Font font = new Font("宋体", 14.25f);
PrintDataSturct data = new PrintDataSturct(75, 70, flno, font);
printData.Add(data);
data = new PrintDataSturct(255, 70, regn, font);
printData.Add(data);
data = new PrintDataSturct(415, 70, psn, font);
printData.Add(data);
/
string y = "";
string m = "";
string d = "";
if (flop != null)
{
y = flop.Substring(0, 4);
m = flop.Substring(4, 2);
d = flop.Substring(6, 2);
}
data = new PrintDataSturct(550, 70, y, font);
printData.Add(data);
data = new PrintDataSturct(616, 70, m, font);
printData.Add(data);
data = new PrintDataSturct(660, 70, d, font);
printData.Add(data);
data = new PrintDataSturct(430, 100, "操作员:", font);
printData.Add(data);
data = new PrintDataSturct(510, 100, enm, font);
printData.Add(data);
data = new PrintDataSturct(610, 100, "序号:", font);
printData.Add(data);
data = new PrintDataSturct(670, 100, tknu, font);
printData.Add(data);
Font font2 = new Font("宋体", 10);
data = new PrintDataSturct(510, 114, "──────", font2);
printData.Add(data);
data = new PrintDataSturct(668, 114, "──", font2);
printData.Add(data);
data = new PrintDataSturct(40, 185, jflno, font);
printData.Add(data);
data = new PrintDataSturct(190, 185, stx, font);
printData.Add(data);
data = new PrintDataSturct(400, 185, etx, font);
printData.Add(data);
data = new PrintDataSturct(40, 230, flno, font);
printData.Add(data);
data = new PrintDataSturct(190, 230, stot, font);
printData.Add(data);
data = new PrintDataSturct(400, 230, etot, font);
printData.Add(data);
}
/// <summary>
/// 预览
/// </summary>
public void Preview()
{
PrintPreviewDialog previewDialog = new PrintPreviewDialog();
previewDialog.Document = printDocument;
previewDialog.ShowDialog();
}
/// <summary>
/// 打印
/// </summary>
public void Print()
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
try
{
printDocument.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
}
}
}
public void Setting()
{
PageSetupDialog setupDialog = new PageSetupDialog();
setupDialog.AllowPaper = false;
setupDialog.EnableMetric = true;
setupDialog.AllowOrientation = false;
setupDialog.Document = printDocument;
if (setupDialog.ShowDialog() == DialogResult.OK)
{
printDocument.DefaultPageSettings.Margins = setupDialog.PageSettings.Margins;
}
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics; // 获得绘图对象
float leftMargin = e.MarginBounds.Left; // 左边距
float topMargin = e.MarginBounds.Top; // 上边距
SolidBrush myBrush = new SolidBrush(Color.Black); // 刷子
foreach (PrintDataSturct data in printData)
{
g.DrawString(data.Name, data.Font, myBrush, leftMargin + data.X, topMargin + data.Y);
}
}
}
}
打印小票类ClsPrintBill
最新推荐文章于 2023-05-04 13:50:02 发布