先新建一个 CustomPrint 类库里面放打印机的一些操作 :
在类库下面新建一个PrintRow类 用来设置打印样式
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
namespace CustomPrint
{
/// <summary>
/// 全部采用居中绘制
/// </summary>
public class PrintRow
{
private string context;
/// <summary>
/// 内容
/// </summary>
public string Context
{
get { return context; }
set { context = value; }
}
private Font drawFont;
/// <summary>
/// 字体
/// </summary>
public Font DrawFont
{
get { return drawFont; }
set { drawFont = value; }
}
private int printIndex;
/// <summary>
/// 打印顺序
/// </summary>
public int PrintIndex
{
get { return printIndex; }
set { printIndex = value; }
}
private int drawHeight;
/// <summary>
/// 绘制位置
/// </summary>
public int DrawHeight
{
get { return drawHeight; }
set { drawHeight = value; }
}
private Brush drawBrush;
public Brush DrawBrush
{
get { return drawBrush; }
set { drawBrush = value; }
}
public PrintRow(int index, string context, Font penFont, Brush drawBrush, int drawHeight)
{
this.printIndex = index;
this.context = context;
this.drawFont = penFont;
this.drawBrush = drawBrush;
this.drawHeight = drawHeight;
}
}
}
然后新建一个 PrintOrder 类用来设置打印机的打印方式
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
namespace CustomPrint
{
public static class PrintOrder
{
private static PrintDocument printDocument;
private static Order printOrder;
private static object lockObj;
/// <summary>
/// 设置打印纸高
/// </summary>
public static int pageHeight = 350;
/// <summary>
/// 设置打印纸宽
/// </summary>
public static int pageWidth = 320;
public static StringFormat sf;
static PrintOrder()
{
sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
lockObj = new object();
printDocument = new System.Drawing.Printing.PrintDocument();
printDocument.BeginPrint += PrintDocument_BeginPrint;
printDocument.EndPrint += PrintDocument_EndPrint;
printDocument.PrintPage += PrintDocument_PrintPage;
}
private static void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
Draw(e.Graphics, printOrder);
}
catch (Exception ex)
{
}
}
private static void PrintDocument_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
}
private static void PrintDocument_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
}
/// <summary>
/// 绘制打印的过程
/// </summary>
/// <param name="g">打印机g</param>
/// <param name="order">订单</param>
private static void Draw(Graphics g, Order order)
{
List<PrintRow> tempList = order.PrintRows.OrderBy(p => p.PrintIndex).ToList();
for (int i = 0; i < tempList.Count; i++)
{
Rectangle drawRect = new Rectangle(0, tempList[i].DrawHeight, pageWidth, tempList[i].DrawFont.Height);
g.DrawString(tempList[i].Context, tempList[i].DrawFont, tempList[i].DrawBrush, drawRect, sf);
}
}
public static Bitmap GetBmp(Order order)
{
Bitmap bmp = new Bitmap(pageWidth, pageHeight);
Graphics g = Graphics.FromImage(bmp);
Draw(g, order);
g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
return bmp;
}
public static void Print(string printName, Order order)
{
lock (lockObj)
{
try
{
printOrder = order;
printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", pageWidth, pageHeight);
printDocument.PrinterSettings.PrinterName = printName;
printDocument.Print();
}
catch (Exception ex)
{
}
}
}
public static void Print(Order order)
{
lock (lockObj)
{
try
{
printOrder = order;
//printDocument.DocumentName = order.HeardInfo.OrderNum;
printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", pageWidth, pageHeight);
printDocument.Print();
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
}
}
/// <summary>
/// 将CM转换成打印像素
/// </summary>
/// <param name="cmValue"></param>
/// <returns></returns>
public static int ConvertCmToPrintPixel(float cmValue)
{
return PrinterUnitConvert.Convert((int)(cmValue * 100), PrinterUnit.TenthsOfAMillimeter, PrinterUnit.Display);
}
}
}
在建一个Order类 用来调用前面二个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomPrint
{
public class Order
{
private List<PrintRow> printRows;
public List<PrintRow> PrintRows
{
get { return printRows; }
set { printRows = value; }
}
public Order(List<PrintRow> rows)
{
this.printRows = rows;
}
}
}
好了到这里打印机程序差不多就完成了 剩下的就是调用和设置样式了
PrintRow row0 = new PrintRow(0, "XXX取号机", new Font("宋体", 25), Brushes.Black, 80);
PrintRow row1 = new PrintRow(1, “建设银行”, new Font("宋体", 18), Brushes.Black, 130);
PrintRow row2 = new PrintRow(2, "排队号:"+“J001”, new Font("宋体", 15), Brushes.Black, 160);
PrintRow row3 = new PrintRow(3, " 姓名:" + “张三”, new Font("宋体", 15), Brushes.Black, 190);
PrintRow row4 = new PrintRow(4, " 取号时间:" + “2017/7/08”, new Font("宋体", 15), Brushes.Black, 220);
PrintRow row5 = new PrintRow(5, "-------------------------------------", new Font("宋体", 10), Brushes.Black, 250);
PrintRow row6 = new PrintRow(6, "请您到等候休息区等待", new Font("宋体", 18), Brushes.Green, 260);
PrintRow row7 = new PrintRow(7, "注意屏幕提示(过号作废)", new Font("宋体", 15), Brushes.Black, 290);
PrintRow row8 = new PrintRow(8, DateTime.Now.ToString(), new Font("宋体", 18), Brushes.Black, 320);
Order tempOrder = new Order(new List<PrintRow>() { row0, row1, row2, row3, row4, row5, row6, row7, row8 });
PrintOrder.Print(tempOrder);
好了这就是调用上面我们写的三个方法来调用 打印机 打印票据,这套打印机代码简单 方便操作,一看就会,这代码都可以直接copy来用的