📄 form1.cs
字号:
this.checkHiliteAddOn.CheckedChanged += new System.EventHandler(this.checkHilite_CheckedChanged);
//
// checkHiliteWhole
//
this.checkHiliteWhole.Location = new System.Drawing.Point(368, 384);
this.checkHiliteWhole.Name = "checkHiliteWhole";
this.checkHiliteWhole.Size = new System.Drawing.Size(56, 16);
this.checkHiliteWhole.TabIndex = 18;
this.checkHiliteWhole.Text = "Whole";
this.checkHiliteWhole.CheckedChanged += new System.EventHandler(this.checkHilite_CheckedChanged);
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 384);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(112, 16);
this.label6.TabIndex = 19;
this.label6.Text = "Highlight Rectangles:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(488, 405);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label6,
this.checkHiliteWhole,
this.checkHiliteAddOn,
this.checkHiliteMain,
this.editFont,
this.linkFont,
this.comboRotation,
this.label5,
this.editAddOnData,
this.editData,
this.label4,
this.label3,
this.comboBarcodeType,
this.label2,
this.buttonSaveImage,
this.buttonPrint,
this.label1,
this.comboOrientation,
this.barcodeControl1,
this.listboxProperties});
this.Name = "Form1";
this.Text = "Barcode .Net Sample2 - 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);
if (comboRotation.SelectedIndex != 0)
CustomPaint(e.Graphics, true);
else
SimplePaint(e.Graphics, true);
}
private void SimplePaint(Graphics g, bool onScreen)
{
// unit of measure is LOENGLISH, i.e., 0.01 inch:
RectangleF rect = GetDrawingArea();
// draw a blue reference rectangle on screen:
if (onScreen)
{
g.PageUnit = GraphicsUnit.Inch;
g.PageScale = 0.01f;
g.DrawRectangle(Pens.Blue, rect.X, rect.Y, rect.Width, rect.Height);
}
// draw the barcode:
// (In case of rotation, BarcodeDrawFlags.NoRestoreGraphicsState will cause to
// preserve transformation on Graphics so that we can HiliteRectangles)
barcodeControl1.Draw(g, rect, GraphicsUnit.Inch, 0.01f,
barcodeControl1.Orientation, barcodeControl1.Font, barcodeControl1.ForeColor,
barcodeControl1.BackColor, _drawFlags | BarcodeDrawFlags.NoRestoreGraphicsState, null);
// The above is the most sophiscated version of Draw(), where we pass in DrawFlags that
// override corresponding properties. Alternatively, we could set properties
// on barcodeControl1 and then call the below simplified version of Draw():
// barcodeControl1.Draw(g, rect, GraphicsUnit.Inch, 0.01f, _drawFlags, null);
if (onScreen) HiliteRectangles(g);
}
private void CustomPaint(Graphics g, bool onScreen)
{
// unit of measure is LOENGLISH, i.e., 0.01 inch:
RectangleF rect = GetDrawingArea();
g.PageUnit = GraphicsUnit.Inch;
g.PageScale = 0.01f;
float rotation = Single.Parse((string) comboRotation.SelectedItem);
PointF rotateAt = new PointF(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
System.Drawing.Drawing2D.Matrix matrix = g.Transform;
if (matrix == null) matrix = new System.Drawing.Drawing2D.Matrix();
matrix.RotateAt(rotation, rotateAt);
g.Transform = matrix;
// draw a blue reference rectangle on screen:
if (onScreen)
g.DrawRectangle(Pens.Blue, rect.X, rect.Y, rect.Width, rect.Height);
// draw the barcode (GraphicsUnit.World: use caller's mapping mode):
barcodeControl1.Draw(g, rect, GraphicsUnit.World, 1.0f,
barcodeControl1.Orientation, barcodeControl1.Font, barcodeControl1.ForeColor,
barcodeControl1.BackColor, _drawFlags, null);
// The above is the most sophiscated version of Draw(), where we pass in DrawFlags that
// override corresponding properties. Alternatively, we could set properties
// on barcodeControl1 and then call the below simplified version of Draw():
// barcodeControl1.Draw(g, rect, GraphicsUnit.Inch, 0.01f, _drawFlags, null);
if (onScreen) HiliteRectangles(g);
}
private void HiliteRectangles(Graphics g)
{
if (checkHiliteWhole.Checked) g.DrawRectangle(Pens.Red, barcodeControl1.ActualRectangle.X, barcodeControl1.ActualRectangle.Y, barcodeControl1.ActualRectangle.Width, barcodeControl1.ActualRectangle.Height);
if (checkHiliteMain.Checked) g.DrawRectangle(Pens.Red, barcodeControl1.ActualMainRectangle.X, barcodeControl1.ActualMainRectangle.Y, barcodeControl1.ActualMainRectangle.Width, barcodeControl1.ActualMainRectangle.Height);
if (checkHiliteAddOn.Checked) g.DrawRectangle(Pens.Red, barcodeControl1.ActualAddOnRectangle.X, barcodeControl1.ActualAddOnRectangle.Y, barcodeControl1.ActualAddOnRectangle.Width, barcodeControl1.ActualAddOnRectangle.Height);
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
if (comboRotation.SelectedIndex != 0)
CustomPaint(e.Graphics, false);
else
SimplePaint(e.Graphics, false);
e.HasMorePages = false ;
}
private void comboOrientation_SelectedIndexChanged(object sender, System.EventArgs e)
{
barcodeControl1.Orientation = (BarcodeOrientation) (comboOrientation.SelectedIndex);
this.Refresh();
}
private void linkFont_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
FontDialog fontDialog = new FontDialog();
fontDialog.ShowColor = true;
fontDialog.Font = barcodeControl1.Font;
fontDialog.Color = barcodeControl1.ForeColor;
if (fontDialog.ShowDialog() == DialogResult.OK)
{
barcodeControl1.Font = fontDialog.Font;
barcodeControl1.ForeColor = fontDialog.Color;
editFont.ForeColor = barcodeControl1.ForeColor;
editFont.Text = barcodeControl1.Font.Name;
this.Refresh();
}
}
private void listboxProperties_SelectedIndexChanged(object sender, System.EventArgs e)
{
_drawFlags = 0;
ListBox.SelectedObjectCollection items = listboxProperties.SelectedItems;
for (int i = 0; i < items.Count; i++)
{
BarcodeDrawFlags flag = (BarcodeDrawFlags) items[i];
_drawFlags |= flag;
}
this.Refresh();
}
private void comboBarcodeType_SelectedIndexChanged(object sender, System.EventArgs e)
{
barcodeControl1.BarcodeType = (BarcodeType) (comboBarcodeType.SelectedIndex + 1);
// for demo purposes, use default data consisting of all 0s
barcodeControl1.UseDefaultData();
barcodeControl1.UseDefaultAddOnData();
editData.Text = barcodeControl1.Data;
editAddOnData.Text = barcodeControl1.AddOnData;
if ((barcodeControl1.Characteristics & BarcodeCharacteristics.HasAddOn) != 0)
this.checkHiliteAddOn.Enabled = true;
else
{
this.checkHiliteAddOn.Enabled = false;
this.checkHiliteAddOn.Checked = false;
}
this.Refresh();
}
private void editData_TextChanged(object sender, System.EventArgs e)
{
try
{
barcodeControl1.Data = editData.Text;
}
catch (ArgumentException)
{
}
this.Refresh();
}
private void editAddOnData_TextChanged(object sender, System.EventArgs e)
{
try
{
barcodeControl1.AddOnData = editAddOnData.Text;
}
catch (ArgumentException)
{
}
this.Refresh();
}
private void comboRotation_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Use the Orientation property only if the rotation transform
// is not in effect, in order not to get confused, even though they CAN
// work together
if (comboRotation.SelectedIndex != 0)
{
comboOrientation.SelectedIndex = 0;
comboOrientation.Enabled = false;
}
else
comboOrientation.Enabled = true;
this.Refresh();
}
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 = "sample2";
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 checkHilite_CheckedChanged(object sender, System.EventArgs e)
{
this.Refresh();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -