using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Threading;
namespace BarCodeTest
{
public partial class Form1 : Form
{
string inputString = "";
code128 barcode = new code128();
Thread thre;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//label14.Location = new Point((panel1.Width - label14.Width) / 2, 92);
/*
label6.Font = new Font("宋体", 12);
label8.Font = new Font("宋体", 12);
label9.Font = new Font("宋体", 12);
label11.Font = new Font("宋体", 12);
label12.Font = new Font("宋体", 12);
label13.Font = new Font("宋体", 12);
label14.Font = new Font("宋体", 12);
label10.Font = new Font("宋体", 26, FontStyle.Bold);
*/
PaintEventArgs pe = new PaintEventArgs(panel1.CreateGraphics(), panel1.ClientRectangle);
DrawLine(pe);
}
public void DrawLine(PaintEventArgs e)
{
/*
//画边框
Rectangle rc = e.ClipRectangle;
rc.Width = rc.Width - 1;
rc.Height = rc.Height - 1;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rc);
*/
barcode.width = Convert.ToSingle(Mwidth.Text);
Graphics gh = e.Graphics;
if (dataToEncode.Text != "")
{
inputString = barcode.getCodeB(dataToEncode.Text);
//drawCodePic();
//把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 70), new Size(411, 38)));
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 10;//这里是距离left
//这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = 0;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / 2;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 70, x, 90);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 70, x, 90);
x = x + barcode.width;
}
}
}
}
if (textBox1.Text != "")
{
inputString = barcode.getCodeB(textBox1.Text);
//drawCodePic2();
//把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(200, 0), new Size(350, 50)));
e.Graphics.DrawString("P/N :" + "102401", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 12, 8);
e.Graphics.DrawString("单位:" + "EA", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 12, 26);
e.Graphics.DrawString("库位:" + "CMA110GV", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 12, 44);
e.Graphics.DrawString("6", new Font(new FontFamily("宋体"), 28, FontStyle.Bold), System.Drawing.Brushes.Black, 160, 12);
e.Graphics.DrawString("数量:" + textBox1.Text, new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 196, 8);
e.Graphics.DrawString("保质期:" + "12" + "月", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 196, 50);
//画边框
gh.DrawRectangle(new Pen(new SolidBrush(Color.Black)), 306, 6, 90, 56);
e.Graphics.DrawString("CML", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 340, 8);
e.Graphics.DrawString("简单检查合格", new Font(new FontFamily("宋体"), 10), System.Drawing.Brushes.Black, 310, 28);
e.Graphics.DrawString("IQC01", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 330, 44);
e.Graphics.DrawString(dataToEncode.Text, new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, (panel1.Width - dataToEncode.Text.Length * 8) / 2, 92);
e.Graphics.DrawString("描述:" + "LAMINATIONJIS C2552-50A800", new Font(new FontFamily("宋体"), 12), System.Drawing.Brushes.Black, 12, 110);
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 200;//这里是距离left
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 26, x, 46);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 26, x, 46);
x = x + barcode.width;
}
}
}
gh.Dispose();
}
}
//预览
private void button3_Click(object sender, EventArgs e)
{
//保存为图片
Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(AppDomain.CurrentDomain.BaseDirectory + "1.png", System.Drawing.Imaging.ImageFormat.Jpeg);
//预览
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.WindowState = FormWindowState.Maximized;
this.printPreviewDialog1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
PaintEventArgs pe = new PaintEventArgs(panel1.CreateGraphics(), panel1.ClientRectangle);
DrawLine(pe);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawLine(e);
/*
//其他什么地方都不写,就写这里也可以
//画边框
Rectangle rc = e.ClipRectangle;
rc.Width = rc.Width - 1;
rc.Height = rc.Height - 1;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), rc);
label11.Text = "数量:" + textBox1.Text;
barcode.width = Convert.ToSingle(Mwidth.Text);
Graphics gh = e.Graphics;
if (dataToEncode.Text != "")
{
inputString = barcode.getCodeB(dataToEncode.Text);
//drawCodePic();
//把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 70), new Size(411, 38)));
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 10;//这里是距离left
//这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = 0;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / 2;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 70, x, 90);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 70, x, 90);
x = x + barcode.width;
}
}
}
}
if (textBox1.Text != "")
{
inputString = barcode.getCodeB(textBox1.Text);
//drawCodePic2();
//把之前显示的用白色覆盖掉
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 26), new Size(411, 38)));
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
inputString = inputString.Trim();
float x = 214;//这里是距离left
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
gh.DrawLine(p, x, 26, x, 46);
x = x + barcode.width;
}
else
{
gh.DrawLine(p1, x, 26, x, 46);
x = x + barcode.width;
}
}
}
gh.Dispose();
}
*/
}
public void drawCodePic2()
{
//把之前显示的用白色覆盖掉
Graphics gh = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 26), new Size(411, 38)));
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
Graphics graphics = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
inputString = inputString.Trim();
float x = 214;//这里是距离left
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
graphics.DrawLine(p, x, 26, x, 46);
x = x + barcode.width;
}
else
{
graphics.DrawLine(p1, x, 26, x, 46);
x = x + barcode.width;
}
}
}
graphics.Dispose();
}
public void drawCodePic()
{
//把之前显示的用白色覆盖掉
Graphics gh = panel1.CreateGraphics();//这种写法条码预览或打印都出不来,NND
//gh.DrawRectangle(new Pen(Color.Black), new Rectangle(new Point(0, 0), new Size(413, 40)));
gh.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(1, 70), new Size(411, 38)));
Brush br = new SolidBrush(Color.Black);
Pen p = new Pen(br, barcode.width);
Brush br1 = new SolidBrush(Color.White);
Pen p1 = new Pen(br1, barcode.width);
Graphics graphics = panel1.CreateGraphics();
inputString = inputString.Trim();
float x = 10;//这里是距离left
//这里想把条码置中,已经画好了,怎么移动位置呢?
//先计算条码的width
float w = 0;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
w = w + barcode.width;
}
else
{
w = w + barcode.width;
}
}
}
x = (panel1.Width - (int)w) / 2;
for (int i = 0; i < inputString.Length; i++)
{
for (int j = 0; j < Convert.ToInt32(inputString[i].ToString()); j++)
{
if (i % 2 == 0)
{
//这里的2是距离top,30是条码高
graphics.DrawLine(p, x, 70, x, 90);
x = x + barcode.width;
}
else
{
graphics.DrawLine(p1, x, 70, x, 90);
x = x + barcode.width;
}
}
}
graphics.Dispose();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//打印内容 为panel1
Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);
}
private void button2_Click(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
//打印次数
printDocument1.PrinterSettings.Copies = 1;// Convert.ToInt16(txt_print.Text.Trim());
this.printDocument1.Print();
/*
//换成多线程方式
if (thre == null)
{
thre = new Thread(this.printDocument1.Print);
thre.Start();
}
if (thre.ThreadState == ThreadState.Stopped)
{
thre.Abort();
GC.Collect();
thre = new Thread(this.printDocument1.Print);
thre.Start();
}
*/
}
private void button4_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.ShowDialog();
}
}
}