FrmShelfPrinterMain.cs
8.2 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShelfPrinter
{
public partial class FrmShelfPrinterMain : Form
{
private Asa.PrintLabel print;
string labelName;
private ContextMenuStrip notifyMenu;
public FrmShelfPrinterMain()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
PrintLabel.Common.MULTIPLE = 100f;
print = new Asa.PrintLabel(Application.StartupPath + "\\Label");
print.PrintStatusChanged += Print_PrintStatusChanged;
labelName = ConfigAppSettings.GetValue("LabelName", "lizhen");
string printerName = ConfigAppSettings.GetValue("PrinterName", "Xprinter XP-236B");
string[] printerNames = print.GetPrinterName();
if (printerNames == null || printerNames.Length == 0)
{
MessageBox.Show("未检测到打印机,请检查打印机是否连接");
return;
}
notifyMenu = new ContextMenuStrip();
ToolStripMenuItem itemShow = new ToolStripMenuItem("显示(&S)");
itemShow.Click += ItemShow_Click;
ToolStripMenuItem itemExit = new ToolStripMenuItem("退出(&X)");
itemExit.Click += ItemExit_Click;
notifyMenu.Items.Add(itemShow);
notifyMenu.Items.Add(itemExit);
notifyIcon1.ContextMenuStrip = notifyMenu;
lblLabelName.Text = labelName;
txtIp.Text = printerName; //printerNames[0];
print.Printer(printerName, false);
timer1.Enabled = true;
}
Asa.PrintLabel.PrinterStatus printerStatus = Asa.PrintLabel.PrinterStatus.Idle;
private void Print_PrintStatusChanged(Asa.PrintLabel.PrinterStatus sta, string msg)
{
this.Invoke(new Action(() =>
{
printerStatus = sta;
lblState.Text = $"打印机状态:{msg}";
}));
}
private void ItemExit_Click(object sender, EventArgs e)
{
//notify.Dispose();
//exit = true;
//Close();
ExitApp();
}
private void ItemShow_Click(object sender, EventArgs e)
{
this.Opacity = 100;
this.Visible = true;
//this.WindowState = FormWindowState.Maximized;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
void printLabel(Label_LZ label_LZ)
{
print.LoadLabel(labelName);
Dictionary<string, string> text = new Dictionary<string, string>();
text.Add("lh", label_LZ.pn);
text.Add("jt", label_LZ.station);
text.Add("xb", label_LZ.line);
text.Add("sj", label_LZ.shijian);
text.Add("zb", label_LZ.slot);
pictureBox1.Image = print.Print(text);
}
Dictionary<string, string> printview(Label_LZ label_LZ)
{
print.LoadLabel(labelName);
Dictionary<string, string> text = new Dictionary<string, string>();
text.Add("lh", label_LZ.pn);
text.Add("jt", label_LZ.station);
text.Add("xb", label_LZ.line);
text.Add("sj", label_LZ.shijian);
text.Add("zb", label_LZ.slot);
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
}
pictureBox1.Image = print.PrintPreview(text);
return text;
}
private void button1_Click(object sender, EventArgs e)
{
print.EditLabel();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
groupBox1.Enabled = checkBox1.Checked;
timer1.Enabled = !checkBox1.Checked;
}
private void lblLabelName_Click(object sender, EventArgs e)
{
ConfigHelper.AdvanceConfigForm.ShowEditDialog(this);
}
private void button2_Click(object sender, EventArgs e)
{
printLabel(new Label_LZ(textBox1.Text, textBox3.Text, textBox4.Text, textBox2.Text,textBox5.Text,textBox6.Text));
}
object handleObj = new object();
private void timer1_Tick(object sender, EventArgs e)
{
if (Monitor.TryEnter(handleObj))
{
try
{
Label_LZ labels = SServerManager.GetPrintInfo();
if (labels != null)
{
lblLabelInfo.Text = "";
// if (printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Idle))
{
timer1.Enabled = false;
//foreach (var item in labels)
{
lblLabelInfo.Text = $"打印任务:{labels.ToStr()}";
printLabel(new Label_LZ(labels.pn, labels.side, labels.station, labels.line));
while (printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Printing)
|| printerStatus.Equals(Asa.PrintLabel.PrinterStatus.Print))
{
Thread.Sleep(300);
}
LogUtil.info($"自动打印[{printerStatus.ToString()}]:{labels.ToStr()}");
while (true)
{
if (SServerManager.FinishPrint())
break;
Thread.Sleep(1000);
}
}
timer1.Enabled = true;
}
//else
//{
// lblLabelInfo.Text = "无法打印标签,因打印机不空闲";
//}
}
}
catch (Exception ex)
{
LogUtil.error($"处理打印异常:", ex);
}
finally
{
Monitor.Exit(handleObj);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
print.PrintPreview(printview(new Label_LZ(textBox1.Text, textBox3.Text, textBox4.Text, textBox2.Text)));
}
catch
{
}
}
private void FrmShelfPrinterMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)//当界面点击窗体右上角X按钮或(Alt + F4)时 发生
{
e.Cancel = true;
HideForm();
}
}
private void HideForm()
{
this.Opacity = 0;
this.ShowInTaskbar = false;
this.notifyIcon1.Visible = true;
this.Hide();
GC.Collect();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Opacity = 100;
this.Visible = true;
//this.WindowState = FormWindowState.Maximized;
this.notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
private void ExitApp()
{
DialogResult result = MessageBox.Show("是否确定退出客户端?", "提示", MessageBoxButtons.YesNo);
if (result.Equals(DialogResult.Yes))
{
try
{
System.Environment.Exit(System.Environment.ExitCode);
}
catch (Exception ex)
{
LogUtil.error("退出出错:", ex);
}
//this.Close();
}
}
}
}