PrintLabel.cs
6.7 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
229
230
231
232
233
234
235
using PrintLabel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Management;
using System.Printing;
namespace Asa
{
public class PrintLabel
{
public delegate void PrintStatusEvent(PrinterStatus sta, string msg);
public event PrintStatusEvent PrintStatusChanged;
private System.Threading.Thread tRun;
public PrintLabel(string dir)
{
Common.LabelPrint = new BLL.LabelPrint();
Common.LabelPrint.Load(dir);
}
/// <summary>
/// 加载标签
/// </summary>
/// <param name="labelName"></param>
public void LoadLabel(string labelName)
{
Common.LabelPrint.LabelName = labelName;
}
/// <summary>
/// 编辑标签
/// </summary>
public void EditLabel()
{
FrmLabel frm = new FrmLabel();
frm.ShowDialog();
}
public string[] GetLabelName()
{
return Common.LabelPrint.GetName();
}
public string[] GetPrinterName()
{
List<string> list = new List<string>();
foreach (string ss in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
list.Add(ss);
return list.ToArray();
}
/// <summary>
/// 打印机
/// </summary>
/// <param name="name">打印机名字</param>
/// <param name="landscape">打印方向,横向纵向</param>
public void Printer(string name, bool landscape)
{
Common.LabelPrint.PrinterName = name;
Common.LabelPrint.PrintLandscape = landscape;
}
public void SetResolution(int printerDPI)
{
Common.MULTIPLE = printerDPI;
}
public bool Rotate180 { set => Common.LabelPrint.Rotate180 = value; get => Common.LabelPrint.Rotate180; }
/// <summary>
/// 打印
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Bitmap Print(Dictionary<string, string> key)
{
Bitmap bmp = Common.LabelPrint.PrintLast(key, out string[] codeContent);
tRun = new System.Threading.Thread(new System.Threading.ThreadStart(GetStatus));
tRun.Start();
return bmp;
}
public Bitmap PrintPreview(Dictionary<string, string> key)
{
//Common.LabelPrint.labelIdx = 1;
Bitmap bmp = Common.LabelPrint.PrintPreview(key, out string[] code);
return bmp;
}
/// <summary>
/// 打印机状态
/// </summary>
public enum PrinterStatus
{
/// <summary>
/// 其他
/// </summary>
Other = 1,
/// <summary>
/// 未知
/// </summary>
Unknown,
/// <summary>
/// 空闲
/// </summary>
Idle,
/// <summary>
/// 正在打印中
/// </summary>
Printing,
/// <summary>
/// 预热
/// </summary>
Preheat,
/// <summary>
/// 停止打印
/// </summary>
Stop,
/// <summary>
/// 打印中
/// </summary>
Print,
/// <summary>
/// 离线
/// </summary>
Offline
}
/// <summary>
/// 获取打印机的当前状态
/// </summary>
public void GetStatus()
{
int n = 0;
int timeout = 0;
string msg = "无";
PrinterStatus sta = PrinterStatus.Other;
string path = "win32_printer.DeviceId='" + Common.LabelPrint.PrinterName + "'";
ManagementObject printer = new ManagementObject(path);
while (true)
{
printer.Get();
int ret = Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
sta = (PrinterStatus)ret;
switch(sta)
{
case PrinterStatus.Other:
msg = "其他";
n = 3;
break;
case PrinterStatus.Unknown:
msg = "未知";
n = 3;
break;
case PrinterStatus.Stop:
msg = "停止";
n = 3;
break;
case PrinterStatus.Offline:
msg = "离线";
n = 3;
break;
case PrinterStatus.Preheat:
msg = "预热";
break;
case PrinterStatus.Print:
msg = "打印中";
n = 1;
break;
case PrinterStatus.Printing:
msg = "打印中";
n = 1;
break;
case PrinterStatus.Idle:
if (n == 1)
{
msg += ",空闲";
n = 2;
}
break;
default:
msg = "default";
n = 3;
break;
}
if (n == 3 || n == 2)
break;
timeout += 500;
System.Threading.Thread.Sleep(500);
if (timeout >= 15000)
break;
}
PrintStatusChanged?.Invoke(sta, msg);
}
private void GetPrintStatus()
{
PrintQueue pq = LocalPrintServer.GetDefaultPrintQueue();
switch (pq.QueueStatus)
{
//正常状态
case PrintQueueStatus.None:
Console.WriteLine("正常运行");
break;
//纸未取走
case PrintQueueStatus.OutputBinFull:
Console.WriteLine("未取走");
break;
//缺纸
case PrintQueueStatus.PaperOut:
Console.WriteLine("缺纸");
break;
//打印
case PrintQueueStatus.Printing:
Console.WriteLine("正在打印");
break;
default:
Console.WriteLine(pq.QueueStatus);
break;
}
}
}
}