-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPringtForm.cs
284 lines (248 loc) · 10.5 KB
/
PringtForm.cs
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using Seagull.BarTender.Print;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace SwiftLabel
{
public partial class PrintForm : Form
{
private Engine engine = null; // The BarTender Print Engine
private LabelFormatDocument format = null;
private string previewPath = "";
private string _btw_path = "";
private string _configPath; // 移除初始值
private IniFile _iniFile;
private Messages messages;
public PrintForm()
{
InitializeComponent();
// 在构造函数中设置配置文件路径
_configPath = Path.Combine(Application.StartupPath, "config.ini");
}
private void PrintForm_Load(object sender, EventArgs e)
{
try
{
engine = new Engine(true);
}
catch (PrintEngineException exception)
{
MessageBox.Show(this, exception.Message, Application.ProductName);
return;
}
_iniFile = new IniFile(_configPath); // 使用完整路径
LoadConfig();
//创建临时文件夹
string tempPath = Path.GetTempPath();
string newFolder;
do
{
newFolder = Path.GetRandomFileName();
previewPath = tempPath + newFolder;
} while (Directory.Exists(previewPath));
Directory.CreateDirectory(previewPath);
}
private void btnFileOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Multiselect = false;//多个文件
dialog.Title = "请选择要打印的文件";
dialog.Filter = "bwt文件(*.btw)|*.btw";
dialog.InitialDirectory = Application.StartupPath;
if (dialog.ShowDialog() == DialogResult.OK)
{
_btw_path = dialog.FileName;
fileNametBox.Text = dialog.SafeFileName;
fileNametBox.BackColor = Color.LightGreen;
if (OpenBtw())
{
fileNametBox.Enabled = false;
}
else
{
fileNametBox.Enabled = true;
}
}
}
}
private bool OpenBtw()
{
string errorMessage = "";
try
{
if (format != null)
format.Close(SaveOptions.DoNotSaveChanges);
format = engine.Documents.Open(_btw_path);
pictureBox.Image = null;
if (format != null)
{
format.ExportImageToFile(previewPath + @"\exp.bmp", ImageType.BMP, Seagull.BarTender.Print.ColorDepth.ColorDepth256, new Resolution(300, 300
), OverwriteOptions.Overwrite);
Image image = Image.FromFile(previewPath + @"\exp.bmp");
Bitmap NmpImage = new Bitmap(image);
pictureBox.Image = NmpImage;
image.Dispose();
}
return true;
}
catch (System.Runtime.InteropServices.COMException comException)
{
errorMessage = String.Format("无法打开格式: {0}\nReason: {1}", _btw_path, comException.Message);
format = null;
MessageBox.Show(this, errorMessage, "出现错误");
return false;
}
}
private void PrintForm_FormClosed(object sender, FormClosedEventArgs e)
{
engine?.Stop(SaveOptions.DoNotSaveChanges);
if (previewPath.Length != 0)
Directory.Delete(previewPath, true);
}
private void saveConfig_Click(object sender, EventArgs e)
{
if (passWrod.Text != "2024")
{
MessageBox.Show(this, "密码错误,请确认", Application.ProductName);
return;
}
try
{
_iniFile.WriteValue("Settings", "Hint", hint.Text);
_iniFile.WriteValue("Settings", "DataName", dataName.Text);
_iniFile.WriteValue("Settings", "StartIndex", inputStart.Text);
_iniFile.WriteValue("Settings", "ContinuousLength", inputContinuous.Text);
_iniFile.WriteValue("Settings", "TotalLength", inputNum.Text);
_iniFile.WriteValue("Settings", "VerifyText", inputText.Text);
_iniFile.WriteValue("Settings", "VerifyEnabled", checkVerify.Checked.ToString());
_iniFile.WriteValue("Settings", "AutoPrintEnabled", checkAutoPrint.Checked.ToString());
_iniFile.WriteValue("Settings", "fileNamePath", _btw_path);
MessageBox.Show(this, "配置保存成功", Application.ProductName);
}
catch (Exception ex)
{
MessageBox.Show("保存配置文件失败:" + ex.Message, "错误");
}
}
void PrintBar(bool isPreView = false)
{
try
{
format.SubStrings.SetSubString(dataName.Text, pringData.Text);
}
catch (Exception ex)
{
MessageBox.Show("修改内容出错 " + ex.Message, "操作提示");
}
if (format != null)
{
format.ExportImageToFile(previewPath + @"\exp.bmp", ImageType.BMP, Seagull.BarTender.Print.ColorDepth.ColorDepth256, new Resolution(350, 165
), OverwriteOptions.Overwrite);
Image image = Image.FromFile(previewPath + @"\exp.bmp");
Bitmap NmpImage = new Bitmap(image);
pictureBox.Image = NmpImage;
image.Dispose();
}
else
{
MessageBox.Show(this, "生成图片错误", Application.ProductName);
}
frontData.Text = pringData.Text;
if (isPreView) return;
//if (printer_comboBox.SelectedItem != null)
// format.PrintSetup.PrinterName = printer_comboBox.SelectedItem.ToString();
format.Print("BarPrint" + DateTime.Now, 1);
pringData.Clear();
}
private void pringData_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && checkAutoPrint.Checked) {
if (checkVerify.Checked)
{
if (inputStart.Text != "" && inputContinuous.Text != "" && inputText.Text != "")
{
string substring = pringData.Text.Substring(Convert.ToInt32(inputStart.Text) - 1, Convert.ToInt32(inputContinuous.Text));
if (pringData.Text.Length < Convert.ToInt32(inputContinuous.Text) + Convert.ToInt32(inputStart.Text))
{
MessageBox.Show(this, "数据设置长度错误,请确认\n" + pringData.Text.Length, Application.ProductName);
return;
}
if (substring != inputText.Text)
{
MessageBox.Show(this, "数据取值内容错误,请确认\n" + substring, Application.ProductName);
return;
}
}
if (pringData.Text.Length != Convert.ToInt32(inputNum.Text))
{
MessageBox.Show(this, "数据取值长度错误,请确认\n" + pringData.Text.Length, Application.ProductName);
return;
}
PrintBar();
}
else
{
PrintBar();
}
}
}
// 添加读取配置方法
private void LoadConfig()
{
try
{
hintShow.Text = _iniFile.ReadValue("Settings", "Hint", "");
hint.Text = _iniFile.ReadValue("Settings", "Hint", "");
dataName.Text = _iniFile.ReadValue("Settings", "DataName", "");
inputStart.Text = _iniFile.ReadValue("Settings", "StartIndex", "0");
inputContinuous.Text = _iniFile.ReadValue("Settings", "ContinuousLength", "0");
inputNum.Text = _iniFile.ReadValue("Settings", "TotalLength", "0");
inputText.Text = _iniFile.ReadValue("Settings", "VerifyText", "");
string verifyEnabled = _iniFile.ReadValue("Settings", "VerifyEnabled", "False");
checkVerify.Checked = bool.Parse(verifyEnabled);
string AutoPrintEnabled = _iniFile.ReadValue("Settings", "AutoPrintEnabled", "False");
checkAutoPrint.Checked = bool.Parse(AutoPrintEnabled);
// 读取并验证btw文件路径
string filePath = _iniFile.ReadValue("Settings", "fileNamePath", "");
if (!string.IsNullOrEmpty(filePath) &&
File.Exists(filePath) &&
Path.GetExtension(filePath).ToLower() == ".btw")
{
_btw_path = filePath;
fileNametBox.Text = Path.GetFileName(filePath);
fileNametBox.BackColor = Color.LightGreen;
format = engine.Documents.Open(_btw_path);
}
}
catch (Exception ex)
{
MessageBox.Show("读取配置文件失败:" + ex.Message, "错误");
}
}
private void passWrod_KeyPress(object sender, KeyPressEventArgs e)
{
if (passWrod.Text != "2024")
{
return;
}
btnFileOpen.Enabled = true;
dataName.Enabled = true;
checkVerify.Enabled = true;
checkAutoPrint.Enabled = true;
inputStart.Enabled = true;
inputContinuous.Enabled = true;
inputText.Enabled = true;
inputNum.Enabled = true;
saveConfig.Enabled = true;
}
}
}