using Bro.Common.Helper; using Bro.Common.Interface; using Bro.UI.Model.Winform; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; using ZXing.Common; namespace Bro.M141.Process { [MenuNode("PrinterStatus", "打印机显示界面", 1, EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)] public partial class UIPrinter : MenuFormBase //public partial class UIPrinter : UserControl { M141Config Config141 => Process.IConfig as M141Config; M141Process Process141 => Process as M141Process; public UIPrinter() { InitializeComponent(); } public override void OnProcessUpdated() { base.OnProcessUpdated(); printDocument1.PrintPage += new PrintPageEventHandler(printDocument_Print); } PrintDocument printDocument1 = new PrintDocument(); int ttwith = (int)(80 * 4); int ttheigh = (int)(40 * 4); string message = ""; public void StartPrint(string str) { ttwith = 320; ttheigh = 160; message = str; this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240 (300 dpi)"; this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh); this.printDocument1.PrintController = new System.Drawing.Printing.StandardPrintController(); this.printDocument1.Print(); } private void printDocument_Print(object sender, PrintPageEventArgs e) { Font fntTxt = new Font("黑体", 15, System.Drawing.FontStyle.Bold);//正文文字 System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷 try { string numvalue = message; Bitmap bitmap = CreateCode(numvalue); int with = (ttwith - bitmap.Width) / 2 - 20; int heih = (ttheigh - bitmap.Height) / 2; //條碼的位置 e.Graphics.DrawImage(bitmap, new System.Drawing.Point(with, heih)); //條碼信息數字的位置 e.Graphics.DrawString(numvalue, fntTxt, brush, new System.Drawing.Point(bitmap.Width / 4 + with - 10, heih + bitmap.Height + 5)); } catch (Exception ee) { MessageBox.Show(ee.Message); } } public static Bitmap CreateCode(string asset) { // 1.设置条形码规格 EncodingOptions options = new EncodingOptions(); options.Height = 100; // 必须制定高度、宽度 options.Width = 250; // 2.生成条形码图片并保存 var writer = new BarcodeWriter(); writer.Options = options; writer.Format = BarcodeFormat.CODE_128; // 这里可以设定条码的标准 //為true不自帶條碼信息 options.PureBarcode = true; Bitmap bp = writer.Write(asset); return bp; // 生成图片 } //打印 private void button1_Click(object sender, EventArgs e) { message = textBox1.Text; if (string.IsNullOrEmpty(message)) { MessageBox.Show("打印内容不可为空"); return; } StartPrint(message); } //预览 private void button2_Click(object sender, EventArgs e) { message = textBox1.Text; if (string.IsNullOrEmpty(message)) { MessageBox.Show("打印内容不可为空"); return; } PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = "Honeywell PX240 (300 dpi)"; this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", ttwith, ttheigh); this.printDocument1.PrintPage += new PrintPageEventHandler(this.printDocument_Print); printPreviewDialog1.Document = printDocument1; DialogResult res = printPreviewDialog1.ShowDialog(); printPreviewDialog1.Document.Dispose(); } } }