kingno
2025-03-31 f5bd202c943df54f160d3ae2dd44e8ef150a8b7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
 
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();
        }
    }
}