C# 打印小票 POS
最近在写一个餐饮的收银系统,以前从来没有碰过打印机这玩意。感觉有些无从下手,在前面做报表时,总想找第三方的控件来用用,结果始终不行没搞定、没研究透,催得急没办法还是的动手自己写个,哈...查了些相关的资料,感觉也不难,没多久就搞定了。主要用到PrintDocument和PrintPreviewDialog两个对象,一个用来打印,一个用来阅览,如果在设置模板OK后,若果人流量比较多的话就直接打印不必在阅览,这样会快好多。
private void button2_Click(object sender, EventArgs e) { //打印预览 PrintPreviewDialog ppd = new PrintPreviewDialog(); PrintDocument pd = new PrintDocument(); //设置边距 Margins margin = new Margins(20, 20, 20, 20); pd.DefaultPageSettings.Margins = margin; ////纸张设置默认 PaperSize pageSize = new PaperSize("First custom size", getYc(58), 600); pd.DefaultPageSettings.PaperSize = pageSize; //打印事件设置 pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); ppd.Document = pd; ppd.ShowDialog(); try { pd.Print(); } catch (Exception ex) { MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error); pd.PrintController.OnEndPrint(pd, new PrintEventArgs()); } }
这里的代码主要是获取需要打印的内容,在实际运用中这里要改成动态的
private int getYc(double cm) { return (int)(cm / 25.4) * 100; } public string GetPrintStr() { StringBuilder sb = new StringBuilder(); string tou = "伊尹餐饮公司"; string address = "深圳市罗湖区东门老街29号"; string saleID = "2010930233330"; string item = "项目"; decimal price = 25.00M; int count = 5; decimal total = 0.00M; decimal fukuan = 500.00M; sb.Append(" " + tou + " /n"); sb.Append("-----------------------------------------------------------------/n"); sb.Append("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID + "/n"); sb.Append("-----------------------------------------------------------------/n"); sb.Append("项目" + "/t/t" + "数量" + "/t" + "单价" + "/t" + "小计" + "/n"); for (int i = 0; i < count; i++) { decimal xiaoji = (i+1) * price; sb.Append(item + (i+1) + "/t/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(); }
打印效果如下: