Skip to content

Commit 6919ff0

Browse files
committed
refactoring
1 parent 7b9bd36 commit 6919ff0

5 files changed

+77
-60
lines changed

Form1.Designer.cs

+12-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Form1.cs

+6-48
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
/***********************************************************************
2-
b-PAC 3.0 Component Sample (TestPrint)
3-
4-
(C)Copyright Brother Industries, Ltd. 2009
5-
************************************************************************/
6-
71
using System;
8-
using System.Collections.Generic;
9-
using System.ComponentModel;
10-
using System.Data;
11-
using System.Drawing;
12-
using System.Text;
132
using System.Windows.Forms;
14-
using System.Runtime.InteropServices;
15-
using bpac;
163

17-
namespace BarcodePrint
4+
namespace BpacBarcode
185
{
196
public partial class BarcodeForm : Form
207
{
@@ -30,53 +17,24 @@ private void Form1_Load(object sender, EventArgs e)
3017

3118
if (args.GetLength(0) == 3)
3219
{
33-
doPrint(args[1], args[2]);
20+
(new PrinterAPI(args[1], args[2])).print();
3421
Close();
3522
}
3623
else if (args.GetLength(0) == 4)
3724
{
38-
doPrint(args[1], args[2], args[3]);
25+
(new PrinterAPI(args[1], args[2], args[3])).print();
3926
Close();
4027
}
4128
else if (args.GetLength(0) == 5)
4229
{
43-
doPrint(args[1], args[2], args[3], Int32.Parse(args[4]));
30+
(new PrinterAPI(args[1], args[2], args[3], Int32.Parse(args[4]))).print();
4431
Close();
4532
}
4633
}
4734

4835
private void btnPrint_Click(object sender, EventArgs e)
4936
{
50-
doPrint(txtTitle.Text, txtBarcode.Text, timestamp.Value, (int)cntCopies.Value);
51-
}
52-
53-
private void doPrint(string title, string barcode, DateTime timestamp, int count = 1)
54-
{
55-
doPrint(title, barcode, timestamp.ToString("dd.MM.yyyy HH:mm"), count);
56-
}
57-
58-
private void doPrint(string title, string barcode, string timestamp = "", int count = 1)
59-
{
60-
timestamp = timestamp.Equals("") ? System.DateTime.Now.ToString("dd.MM.yyyy HH:mm") : timestamp;
61-
62-
bpac.DocumentClass doc = new DocumentClass();
63-
string templatePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + doc.Printer.GetMediaId().ToString() + ".lbx";
64-
if (doc.Open(templatePath) != false)
65-
{
66-
doc.GetObject("title").Text = title;
67-
doc.GetObject("barcode").Text = barcode;
68-
doc.GetObject("timestamp").Text = timestamp;
69-
70-
bpac.PrintOptionConstants printOptions = PrintOptionConstants.bpoAutoCut | PrintOptionConstants.bpoQuality;
71-
doc.StartPrint("", printOptions);
72-
doc.PrintOut(count, printOptions);
73-
doc.EndPrint();
74-
doc.Close();
75-
}
76-
else
77-
{
78-
MessageBox.Show("Open() Error: " + doc.ErrorCode + "\nMedia ID" + doc.Printer.GetMediaId().ToString());
79-
}
37+
(new PrinterAPI(txtTitle.Text, txtBarcode.Text, timestamp.Value.ToString(), (int)cntCopies.Value)).print();
8038
}
8139

8240
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
@@ -88,7 +46,7 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
8846
}
8947
else if (keyData == Keys.Enter)
9048
{
91-
doPrint(txtTitle.Text, txtBarcode.Text);
49+
(new PrinterAPI(txtTitle.Text, txtBarcode.Text, timestamp.Value.ToString(), (int)cntCopies.Value)).print();
9250
}
9351
return base.ProcessCmdKey(ref msg, keyData);
9452
}

PrinterAPI.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Windows.Forms;
3+
using bpac;
4+
5+
namespace BpacBarcode
6+
{
7+
class PrinterAPI
8+
{
9+
private string title;
10+
private string barcode;
11+
private string timestamp = System.DateTime.Now.ToString("dd.MM.yyyy HH:mm");
12+
private int count = 1;
13+
14+
public PrinterAPI(string title, string barcode)
15+
{
16+
this.title = title;
17+
this.barcode = barcode;
18+
}
19+
20+
public PrinterAPI(string title, string barcode, string timestamp)
21+
{
22+
this.title = title;
23+
this.barcode = barcode;
24+
this.timestamp = timestamp;
25+
}
26+
27+
public PrinterAPI(string title, string barcode, string timestamp, int count)
28+
{
29+
this.title = title;
30+
this.barcode = barcode;
31+
this.timestamp = timestamp;
32+
this.count = count;
33+
}
34+
35+
public void print()
36+
{
37+
bpac.DocumentClass doc = new DocumentClass();
38+
string templatePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + doc.Printer.GetMediaId().ToString() + ".lbx";
39+
if (doc.Open(templatePath) != false)
40+
{
41+
doc.GetObject("title").Text = title;
42+
doc.GetObject("barcode").Text = barcode;
43+
doc.GetObject("timestamp").Text = timestamp;
44+
45+
bpac.PrintOptionConstants printOptions = PrintOptionConstants.bpoAutoCut | PrintOptionConstants.bpoQuality;
46+
doc.StartPrint("", printOptions);
47+
doc.PrintOut(count, printOptions);
48+
doc.EndPrint();
49+
doc.Close();
50+
}
51+
else
52+
{
53+
MessageBox.Show("Open() Error: " + doc.ErrorCode + "\nMedia ID" + doc.Printer.GetMediaId().ToString());
54+
}
55+
}
56+
}
57+
}

Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Windows.Forms;
44

5-
namespace BarcodePrint
5+
namespace BpacBarcode
66
{
77
static class Program
88
{

bpac-barcode.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Reference Include="System.Xml" />
9090
</ItemGroup>
9191
<ItemGroup>
92+
<Compile Include="PrinterAPI.cs" />
9293
<Compile Include="Form1.cs">
9394
<SubType>Form</SubType>
9495
</Compile>

0 commit comments

Comments
 (0)