Commit 21403dfb 刘韬

1

1 个父辈 1afba36f
...@@ -14,11 +14,11 @@ namespace PrintLabel ...@@ -14,11 +14,11 @@ namespace PrintLabel
/// <summary> /// <summary>
/// 像素毫米转换的倍数 /// 像素毫米转换的倍数
/// </summary> /// </summary>
internal static float MULTIPLE = 203f; internal static float MULTIPLE = 100;
/// <summary> /// <summary>
/// 添加标签时初始的大小,mm /// 添加标签时初始的大小,mm
/// </summary> /// </summary>
internal const string LABEL_SIZE = "50,70"; internal const string LABEL_SIZE = "50,30";
/// <summary> /// <summary>
......
...@@ -4,6 +4,7 @@ using System; ...@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -17,9 +18,12 @@ namespace PrintLabel ...@@ -17,9 +18,12 @@ namespace PrintLabel
private bool change = false; private bool change = false;
private bool isLabel = false; private bool isLabel = false;
public string desc = ""; public string desc = "";
public FrmLabel() public List<string> KeyWord = new List<string>();
public FrmLabel(bool processui=false)
{ {
InitializeComponent(); InitializeComponent();
if (processui)
ProcessUI(this);
} }
private void LabelPrint_FieldSelected(PointF location, SizeF size, Model.LabelFieldType type) private void LabelPrint_FieldSelected(PointF location, SizeF size, Model.LabelFieldType type)
...@@ -27,6 +31,8 @@ namespace PrintLabel ...@@ -27,6 +31,8 @@ namespace PrintLabel
change = true; change = true;
NumX.Enabled = true; NumX.Enabled = true;
NumY.Enabled = true; NumY.Enabled = true;
NumW.Enabled = true;
NumH.Enabled = true;
NumW.Value = Convert.ToDecimal(size.Width < 0.01 ? 0.01 : size.Width); NumW.Value = Convert.ToDecimal(size.Width < 0.01 ? 0.01 : size.Width);
NumH.Value = Convert.ToDecimal(size.Height < 0.01 ? 0.01 : size.Height); NumH.Value = Convert.ToDecimal(size.Height < 0.01 ? 0.01 : size.Height);
NumX.Value = Convert.ToDecimal(location.X); NumX.Value = Convert.ToDecimal(location.X);
...@@ -47,12 +53,14 @@ namespace PrintLabel ...@@ -47,12 +53,14 @@ namespace PrintLabel
private void LabelPrint_LabelSelected(SizeF size) private void LabelPrint_LabelSelected(SizeF size)
{ {
change = true; change = true;
NumW.Value = Convert.ToDecimal(size.Width); NumLabelW.Value = Convert.ToDecimal(size.Width);
NumH.Value = Convert.ToDecimal(size.Height); NumLabelH.Value = Convert.ToDecimal(size.Height);
NumX.Value = 0; NumX.Value = 0;
NumY.Value = 0; NumY.Value = 0;
NumX.Enabled = false; NumX.Enabled = false;
NumY.Enabled = false; NumY.Enabled = false;
NumW.Enabled = false;
NumH.Enabled = false;
TxtFormatText.Text = ""; TxtFormatText.Text = "";
CboFieldType.SelectedIndex = -1; CboFieldType.SelectedIndex = -1;
CboFieldType.Enabled = false; CboFieldType.Enabled = false;
...@@ -71,10 +79,20 @@ namespace PrintLabel ...@@ -71,10 +79,20 @@ namespace PrintLabel
Common.LabelPrint.EditControl(pictureBox1); Common.LabelPrint.EditControl(pictureBox1);
CboFieldType.Items.AddRange(Common.LabelPrint.FieldType); CboFieldType.Items.AddRange(Common.LabelPrint.FieldType);
groupBox1.Visible = false; groupBox1.Visible = false;
cb_keyword.Visible = false;
if (desc != "") { if (desc != "") {
label_desc.Text = desc; label_desc.Text = desc;
groupBox1.Visible = true; groupBox1.Visible = true;
} }
if (KeyWord.Count > 0) {
cb_keyword.Visible = true;
cb_keyword.Items.Add("keyword");
cb_keyword.Items.AddRange(KeyWord.ToArray());
cb_keyword.SelectedIndex = 0;
}
if (LstLabelName.Items.Count > 0)
LstLabelName.SelectedIndex = 0;
} }
private void FrmLabel_FormClosing(object sender, FormClosingEventArgs e) private void FrmLabel_FormClosing(object sender, FormClosingEventArgs e)
...@@ -88,16 +106,20 @@ namespace PrintLabel ...@@ -88,16 +106,20 @@ namespace PrintLabel
private void LstLabelName_SelectedIndexChanged(object sender, EventArgs e) private void LstLabelName_SelectedIndexChanged(object sender, EventArgs e)
{ {
Common.LabelPrint.LabelIndex = LstLabelName.SelectedIndex;
if (LstLabelName.SelectedIndex == -1) return; if (LstLabelName.SelectedIndex == -1) return;
Common.LabelPrint.LabelIndex = LstLabelName.SelectedIndex;
NumW.Value = Convert.ToDecimal(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Width); NumLabelW.Value = Convert.ToDecimal(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Width);
NumH.Value = Convert.ToDecimal(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Height); NumLabelH.Value = Convert.ToDecimal(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Height);
NumX.Value = 0; NumX.Value = 0;
NumY.Value = 0; NumY.Value = 0;
NumX.Enabled = false; NumX.Enabled = false;
NumY.Enabled = false; NumY.Enabled = false;
NumW.Enabled = false;
NumH.Enabled = false;
CboFieldType.Enabled = false; CboFieldType.Enabled = false;
} }
private void BtnLabelNew_Click(object sender, EventArgs e) private void BtnLabelNew_Click(object sender, EventArgs e)
...@@ -114,7 +136,7 @@ namespace PrintLabel ...@@ -114,7 +136,7 @@ namespace PrintLabel
private void BtnLabelDel_Click(object sender, EventArgs e) private void BtnLabelDel_Click(object sender, EventArgs e)
{ {
if (LstLabelName.SelectedIndex == -1) return; if (LstLabelName.SelectedIndex == -1) return;
DialogResult dr = MessageBox.Show("确定要删除 " + LstLabelName.Text + " 吗?", "删除", MessageBoxButtons.YesNo); DialogResult dr = MessageBox.Show("Delete " + LstLabelName.Text + " ?", "Delete", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) return; if (dr == DialogResult.No) return;
Common.LabelPrint.LabelDel(); Common.LabelPrint.LabelDel();
LstLabelName.Items.RemoveAt(LstLabelName.SelectedIndex); LstLabelName.Items.RemoveAt(LstLabelName.SelectedIndex);
...@@ -174,7 +196,7 @@ namespace PrintLabel ...@@ -174,7 +196,7 @@ namespace PrintLabel
{ {
if (change) return; if (change) return;
if (isLabel) if (isLabel)
Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumLabelW.Value), Convert.ToSingle(NumLabelH.Value));
else else
Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value));
} }
...@@ -183,7 +205,7 @@ namespace PrintLabel ...@@ -183,7 +205,7 @@ namespace PrintLabel
{ {
if (change) return; if (change) return;
if (isLabel) if (isLabel)
Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumLabelW.Value), Convert.ToSingle(NumLabelH.Value));
else else
Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value));
} }
...@@ -192,7 +214,7 @@ namespace PrintLabel ...@@ -192,7 +214,7 @@ namespace PrintLabel
{ {
if (change) return; if (change) return;
if (isLabel) if (isLabel)
Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumLabelW.Value), Convert.ToSingle(NumLabelH.Value));
else else
Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value));
} }
...@@ -201,7 +223,7 @@ namespace PrintLabel ...@@ -201,7 +223,7 @@ namespace PrintLabel
{ {
if (change) return; if (change) return;
if (isLabel) if (isLabel)
Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumLabelW.Value), Convert.ToSingle(NumLabelH.Value));
else else
Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value)); Common.LabelPrint.SetFieldSize(Convert.ToSingle(NumX.Value), Convert.ToSingle(NumY.Value), Convert.ToSingle(NumW.Value), Convert.ToSingle(NumH.Value));
} }
...@@ -212,6 +234,77 @@ namespace PrintLabel ...@@ -212,6 +234,77 @@ namespace PrintLabel
Common.LabelPrint.SetFieldFormat(TxtFormatText.Text); Common.LabelPrint.SetFieldFormat(TxtFormatText.Text);
} }
private void cb_keyword_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_keyword.SelectedIndex > 0) {
TxtFormatText.Text = TxtFormatText.Text.Insert(TxtFormatText.SelectionStart, "{"+cb_keyword.SelectedItem.ToString()+"}");
cb_keyword.SelectedIndex = 0;
}
}
public void ProcessUI(Control partentControl)
{
if (IsDesignMode())
return;
partentControl.BackColor = Color.Black;//Color.FromArgb(53, 67, 88);
partentControl.ForeColor = Color.White;
foreach (Control con in partentControl.Controls)
{
con.BackColor = Color.Black; //Color.FromArgb(53, 67, 88);
con.ForeColor = Color.White;
if (con is ComboBox)
{
//(con as ComboBox).DrawMode = DrawMode.OwnerDrawVariable;
}
if (con is ComboBox || con is TextBox)
{
con.BackColor = Color.Black;//Color.FromArgb(160, 165, 172);
con.ForeColor = Color.White; //Color.FromArgb(0, 0, 40); ;
//con.BackColor = Color.White;//Color.FromArgb(160, 165, 172);
//con.ForeColor = Color.Black; //Color.FromArgb(0, 0, 40); ;
}
if (con is LinkLabel)
{
(con as LinkLabel).LinkColor = Color.White;
}
if (con is DataGridView)
{
con.ForeColor = Color.Black;
continue;
}
if (con.Controls.Count > 0)
{
ProcessUI(con);
}
}
}
public static bool IsDesignMode()
{
bool returnFlag = false;
if (Process.GetCurrentProcess().ProcessName == "devenv")
{
returnFlag = true;
}
return returnFlag;
}
private void NumLabelW_ValueChanged(object sender, EventArgs e)
{
Common.LabelPrint.SetLabelSize(Convert.ToSingle(NumLabelW.Value), Convert.ToSingle(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Height));
}
private void NumLabelH_ValueChanged(object sender, EventArgs e)
{
Common.LabelPrint.SetLabelSize(Convert.ToSingle(Common.Label[Common.LabelPrint.LabelIndex].Size_mm.Width),Convert.ToSingle(NumLabelH.Value));
}
} }
} }
...@@ -80,7 +80,7 @@ namespace BLL ...@@ -80,7 +80,7 @@ namespace BLL
else else
{ {
int x = Convert.ToInt32((_pic.Width - Common.Label[labelIdx].Size_px.Width) / 2); int x = Convert.ToInt32((_pic.Width - Common.Label[labelIdx].Size_px.Width) / 2);
int y = Convert.ToInt32((_pic.Height - Common.Label[labelIdx].Size_px.Height) / 2); int y = 20;// Convert.ToInt32((_pic.Height - Common.Label[labelIdx].Size_px.Height) / 4);
offset = new Point(x, y); offset = new Point(x, y);
} }
...@@ -237,7 +237,7 @@ namespace BLL ...@@ -237,7 +237,7 @@ namespace BLL
rect = list[j].Attributes["Rectangle"].Value; rect = list[j].Attributes["Rectangle"].Value;
field.SetLocationSize(rect); field.SetLocationSize(rect);
field.SetFont(list[j].Attributes["Font"].Value); field.SetFont(list[j].Attributes["Font"].Value);
field.SetBarQRCode(field.FormatText); field.SetBarQRCode(field.FormatText,field.Size_px);
label.Field.Add(field); label.Field.Add(field);
} }
Common.Label.Add(label); Common.Label.Add(label);
...@@ -340,7 +340,7 @@ namespace BLL ...@@ -340,7 +340,7 @@ namespace BLL
{ {
if (fieldIdx == -1) return; if (fieldIdx == -1) return;
Common.Label[labelIdx].Field[fieldIdx].FormatText = s; Common.Label[labelIdx].Field[fieldIdx].FormatText = s;
Common.Label[labelIdx].Field[fieldIdx].SetBarQRCode(s); Common.Label[labelIdx].Field[fieldIdx].SetBarQRCode();
if (_pic != null) _pic.Refresh(); if (_pic != null) _pic.Refresh();
} }
...@@ -466,7 +466,11 @@ namespace BLL ...@@ -466,7 +466,11 @@ namespace BLL
using (PrintDialog dlg = new PrintDialog()) using (PrintDialog dlg = new PrintDialog())
{ {
if (dlg.ShowDialog() != DialogResult.OK) return; if (dlg.ShowDialog() != DialogResult.OK) return;
System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument { PrinterSettings = dlg.PrinterSettings }; System.Drawing.Printing.PrintDocument print = new System.Drawing.Printing.PrintDocument {
PrinterSettings = dlg.PrinterSettings
};
//print.PrinterSettings.DefaultPageSettings.Color = false;
print.PrintPage += PrintTest_PrintPage; print.PrintPage += PrintTest_PrintPage;
print.DefaultPageSettings.Landscape = dlg.PrinterSettings.DefaultPageSettings.Landscape; print.DefaultPageSettings.Landscape = dlg.PrinterSettings.DefaultPageSettings.Landscape;
print.Print(); print.Print();
...@@ -501,7 +505,7 @@ namespace BLL ...@@ -501,7 +505,7 @@ namespace BLL
} }
if (field.Type != Model.LabelFieldType.Text) if (field.Type != Model.LabelFieldType.Text)
{ {
field.SetBarQRCode(field.PrintText); field.SetBarQRCode(field.PrintText,field.Size_px);
content.Add(field.PrintText); content.Add(field.PrintText);
} }
} }
...@@ -509,12 +513,15 @@ namespace BLL ...@@ -509,12 +513,15 @@ namespace BLL
Bitmap bmp = new Bitmap((int)Common.Label[labelIdx].Size_px.Width, (int)Common.Label[labelIdx].Size_px.Height); Bitmap bmp = new Bitmap((int)Common.Label[labelIdx].Size_px.Width, (int)Common.Label[labelIdx].Size_px.Height);
Graphics g = Graphics.FromImage(bmp); Graphics g = Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.Clear(Color.White); g.Clear(Color.White);
for (int i = 0; i < Common.Label[labelIdx].Field.Count; i++) for (int i = 0; i < Common.Label[labelIdx].Field.Count; i++)
{ {
Model.ClsLabelField field = Common.Label[labelIdx].Field[i]; Model.ClsLabelField field = Common.Label[labelIdx].Field[i];
if (field.Type == Model.LabelFieldType.Text) if (field.Type == Model.LabelFieldType.Text)
{ {
//StringFormat stringFormat = new StringFormat();
g.DrawString(field.PrintText, field.Font, Brushes.Black, field.GetRectangleF()); g.DrawString(field.PrintText, field.Font, Brushes.Black, field.GetRectangleF());
} }
else else
...@@ -770,6 +777,7 @@ namespace BLL ...@@ -770,6 +777,7 @@ namespace BLL
private void PicEdit_MouseUp(object sender, MouseEventArgs e) private void PicEdit_MouseUp(object sender, MouseEventArgs e)
{ {
} }
private void PicEdit_MouseMove(object sender, MouseEventArgs e) private void PicEdit_MouseMove(object sender, MouseEventArgs e)
...@@ -797,7 +805,11 @@ namespace BLL ...@@ -797,7 +805,11 @@ namespace BLL
oldPoint = e.Location; oldPoint = e.Location;
if (_pic != null) if (_pic != null)
{
if (fieldIdx>=0)
Common.Label[labelIdx].Field[fieldIdx].SetBarQRCode();
_pic.Refresh(); _pic.Refresh();
}
} }
else if (e.Button == MouseButtons.None) else if (e.Button == MouseButtons.None)
{ {
......
...@@ -274,7 +274,7 @@ namespace Model ...@@ -274,7 +274,7 @@ namespace Model
Width = 300, Width = 300,
Height = 300, Height = 300,
Margin = 1, Margin = 1,
PureBarcode = true PureBarcode = true,
}; };
ZXing.PDF417.PDF417EncodingOptions pDF417EncodingOptions = new ZXing.PDF417.PDF417EncodingOptions() ZXing.PDF417.PDF417EncodingOptions pDF417EncodingOptions = new ZXing.PDF417.PDF417EncodingOptions()
{ {
...@@ -302,7 +302,8 @@ namespace Model ...@@ -302,7 +302,8 @@ namespace Model
Image = writer.Write(content); Image = writer.Write(content);
break; break;
case LabelFieldType.Code128: case LabelFieldType.Code128:
writer.Format = ZXing.BarcodeFormat.CODE_128; writer.Format = ZXing.BarcodeFormat.CODE_128;
Image = writer.Write(content); Image = writer.Write(content);
break; break;
case LabelFieldType.DataMatrix: case LabelFieldType.DataMatrix:
...@@ -327,13 +328,88 @@ namespace Model ...@@ -327,13 +328,88 @@ namespace Model
} }
} }
/// <summary>
/// 设置条码或二维码图片
/// </summary>
/// <param name="content">内容</param>
public void SetBarQRCode(string content,SizeF size)
{
try
{
Image = null;
if (Type == LabelFieldType.Text) return;
ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter();
ZXing.QrCode.QrCodeEncodingOptions qrCodeEncodingOptions = new ZXing.QrCode.QrCodeEncodingOptions()
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = (int)size.Width,
Height = (int)size.Width,
Margin = 1,
PureBarcode = true
};
ZXing.PDF417.PDF417EncodingOptions pDF417EncodingOptions = new ZXing.PDF417.PDF417EncodingOptions()
{
AspectRatio = PDF417AspectRatio.AUTO,
Compact = false,
Compaction = Compaction.AUTO,
DisableECI = true,
ErrorCorrection = PDF417ErrorCorrectionLevel.AUTO,
PureBarcode = false,
GS1Format = false,
Width = (int)size.Width,
CharacterSet = "UTF-8",
Height = (int)size.Height,
Margin = 1
};
writer.Options.PureBarcode = true;
//writer.Options.Margin = 0;
//writer.Options.Height = (int)size.Height;
writer.Options.Width = (int)size.Width;
switch (Type)
{
case LabelFieldType.Code39:
writer.Format = ZXing.BarcodeFormat.CODE_39;
Image = writer.Write(content);
break;
case LabelFieldType.Code93:
writer.Format = ZXing.BarcodeFormat.CODE_93;
Image = writer.Write(content);
break;
case LabelFieldType.Code128:
writer.Format = ZXing.BarcodeFormat.CODE_128;
Image = writer.Write(content);
break;
case LabelFieldType.DataMatrix:
writer.Format = ZXing.BarcodeFormat.DATA_MATRIX;
Image = writer.Write(content);
break;
case LabelFieldType.QRCode:
writer.Options = qrCodeEncodingOptions;
writer.Format = ZXing.BarcodeFormat.QR_CODE;
Image = writer.Write(content);
break;
case LabelFieldType.PDF417:
writer.Options = pDF417EncodingOptions;
writer.Format = ZXing.BarcodeFormat.PDF_417;
Image = writer.Write(content);
break;
}
}
catch (Exception ex)
{
}
}
/// <summary> /// <summary>
/// 设置条码或二维码图片 /// 设置条码或二维码图片
/// </summary> /// </summary>
public void SetBarQRCode() public void SetBarQRCode()
{ {
SetBarQRCode(FormatText); SetBarQRCode(FormatText, Size_px);
} }
/// <summary> /// <summary>
......
...@@ -15,8 +15,9 @@ namespace Asa ...@@ -15,8 +15,9 @@ namespace Asa
private System.Threading.Thread tRun; private System.Threading.Thread tRun;
public PrintLabel(string dir) public PrintLabel(string dir, int dpi = 100)
{ {
Common.MULTIPLE = dpi;
Common.LabelPrint = new BLL.LabelPrint(); Common.LabelPrint = new BLL.LabelPrint();
Common.LabelPrint.Load(dir); Common.LabelPrint.Load(dir);
......
...@@ -9,9 +9,10 @@ ...@@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Asa</RootNamespace> <RootNamespace>Asa</RootNamespace>
<AssemblyName>Asa.PrintLabel</AssemblyName> <AssemblyName>Asa.PrintLabel</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
......
...@@ -21,7 +21,7 @@ namespace PrintLabel_Test ...@@ -21,7 +21,7 @@ namespace PrintLabel_Test
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
print = new Asa.PrintLabel(Application.StartupPath + "\\Label"); print = new Asa.PrintLabel(Application.StartupPath + "\\Label",100);
print.PrintStatusChanged += Print_PrintStatusChanged; print.PrintStatusChanged += Print_PrintStatusChanged;
} }
......
...@@ -21,7 +21,7 @@ namespace PrintLabel_Test ...@@ -21,7 +21,7 @@ namespace PrintLabel_Test
private void Form2_Load(object sender, EventArgs e) private void Form2_Load(object sender, EventArgs e)
{ {
print = new Asa.PrintLabel(Application.StartupPath + "\\Label"); print = new Asa.PrintLabel(Application.StartupPath + "\\Label",300);
reloadlabel(); reloadlabel();
ConfigHelper.Config.PropertyBind("IP", textBox1, "Text", "TextChanged"); ConfigHelper.Config.PropertyBind("IP", textBox1, "Text", "TextChanged");
ConfigHelper.Config.PropertyBind<int>("labelindex", comboBox1, "SelectedIndex", "SelectedIndexChanged"); ConfigHelper.Config.PropertyBind<int>("labelindex", comboBox1, "SelectedIndex", "SelectedIndexChanged");
...@@ -29,7 +29,9 @@ namespace PrintLabel_Test ...@@ -29,7 +29,9 @@ namespace PrintLabel_Test
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
print.EditLabel(); PrintLabel.FrmLabel frmLabel = new PrintLabel.FrmLabel();
frmLabel.KeyWord = new List<string>() { "123", "234" };
frmLabel.ShowDialog();
reloadlabel(); reloadlabel();
} }
void reloadlabel() { void reloadlabel() {
......
...@@ -16,7 +16,7 @@ namespace PrintLabel_Test ...@@ -16,7 +16,7 @@ namespace PrintLabel_Test
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2()); Application.Run(new Form1());
} }
} }
} }
...@@ -18,13 +18,14 @@ namespace ZebraPrinterHelper ...@@ -18,13 +18,14 @@ namespace ZebraPrinterHelper
{ {
ConnectionType ConnectionType; ConnectionType ConnectionType;
string ConnectionEndpoint; string ConnectionEndpoint;
public int PrinterDPI=203; public int PrinterDPI = 203;
/// <summary> /// <summary>
/// 初始化打印机设置 /// 初始化打印机设置
/// </summary> /// </summary>
/// <param name="endpoint">连接地址</param> /// <param name="endpoint">连接地址</param>
/// <param name="connectionType">连接类型</param> /// <param name="connectionType">连接类型</param>
public ZebraManger(string endpoint, ConnectionType connectionType) { public ZebraManger(string endpoint, ConnectionType connectionType)
{
ConnectionEndpoint = endpoint; ConnectionEndpoint = endpoint;
ConnectionType = connectionType; ConnectionType = connectionType;
} }
...@@ -48,7 +49,8 @@ namespace ZebraPrinterHelper ...@@ -48,7 +49,8 @@ namespace ZebraPrinterHelper
{ {
try try
{ {
if (string.IsNullOrEmpty(ConnectionEndpoint)) { if (string.IsNullOrEmpty(ConnectionEndpoint))
{
var dl = GetUsbDirectAddress(); var dl = GetUsbDirectAddress();
if (dl.Count == 0) if (dl.Count == 0)
...@@ -85,7 +87,8 @@ namespace ZebraPrinterHelper ...@@ -85,7 +87,8 @@ namespace ZebraPrinterHelper
/// </summary> /// </summary>
/// <param name="msg">错误消息</param> /// <param name="msg">错误消息</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool Connection(out string msg) { public bool Connection(out string msg)
{
msg = ""; msg = "";
try try
{ {
...@@ -124,7 +127,8 @@ namespace ZebraPrinterHelper ...@@ -124,7 +127,8 @@ namespace ZebraPrinterHelper
/// <summary> /// <summary>
/// 关闭打印机连接 /// 关闭打印机连接
/// </summary> /// </summary>
public void Close() { public void Close()
{
try try
{ {
if (printerConnection != null) if (printerConnection != null)
...@@ -141,7 +145,8 @@ namespace ZebraPrinterHelper ...@@ -141,7 +145,8 @@ namespace ZebraPrinterHelper
} }
} }
~ZebraManger() { ~ZebraManger()
{
Close(); Close();
} }
/// <summary> /// <summary>
...@@ -149,51 +154,66 @@ namespace ZebraPrinterHelper ...@@ -149,51 +154,66 @@ namespace ZebraPrinterHelper
/// </summary> /// </summary>
/// <param name="bmp"></param> /// <param name="bmp"></param>
/// <param name="msg">错误消息</param> /// <param name="msg">错误消息</param>
/// <returns></returns>
public bool PrintImage(Bitmap bmp, out string msg) {
return PrintImage(bmp, out msg, true);
}
/// <summary>
/// 打印图像
/// </summary>
/// <param name="bmp"></param>
/// <param name="msg">错误消息</param>
/// <param name="checkPeeler">是否检查剥离器</param>
/// <returns>是否成功</returns> /// <returns>是否成功</returns>
public bool PrintImage(Bitmap bmp, out string msg) public bool PrintImage(Bitmap bmp, out string msg,bool checkPeeler)
{ {
if (!CheckAndGetStatus(out PrinterStatus status, out msg)) if (!CheckAndGetStatus(out PrinterStatus status, out msg))
return false; return false;
if (status.labelsRemainingInBatch>0) if (status.labelsRemainingInBatch > 0)
CancelAll(); CancelAll();
if (!status.isReadyToPrint) {
if (!status.isReadyToPrint)
{
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage(); string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
msg = string.Join("\r\n", printerStatusString); msg = string.Join("\r\n", printerStatusString);
return false; return false;
} }
if (IsLabelOnPeeler) { if (checkPeeler && IsLabelOnPeeler)
{
msg = "上一个标签尚未移走"; msg = "上一个标签尚未移走";
return false; return false;
} }
try try
{ {
//var width = bmp.Width * ((double)PrinterDPI / 300);
//var height = bmp.Height * ((double)PrinterDPI / 300);
//var db = GetReducedImage(bmp, (int)width, (int)height);
//db.Save("temp.bmp");
ZebraImageI image = ZebraImageFactory.GetImage(bmp); ZebraImageI image = ZebraImageFactory.GetImage(bmp);
var width = image.Width; // * (PrinterDPI/203f); printer.PrintImage(image, 0, 0, (int)bmp.Width, (int)bmp.Height, false);
var height = image.Height; // * (PrinterDPI / 203f);
printer.PrintImage(image, 0, 0, (int)width, (int)height, false);
return true; return true;
} }
catch (ConnectionException e) catch (ConnectionException e)
{ {
msg = "Connection Error:" + e.Message; msg = "Connection Error:" + e.ToString();
} }
catch (ZebraPrinterLanguageUnknownException e) catch (ZebraPrinterLanguageUnknownException e)
{ {
msg = "Language Unknown:" + e.Message; msg = "Language Unknown:" + e.ToString();
} }
catch (IOException e) catch (IOException e)
{ {
msg = "Image Error" + e.Message; msg = "Image Error" + e.ToString();
} }
catch (ZebraIllegalArgumentException e) catch (ZebraIllegalArgumentException e)
{ {
msg = "Illegal Arguments"+e.Message; msg = "Illegal Arguments" + e.ToString();
} }
catch (ArgumentException e) catch (ArgumentException e)
{ {
msg = "Invalid File Path" + e.Message; msg = "Invalid File Path" + e.ToString();
} }
finally finally
{ {
...@@ -201,11 +221,12 @@ namespace ZebraPrinterHelper ...@@ -201,11 +221,12 @@ namespace ZebraPrinterHelper
} }
return false; return false;
} }
bool CheckAndGetStatus(out PrinterStatus status, out string msg) { bool CheckAndGetStatus(out PrinterStatus status, out string msg)
{
msg = ""; msg = "";
status = null; status = null;
int ReTryCount = 0; int ReTryCount = 0;
ReTry: ReTry:
if (printerConnection == null || !printerConnection.Connected) if (printerConnection == null || !printerConnection.Connected)
{ {
if (!Connection(out string m)) if (!Connection(out string m))
...@@ -214,7 +235,7 @@ namespace ZebraPrinterHelper ...@@ -214,7 +235,7 @@ namespace ZebraPrinterHelper
return false; return false;
} }
} }
status = GetStatus(); status = GetStatus();
if (status == null) if (status == null)
{ {
Close(); Close();
...@@ -226,7 +247,18 @@ namespace ZebraPrinterHelper ...@@ -226,7 +247,18 @@ namespace ZebraPrinterHelper
} }
goto ReTry; goto ReTry;
} }
ReTryCount = 0;
if (status.isPaused)
{
PrintStartMode();
ReTryCount++;
if (ReTryCount > 2)
{
msg = "暂停状态无法解除";
return false;
}
goto ReTry;
}
return true; return true;
} }
/// <summary> /// <summary>
...@@ -247,7 +279,8 @@ namespace ZebraPrinterHelper ...@@ -247,7 +279,8 @@ namespace ZebraPrinterHelper
//log("sensor.peeler" + rs); //log("sensor.peeler" + rs);
return rs.IndexOf("not clear") > -1; return rs.IndexOf("not clear") > -1;
} }
catch { catch
{
return false; return false;
} }
} }
...@@ -268,7 +301,8 @@ namespace ZebraPrinterHelper ...@@ -268,7 +301,8 @@ namespace ZebraPrinterHelper
rs = rs.Trim('\"'); rs = rs.Trim('\"');
return int.Parse(rs); return int.Parse(rs);
} }
else { else
{
throw new ZebraPrinterLanguageUnknownException("Get DPI Error!"); throw new ZebraPrinterLanguageUnknownException("Get DPI Error!");
} }
return 203; return 203;
...@@ -277,7 +311,8 @@ namespace ZebraPrinterHelper ...@@ -277,7 +311,8 @@ namespace ZebraPrinterHelper
/// <summary> /// <summary>
/// 取消所有任务 /// 取消所有任务
/// </summary> /// </summary>
public void CancelAll() { public void CancelAll()
{
try try
{ {
string d = "~JA\n"; string d = "~JA\n";
...@@ -292,7 +327,8 @@ namespace ZebraPrinterHelper ...@@ -292,7 +327,8 @@ namespace ZebraPrinterHelper
/// 写入ZPL指令 /// 写入ZPL指令
/// </summary> /// </summary>
/// <param name="zplcommand"></param> /// <param name="zplcommand"></param>
public void WriteZPL(string zplcommand) { public void WriteZPL(string zplcommand)
{
var b = Encoding.ASCII.GetBytes(zplcommand); var b = Encoding.ASCII.GetBytes(zplcommand);
printerConnection.Write(b); printerConnection.Write(b);
} }
...@@ -320,7 +356,8 @@ namespace ZebraPrinterHelper ...@@ -320,7 +356,8 @@ namespace ZebraPrinterHelper
var b = Encoding.ASCII.GetBytes(d); var b = Encoding.ASCII.GetBytes(d);
printerConnection.Write(b); printerConnection.Write(b);
} }
public bool IsPrinterReady(out string msg) { public bool IsPrinterReady(out string msg)
{
msg = ""; msg = "";
var status = GetStatus(); var status = GetStatus();
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage(); string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
...@@ -334,14 +371,15 @@ namespace ZebraPrinterHelper ...@@ -334,14 +371,15 @@ namespace ZebraPrinterHelper
{ {
msg = "打印机缓冲池内有未打印文档"; msg = "打印机缓冲池内有未打印文档";
return false; return false;
} }
return true; return true;
} }
/// <summary> /// <summary>
/// 获取打印机状态 /// 获取打印机状态
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public PrinterStatus GetStatus() { public PrinterStatus GetStatus()
{
if (printerConnection == null || !printerConnection.Connected) if (printerConnection == null || !printerConnection.Connected)
{ {
if (!Connection(out string m)) if (!Connection(out string m))
...@@ -356,11 +394,13 @@ namespace ZebraPrinterHelper ...@@ -356,11 +394,13 @@ namespace ZebraPrinterHelper
//ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer); //ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);
//return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus(); //return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();
} }
catch { catch
{
return null; return null;
} }
} }
public static Dictionary<string, Dictionary<string,string>> GetUsbDirectAddress() { public static Dictionary<string, Dictionary<string, string>> GetUsbDirectAddress()
{
var devicelist = new Dictionary<string, Dictionary<string, string>>(); var devicelist = new Dictionary<string, Dictionary<string, string>>();
foreach (DiscoveredUsbPrinter printer in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter())) foreach (DiscoveredUsbPrinter printer in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter()))
{ {
...@@ -368,9 +408,37 @@ namespace ZebraPrinterHelper ...@@ -368,9 +408,37 @@ namespace ZebraPrinterHelper
} }
return devicelist; return devicelist;
} }
private Image GetReducedImage(Image resourceImage, int width, int height)
{
try
{
Image data = null;
//用指定的大小和格式初始化Bitmap类的新实例
using (Bitmap bitmap = new Bitmap(width, height, resourceImage.PixelFormat))
{
//从指定的Image对象创建新Graphics对象
using (Graphics graphics = Graphics.FromImage(bitmap))
{
//清除整个绘图面并以透明背景色填充
//graphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片对象
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
//graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.None;
graphics.DrawImage(resourceImage, new Rectangle(0, 0, width, height));
}
data = new Bitmap(bitmap);
}
return data;
}
catch (Exception e)
{
throw e;
}
}
} }
public enum ConnectionType public enum ConnectionType
{ {
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!