C#打印条型码生成源程序[转贴]

来源:互联网 发布:360软件管家贴吧 编辑:程序博客网 时间:2024/05/24 00:52

第一次在这里写东西,是想在这里认识更多的朋友,能与高手们交流,呵呵。。这个程序是早此时候要用到条形码时到处找资料,受别人启发,自己尝试开发的简单测试程序,希望对有需要的朋友有所帮助,其实本人也算是C#的初学者,最近刚用它为公司开发了一个数据库应用程序.呵呵..题外话就不多说了.下面就将代码贴出来,自己看去吧.谢谢大家,谢多多支持

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace barcode
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  Bitmap memoryImage ;  
  [DllImport("gdi32.dll")]
  public static extern long  BitBlt(IntPtr hdcDest, int NXDestt,int nYDest,int nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,System.Int32 dwRop);
  private System.Windows.Forms.LinkLabel linkLabel1;
  private System.Windows.Forms.GroupBox setbarcode;
  private System.Windows.Forms.TextBox barcodewide;
  private System.Windows.Forms.TextBox barcodeleng;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.ComboBox to;
  private System.Windows.Forms.ComboBox line;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.ComboBox stand2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.ComboBox stand;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  private System.Drawing.Printing.PrintDocument printDocument1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button savebarcode;
  private System.Windows.Forms.Button printbarcode;
  private AxBARCODELib.AxBarCodeCtrl BarCode1;
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.linkLabel1 = new System.Windows.Forms.LinkLabel();
   this.setbarcode = new System.Windows.Forms.GroupBox();
   this.barcodewide = new System.Windows.Forms.TextBox();
   this.barcodeleng = new System.Windows.Forms.TextBox();
   this.label8 = new System.Windows.Forms.Label();
   this.label7 = new System.Windows.Forms.Label();
   this.to = new System.Windows.Forms.ComboBox();
   this.line = new System.Windows.Forms.ComboBox();
   this.label5 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.stand2 = new System.Windows.Forms.ComboBox();
   this.label3 = new System.Windows.Forms.Label();
   this.stand = new System.Windows.Forms.ComboBox();
   this.label1 = new System.Windows.Forms.Label();
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
   this.printDocument1 = new System.Drawing.Printing.PrintDocument();
   this.label2 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.savebarcode = new System.Windows.Forms.Button();
   this.printbarcode = new System.Windows.Forms.Button();
   this.BarCode1 = new AxBARCODELib.AxBarCodeCtrl();
   this.setbarcode.SuspendLayout();
   ((System.ComponentModel.ISupportInitialize)(this.BarCode1)).BeginInit();
   this.SuspendLayout();
   //
   // linkLabel1
   //
   this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(8, 16);
   this.linkLabel1.Location = new System.Drawing.Point(24, 280);
   this.linkLabel1.Name = "linkLabel1";
   this.linkLabel1.Size = new System.Drawing.Size(252, 27);
   this.linkLabel1.TabIndex = 26;
   this.linkLabel1.TabStop = true;
   this.linkLabel1.Text = "程序设计:陈凌 Fireling@126.com";
   //
   // setbarcode
   //
   this.setbarcode.Controls.Add(this.barcodewide);
   this.setbarcode.Controls.Add(this.barcodeleng);
   this.setbarcode.Controls.Add(this.label8);
   this.setbarcode.Controls.Add(this.label7);
   this.setbarcode.Controls.Add(this.to);
   this.setbarcode.Controls.Add(this.line);
   this.setbarcode.Controls.Add(this.label5);
   this.setbarcode.Controls.Add(this.label4);
   this.setbarcode.Controls.Add(this.stand2);
   this.setbarcode.Controls.Add(this.label3);
   this.setbarcode.Controls.Add(this.stand);
   this.setbarcode.Controls.Add(this.label1);
   this.setbarcode.Location = new System.Drawing.Point(249, 8);
   this.setbarcode.Name = "setbarcode";
   this.setbarcode.Size = new System.Drawing.Size(272, 224);
   this.setbarcode.TabIndex = 25;
   this.setbarcode.TabStop = false;
   this.setbarcode.Text = "设置条形码";
   //
   // barcodewide
   //
   this.barcodewide.Location = new System.Drawing.Point(168, 176);
   this.barcodewide.Name = "barcodewide";
   this.barcodewide.Size = new System.Drawing.Size(74, 21);
   this.barcodewide.TabIndex = 18;
   this.barcodewide.Text = "";
   this.barcodewide.TextChanged += new System.EventHandler(this.barcodewide_TextChanged);
   //
   // barcodeleng
   //
   this.barcodeleng.Location = new System.Drawing.Point(80, 176);
   this.barcodeleng.Name = "barcodeleng";
   this.barcodeleng.Size = new System.Drawing.Size(56, 21);
   this.barcodeleng.TabIndex = 17;
   this.barcodeleng.Text = "";
   this.barcodeleng.TextChanged += new System.EventHandler(this.barcodeleng_TextChanged);
   //
   // label8
   //
   this.label8.AutoSize = true;
   this.label8.Location = new System.Drawing.Point(144, 176);
   this.label8.Name = "label8";
   this.label8.Size = new System.Drawing.Size(11, 17);
   this.label8.TabIndex = 16;
   this.label8.Text = "X";
   //
   // label7
   //
   this.label7.AutoSize = true;
   this.label7.Location = new System.Drawing.Point(24, 176);
   this.label7.Name = "label7";
   this.label7.Size = new System.Drawing.Size(35, 17);
   this.label7.TabIndex = 15;
   this.label7.Text = "长度:";
   //
   // to
   //
   this.to.Location = new System.Drawing.Point(112, 136);
   this.to.Name = "to";
   this.to.Size = new System.Drawing.Size(141, 20);
   this.to.TabIndex = 14;
   this.to.SelectedIndexChanged += new System.EventHandler(this.to_SelectedIndexChanged);
   //
   // line
   //
   this.line.Location = new System.Drawing.Point(112, 104);
   this.line.Name = "line";
   this.line.Size = new System.Drawing.Size(141, 20);
   this.line.TabIndex = 13;
   this.line.SelectedIndexChanged += new System.EventHandler(this.line_SelectedIndexChanged);
   //
   // label5
   //
   this.label5.AutoSize = true;
   this.label5.Location = new System.Drawing.Point(24, 136);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(35, 17);
   this.label5.TabIndex = 12;
   this.label5.Text = "方向:";
   //
   // label4
   //
   this.label4.AutoSize = true;
   this.label4.Location = new System.Drawing.Point(24, 104);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(42, 17);
   this.label4.TabIndex = 11;
   this.label4.Text = "线条:";
   //
   // stand2
   //
   this.stand2.Location = new System.Drawing.Point(112, 72);
   this.stand2.Name = "stand2";
   this.stand2.Size = new System.Drawing.Size(141, 20);
   this.stand2.TabIndex = 10;
   this.stand2.SelectedIndexChanged += new System.EventHandler(this.stand2_SelectedIndexChanged);
   //
   // label3
   //
   this.label3.AutoSize = true;
   this.label3.Location = new System.Drawing.Point(24, 72);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(42, 17);
   this.label3.TabIndex = 9;
   this.label3.Text = "样式:";
   //
   // stand
   //
   this.stand.Location = new System.Drawing.Point(112, 37);
   this.stand.Name = "stand";
   this.stand.Size = new System.Drawing.Size(140, 20);
   this.stand.TabIndex = 1;
   this.stand.SelectedIndexChanged += new System.EventHandler(this.stand_SelectedIndexChanged);
   //
   // label1
   //
   this.label1.AutoSize = true;
   this.label1.Location = new System.Drawing.Point(24, 37);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(42, 17);
   this.label1.TabIndex = 0;
   this.label1.Text = "标准:";
   //
   // groupBox2
   //
   this.groupBox2.Location = new System.Drawing.Point(-23, 256);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(550, 9);
   this.groupBox2.TabIndex = 24;
   this.groupBox2.TabStop = false;
   //
   // saveFileDialog1
   //
   this.saveFileDialog1.DefaultExt = "BMP";
   this.saveFileDialog1.FileName = "我的条形码";
   this.saveFileDialog1.Filter = "位图文件|*.BMP";
   this.saveFileDialog1.Title = "保存条形码";
   //
   // printDocument1
   //
   this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
   //
   // label2
   //
   this.label2.AutoSize = true;
   this.label2.Location = new System.Drawing.Point(16, 208);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(54, 17);
   this.label2.TabIndex = 23;
   this.label2.Text = "条形码值";
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(88, 208);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(144, 21);
   this.textBox1.TabIndex = 22;
   this.textBox1.Text = "";
   this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
   //
   // savebarcode
   //
   this.savebarcode.Location = new System.Drawing.Point(321, 280);
   this.savebarcode.Name = "savebarcode";
   this.savebarcode.Size = new System.Drawing.Size(87, 27);
   this.savebarcode.TabIndex = 20;
   this.savebarcode.Text = "保存条码";
   this.savebarcode.Click += new System.EventHandler(this.savebarcode_Click);
   //
   // printbarcode
   //
   this.printbarcode.Location = new System.Drawing.Point(425, 280);
   this.printbarcode.Name = "printbarcode";
   this.printbarcode.Size = new System.Drawing.Size(87, 27);
   this.printbarcode.TabIndex = 21;
   this.printbarcode.Text = "打印条码";
   this.printbarcode.Click += new System.EventHandler(this.printbarcode_Click);
   //
   // BarCode1
   //
   this.BarCode1.Enabled = true;
   this.BarCode1.Location = new System.Drawing.Point(32, 24);
   this.BarCode1.Name = "BarCode1";
   this.BarCode1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("BarCode1.OcxState")));
   this.BarCode1.Size = new System.Drawing.Size(200, 144);
   this.BarCode1.TabIndex = 27;
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(544, 326);
   this.Controls.Add(this.BarCode1);
   this.Controls.Add(this.groupBox2);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.savebarcode);
   this.Controls.Add(this.printbarcode);
   this.Controls.Add(this.linkLabel1);
   this.Controls.Add(this.setbarcode);
   this.MaximizeBox = false;
   this.MinimizeBox = false;
   this.Name = "Form1";
   this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
   this.Text = "条形码生成器";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.setbarcode.ResumeLayout(false);
   ((System.ComponentModel.ISupportInitialize)(this.BarCode1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
   BarCode1.Value ="200409011649";
   //标准
   this.stand.Items.Add ("UPC-A");
   this.stand.Items.Add( "UPC-E");
   this.stand.Items.Add("EAN-13");
   this.stand.Items.Add("EAN-8");
   this.stand.Items.Add("Case Code");
   this.stand.Items.Add("Codabar (NW-T)");
   this.stand.Items.Add("Code-39");
   this.stand.Items.Add("Code-128");
   this.stand.Items.Add("U.S. Postnet");
   this.stand.Items.Add("U.S. Postal FIM");
   this.stand.Items.Add("JP Post");
   //样式
   this.stand2.Items.Add("Standard");
   this.stand2.Items.Add("2-Digit Supplement");
   this.stand2.Items.Add("5-Digit Supplement");
   this.stand2.Items.Add("POS Case Code");
   //线条
   this.line.Items.Add ("细");
   this.line.Items.Add ("特别淡");
   this.line.Items.Add ("淡");
   this.line.Items.Add ("普通");
   this.line.Items.Add ("中等");
   this.line.Items.Add ("粗");
   this.line.Items.Add ("浓");
   this.line.Items.Add ("特浓");
   //方向
   this.to.Items.Add ("0度");
   this.to.Items.Add ("90度");
   this.to.Items.Add ("180度");
   this.to.Items.Add ("270度");
   this.barcodewide .Text = BarCode1.Width.ToString ();
   this.barcodeleng.Text = BarCode1.Height .ToString ();
  }
  private void CaptureScreen()
  {
   Graphics mygraphics = BarCode1.CreateGraphics() ;
   this.memoryImage = new Bitmap (BarCode1.Width ,BarCode1.Height ,mygraphics);
   Graphics memoryGraphics =Graphics.FromImage (memoryImage);
   IntPtr dc1,dc2;
   dc1= mygraphics.GetHdc() ;
   dc2 =memoryGraphics.GetHdc ();
   BitBlt(dc2,0,0,BarCode1.Width,BarCode1.Height ,dc1,0,0,13369376);
   mygraphics.ReleaseHdc (dc1);
   memoryGraphics.ReleaseHdc (dc2);

  }
  private void savebarcode_Click(object sender, System.EventArgs e)
  {
   CaptureScreen();
   DialogResult saveornot = saveFileDialog1.ShowDialog ();
   if (saveornot ==DialogResult.OK )
   {
    memoryImage.Save(saveFileDialog1.FileName , System.Drawing.Imaging.ImageFormat.Bmp);

   }
   memoryImage.Save("c://temp//barcode1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
  }

  private void printbarcode_Click(object sender, System.EventArgs e)
  {
   this.CaptureScreen  ();
   printDocument1.Print ();
  }


  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
   Point ulCorner = new Point( 100, 100);
   e.Graphics.DrawImage (memoryImage,ulCorner);
  }
  private void stand_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   int i =stand.SelectedIndex ;
   short ii = short.Parse (i.ToString ());
   BarCode1.Style = ii;
  }

  private void stand2_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   int i = stand2.SelectedIndex ;
   short ii=short.Parse (i.ToString ());
   BarCode1.SubStyle = ii;

  }

  private void line_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   int i =stand.SelectedIndex ;
   short ii = short.Parse (i.ToString ());
   BarCode1.LineWeight  = ii;
  }

  private void to_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   int i =stand.SelectedIndex ;
   short ii = short.Parse (i.ToString ());
   BarCode1.Direction  = ii;
  }

  private void barcodeleng_TextChanged(object sender, System.EventArgs e)
  {
   if (this.barcodeleng.Text !="")
   {
    BarCode1.Width =int.Parse (this.barcodeleng.Text .Trim ());
      
   }
  }

  private void barcodewide_TextChanged(object sender, System.EventArgs e)
  {
   if (this.barcodeleng.Text !="")
   {
    BarCode1.Height  =int.Parse (this.barcodewide .Text .Trim ());
   }
  }

  private void textBox1_TextChanged(object sender, System.EventArgs e)
  {
   this.BarCode1.Value = this.textBox1.Text.Trim () ;
  }

  
 }
}
此程序在vs.net2003 +winxp下测试通过.

联系 QQ 45984781  E-mail Fireling@126.com

原文出自:http://blog.csdn.net/fireling/archive/2005/03/18/322965.aspx

原创粉丝点击