📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Drawing.Imaging;
using Bokai.Barcodes;
namespace Bokai.Barcodes.Sample1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button ButtonPrint;
private System.Windows.Forms.Button ButtonSaveImage;
private System.Windows.Forms.ListBox ListBoxBarcodeType;
private System.Windows.Forms.ComboBox ComboBoxOrientation;
private Bokai.Barcodes.BarcodeControl barcodeControl1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// corresponding BarcodeType value is the corresponding array index + 1
string[] barcodeTypes = {
"EAN-13",
"EAN-8",
"UPC-A",
"Code 39 Check",
"Codabar",
"Code 39",
"Code 2 of 5",
"Interleaved 2 of 5",
"UPC-E",
"EAN-13 + 2",
"EAN-13 + 5",
"EAN-8 + 2",
"EAN-8 + 5",
"UPC-A + 2",
"UPC-A + 5",
"UPC-E + 2",
"UPC-E + 5",
"EAN-128 A",
"EAN-128 B",
"EAN-128 C",
"Code 93",
"POSTNET",
"Code-128 A",
"Code-128 B",
"Code-128 C"
};
ListBoxBarcodeType.Items.AddRange(barcodeTypes);
ListBoxBarcodeType.SelectedIndex = 5; // index of "Code 39" above
// item index corresponds to actual numeric value of BarcodeOrientation
ComboBoxOrientation.Items.Add("BottomFacing");
ComboBoxOrientation.SelectedIndex = 0;
ComboBoxOrientation.Items.Add("RightFacing");
ComboBoxOrientation.Items.Add("TopFacing");
ComboBoxOrientation.Items.Add("LeftFacing");
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ListBoxBarcodeType = new System.Windows.Forms.ListBox();
this.barcodeControl1 = new Bokai.Barcodes.BarcodeControl();
this.ComboBoxOrientation = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.ButtonPrint = new System.Windows.Forms.Button();
this.ButtonSaveImage = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// ListBoxBarcodeType
//
this.ListBoxBarcodeType.Location = new System.Drawing.Point(304, 16);
this.ListBoxBarcodeType.Name = "ListBoxBarcodeType";
this.ListBoxBarcodeType.Size = new System.Drawing.Size(168, 329);
this.ListBoxBarcodeType.TabIndex = 0;
this.ListBoxBarcodeType.SelectedIndexChanged += new System.EventHandler(this.ListBoxBarcodeType_SelectedIndexChanged);
//
// barcodeControl1
//
this.barcodeControl1.AddOnCaption = null;
this.barcodeControl1.AddOnData = null;
this.barcodeControl1.BackColor = System.Drawing.Color.White;
this.barcodeControl1.Caption = null;
this.barcodeControl1.Data = "000000";
this.barcodeControl1.Font = new System.Drawing.Font("Arial", 9F);
this.barcodeControl1.ForeColor = System.Drawing.Color.Black;
this.barcodeControl1.Location = new System.Drawing.Point(32, 88);
this.barcodeControl1.LowerTopTextBy = 0F;
this.barcodeControl1.Name = "barcodeControl1";
this.barcodeControl1.RaiseBottomTextBy = 0F;
this.barcodeControl1.Size = new System.Drawing.Size(224, 112);
this.barcodeControl1.TabIndex = 1;
this.barcodeControl1.Visible = false;
//
// ComboBoxOrientation
//
this.ComboBoxOrientation.Location = new System.Drawing.Point(120, 288);
this.ComboBoxOrientation.Name = "ComboBoxOrientation";
this.ComboBoxOrientation.Size = new System.Drawing.Size(128, 21);
this.ComboBoxOrientation.TabIndex = 2;
this.ComboBoxOrientation.SelectedIndexChanged += new System.EventHandler(this.ComboBoxOrientation_SelectedIndexChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 288);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 3;
this.label1.Text = "Orientation:";
//
// ButtonPrint
//
this.ButtonPrint.Location = new System.Drawing.Point(24, 320);
this.ButtonPrint.Name = "ButtonPrint";
this.ButtonPrint.TabIndex = 4;
this.ButtonPrint.Text = "Print...";
this.ButtonPrint.Click += new System.EventHandler(this.ButtonPrint_Click);
//
// ButtonSaveImage
//
this.ButtonSaveImage.Location = new System.Drawing.Point(160, 320);
this.ButtonSaveImage.Name = "ButtonSaveImage";
this.ButtonSaveImage.Size = new System.Drawing.Size(88, 23);
this.ButtonSaveImage.TabIndex = 5;
this.ButtonSaveImage.Text = "Save Image...";
this.ButtonSaveImage.Click += new System.EventHandler(this.ButtonSaveImage_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(488, 357);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.ButtonSaveImage,
this.ButtonPrint,
this.label1,
this.ComboBoxOrientation,
this.barcodeControl1,
this.ListBoxBarcodeType});
this.Name = "Form1";
this.Text = "Barcode .Net Sample1 - www.bokai.com";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
// in units of 0.01 inch ("LOENGLISH")
private RectangleF GetDrawingArea()
{
RectangleF rect;
switch (barcodeControl1.Orientation)
{
case BarcodeOrientation.LeftFacing:
case BarcodeOrientation.RightFacing:
rect = new RectangleF(80.0f, 40.0f, 60.0f, 180.0f);
break;
case BarcodeOrientation.BottomFacing:
case BarcodeOrientation.TopFacing:
default:
rect = new RectangleF(40.0f, 80.0f, 180.0f, 60.0f);
break;
}
return rect;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// unit of measure is LOENGLISH or 0.01 inch:
barcodeControl1.Draw(e.Graphics, GetDrawingArea(), GraphicsUnit.Inch, 0.01f, BarcodeDrawFlags.PaintWholeRect, null);
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
// unit of measure is LOENGLISH or 0.01 inch:
barcodeControl1.Draw(e.Graphics, GetDrawingArea(), GraphicsUnit.Inch, 0.01f, 0, null);
e.HasMorePages = false ;
}
private void ButtonPrint_Click(object sender, System.EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog dlg = new PrintDialog() ;
dlg.Document = pd;
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
pd.Print();
}
}
private void ButtonSaveImage_Click(object sender, System.EventArgs e)
{
System.IO.Stream myStream;
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.Filter = "GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP files (*.bmp;*.dib)|*.bmp;*.dib|TIFF files (*.tif)|*.tif";
fileDialog.FilterIndex = 1;
fileDialog.FileName = "sample1";
fileDialog.RestoreDirectory = false;
ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff };
if(fileDialog.ShowDialog() == DialogResult.OK)
{
if((myStream = fileDialog.OpenFile())!= null)
{
int barcodeWidth = 1;
int barcodeHeight = 50;
barcodeControl1.MakeImage(afmt[fileDialog.FilterIndex - 1], barcodeWidth, barcodeHeight, true, false, null, myStream);
myStream.Close();
}
}
}
private void ListBoxBarcodeType_SelectedIndexChanged(object sender, System.EventArgs e)
{
barcodeControl1.BarcodeType = (BarcodeType) (ListBoxBarcodeType.SelectedIndex + 1);
// for demo purposes, use default data consisting of all 0s
barcodeControl1.UseDefaultData();
barcodeControl1.UseDefaultAddOnData();
this.Refresh();
}
private void ComboBoxOrientation_SelectedIndexChanged(object sender, System.EventArgs e)
{
barcodeControl1.Orientation = (BarcodeOrientation) (ComboBoxOrientation.SelectedIndex);
this.Refresh();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -