这种方法是有BUG的~! 还有打印功能同保存功能还没有成功~!请高手帮一帮呀~!
先在窗口上放上两个“textBox”一个"LISTBOX",一个"button"一个“pictrueBOX”。
公共变量:{
int DrawX, DrawY, StartX, StartY; //画线的坐标
string myx,myy;
bool bolPrintText; //是否打印人工标识符
string[] strBarTable= new string[40]; //条码编码格式表
string[] boolstr=new string[40]{"0","1","2","3","4","5","6","7","8","9","10","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","Y","Z","-","%","$","*"};
}
以下是主要实现部分程序:
protected void StartDraw(int X, int Y)//初始化过程 ,x,y 为坐标值
{
StartX = X; //初始化 开始的X,Y坐标
StartY = Y;
bX=StartX;
bY=StartY;
//初始化条码编码格式表
strBarTable[0] = "001100100"; //注释: 0
strBarTable[1] = "100010100"; //注释: 1
strBarTable[2] = "010010100"; //注释: 2
strBarTable[3] = "110000100"; //注释: 3
strBarTable[4] = "001010100"; //注释: 4
strBarTable[5] = "101000100"; //注释: 5
strBarTable[6] = "011000100"; //注释: 6
strBarTable[7] = "000110100"; //注释: 7
strBarTable[8] = "100100100"; //注释: 8
strBarTable[9] = "010100100"; //注释: 9
strBarTable[10] = "100010010"; //注释: A
strBarTable[11] = "010010010"; //注释: B
strBarTable[12] = "110000010"; //注释: C
strBarTable[13] = "001010010"; //注释: D
strBarTable[14] = "101000010"; //注释: E
strBarTable[15] = "011000010"; //注释: F
strBarTable[16] = "000110010"; //注释: G
strBarTable[17] = "100100010"; //注释: H
strBarTable[18] = "010100010"; //注释: I
strBarTable[19] = "001100010"; //注释: J
strBarTable[20] = "100010001"; //注释: K
strBarTable[21] = "010010001"; //注释: L
strBarTable[22] = "110000001"; //注释: M
strBarTable[23] = "001010001"; //注释: N
strBarTable[24] = "101000001"; //注释: O
strBarTable[25] = "011000001"; //注释: P
strBarTable[26] = "000110001"; //注释: Q
strBarTable[27] = "100100001"; //注释: R
strBarTable[28] = "010100001"; //注释: S
strBarTable[29] = "001100001"; //注释: T
strBarTable[30] = "100011000"; //注释: U
strBarTable[31] = "010011000"; //注释: V
strBarTable[32] = "110001000"; //注释: W
strBarTable[33] = "001011000"; //注释: X
strBarTable[34] = "101001000"; //注释: Y
strBarTable[35] = "011001000"; //注释: Z
strBarTable[36] = "000111000"; //注释: -
strBarTable[37] = "100101000"; //注释: %
strBarTable[38] = "010101000"; //注释: $
strBarTable[39] = "001101000"; //注释: *
}
public void DrawBlackLine(int i)//画黑线 ,i为参数,i=1画细线,i=3画粗线
{
int j=0;//j 用来循环3次画粗线
Graphics mygar=this.pictureBox1.CreateGraphics();
Pen myPen1=new Pen(Color.Black);
switch(i)
{
case 1:
mygar.DrawLine(myPen1,StartX, StartY,StartX,StartY + DrawY);//画线
StartX = StartX + 1; //X 向右移动 1个单位
break;
case 3:
for (j=0;j<=2;j++)
{
mygar.DrawLine(myPen1,StartX, StartY,StartX,StartY + DrawY);//画线
StartX = StartX + 1; //X 向右移动 1个单位
}
break;
}
///
/// 画白线 ,i为参数,i=1画细线,i=3画粗线
///
protected void DrawWhiteLine(int i)
{
int j=0;
switch(i)
{
case 1:
StartX = StartX + 1; //X 向右移动 1个单位
break;
case 3:
for (j=0;j<=2;j++)
{
StartX = StartX + 1; //X 向右移动 1个单位
}
break;
}
}
}
///
/// 根据传入的数值画条形码 ,(编码编号,字符,是否打印字符)
///
protected void DrawStrBar(int Index, string str, bool printtext)//根据传入的数值画条形码
{
int l, m;
bool isblack; //控制交互打印黑白线
int tmpX, tmpY; //临时变量,存放X,Y坐标,保护现场
isblack=true; //初始化为True ,先打黑线
Font hfontFont=new Font("黑体",9);
Graphics graphics=this.pictureBox1.CreateGraphics();
for(l=0;l<=4;l++)
{
for(m=0;m<=1;m++)
{
if (isblack==true) //isblack 为真 打黑线
{
if(l==2) //在第3条线下面添加字符
{
if (printtext==true) //如果为真,打印人工识别标记
{
tmpX = StartX; // 保护现场 :)
tmpY = StartY + DrawY+5; // 保护现场 :)
graphics.DrawString(str,hfontFont,Brushes.Black,tmpX, tmpY);
}
}
if (strBarTable[Index].Substring(l,1)=="0") //为'0'打细黑线
{
DrawBlackLine(1); //细黑线
}
else
{
DrawBlackLine(3); //否则粗黑线
}
isblack = false; //打完一条黑线,置为false ,开始打白线
}
else
{
if (l == 4) //判断l是否为4, 成立打印 粗白线
{
DrawWhiteLine(3); //l=4时粗白线为 下一个字符的间隔
}
else
{
if (strBarTable[Index].Substring( l+5, 1) == "0") //为'0'打细白线
{
DrawWhiteLine(1); //细白线
}
else
{
DrawWhiteLine(3); //否则粗白线
}
isblack = true; //打完一条白线,置为True ,开始打黑线
}
}
}
}
}
//应用
private void button1_Click(object sender, System.EventArgs e)
{
int i, IntIndex,k;
string strBC,tmpstr;
// string aa;
this.listBox1.Items.Clear();
Graphics gra4=this.pictureBox1.CreateGraphics();
gra4.Clear(Color.White);
StartDraw(10, 10);
myx =textBox1.Text.Trim(); //开始画点的X坐标
myy =textBox2.Text.Trim(); //条形码高度
DrawX=int.Parse(myx); //开始画点的X坐标
DrawY = int.Parse(myy); //条形码高度
bolPrintText = this.checkBox1.Checked;
strBC = this.textBox3.Text.ToUpper();
strBC="*"+strBC+"*";
strBC=strBC.Trim();
i=0;
// MessageBox.Show(tmpstr);
if (strBC != "")
{
for (i=0;i
//在字符数组中的位置
IntIndex=-1;
tmpstr=strBC.Substring(i,1);
for(k=0;k < boolstr.Length;k++)
{
if(tmpstr==boolstr[k])
{
IntIndex=k;
// aa=IntIndex.ToString();
// this.listBox1.Items.Add(aa+"/n");
break;
}
}
if(IntIndex == -1)
{
MessageBox.Show("要打印的条形码字符串中包含:"+tmpstr+" 的无效字符!!","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.listBox1.Items.Clear();
Graphics gra3=this.pictureBox1.CreateGraphics();
gra3.Clear(Color.White);
break;
}
else
{
DrawStrBar(IntIndex, tmpstr, bolPrintText); //调用画条形码过程
this.listBox1.Items.Add(IntIndex.ToString()+"/n");
// this.listBox1.Items.Add(strBarTable[IntIndex]+"/n");
}
}
}
}