1、新建自定义C#控件项目Custom
using System;
using System.Collections.Generic;
using System.Text;
namespace CustomControls
{
public class CommonSettings
{
/// <summary>
/// 把像素换算成毫米
/// </summary>
/// <param name="Pixel">多少像素</param>
/// <returns>多少毫米</returns>
public static float PixelConvertMillimeter(float Pixel)
{
return Pixel / 96 * 25.4f;
}
/// <summary>
/// 把毫米换算成像素
/// </summary>
/// <param name="Millimeter">多少毫米</param>
/// <returns>多少像素</returns>
public static int MillimeterConvertPixel(float Millimeter)
{
return ((int)(Millimeter / 25.4 * 96)+1);
}
}
}
GraphicsTools
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
namespace CustomControls
{
internal static class GraphicsTools
{
/// <summary>
/// Creates a rounded rectangle from the specified rectangle and radius
/// </summary>
/// <param name="rectangle">Base rectangle</param>
/// <param name="radius">Radius of the corners</param>
/// <returns>Rounded rectangle as a GraphicsPath</returns>
public static GraphicsPath CreateRoundRectangle(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rectangle.Left;
int t = rectangle.Top;
int w = rectangle.Width;
int h = rectangle.Height;
int d = radius << 1;
path.AddArc(l, t, d, d, 180, 90); // topleft
path.AddLine(l + radius, t, l + w - radius, t); // top
path.AddArc(l + w - d, t, d, d, 270, 90); // topright
path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
path.AddLine(l, t + h - radius, l, t + radius); // left
path.CloseFigure();
return path;
}
/// <summary>
/// Creates a rectangle rounded on the top
/// </summary>
/// <param name="rectangle">Base rectangle</param>
/// <param name="radius">Radius of the top corners</param>
/// <returns>Rounded rectangle (on top) as a GraphicsPath object</returns>
public static GraphicsPath CreateTopRoundRectangle(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rectangle.Left;
int t = rectangle.Top;
int w = rectangle.Width;
int h = rectangle.Height;
int d = radius << 1;
path.AddArc(l, t, d, d, 180, 90); // topleft
path.AddLine(l + radius, t, l + w - radius, t); // top
path.AddArc(l + w - d, t, d, d, 270, 90); // topright
path.AddLine(l + w, t + radius, l + w, t + h); // right
path.AddLine(l + w, t + h, l, t + h); // bottom
path.AddLine(l, t + h, l, t + radius); // left
path.CloseFigure();
return path;
}
/// <summary>
/// Creates a rectangle rounded on the bottom
/// </summary>
/// <param name="rectangle">Base rectangle</param>
/// <param name="radius">Radius of the bottom corners</param>
/// <returns>Rounded rectangle (on bottom) as a GraphicsPath object</returns>
public static GraphicsPath CreateBottomRoundRectangle(Rectangle rectangle, int radius)
{
GraphicsPath path = new GraphicsPath();
int l = rectangle.Left;
int t = rectangle.Top;
int w = rectangle.Width;
int h = rectangle.Height;
int d = radius << 1;
path.AddLine(l + radius, t, l + w - radius, t); // top
path.AddLine(l + w, t + radius, l + w, t + h - radius); // right
path.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomright
path.AddLine(l + w - radius, t + h, l + radius, t + h); // bottom
path.AddArc(l, t + h - d, d, d, 90, 90); // bottomleft
path.AddLine(l, t + h - radius, l, t + radius); // left
path.CloseFigure();
return path;
}
}
}
2、打印机设置界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using CustomControls.IO;
namespace CustomControls.Control
{
public partial class frmSetting : Form
{
public frmSetting(PrintConfig confg)
{
InitializeComponent();
pconfig = confg;
}
private PrintConfig pconfig;
private void frmSetting_Load(object sender, EventArgs e)
{
for (int i=0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
cbPrintName.Items.Add(PrinterSettings.InstalledPrinters[i]);
}
if (cbPrintName.Items.Count > 0)
{
cbPrintName.SelectedItem = pconfig.PrintName;
}
numX.Value = pconfig.XOFFSET;
numY.Value = pconfig.YOFFSET;
numZoom.Value = (decimal)pconfig.ZOOM;
numCopies.Value = pconfig.Copies;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
pconfig.XOFFSET = (int)numX.Value;
pconfig.YOFFSET = (int)numY.Value;
pconfig.PrintName = cbPrintName.SelectedItem.ToString();
pconfig.ZOOM = (float)numZoom.Value;
pconfig.Copies = (int)numCopies.Value;
this.Close();
}
}
}
3、条形码39码\93码实现
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CustomControls.BarCode
{
[Serializable]
internal class Code39 : IBarcode
{
public Code39()
{
EncodeBarcodeValue();
}
#region Variable
/// <summary>
/// 是否显示条码的值
/// </summary>
private bool bShowText = true;
/// <summary>
/// 是否在条码上方显示字符
/// </summary>
private bool bShowTextOnTop = false;
/// <summary>
/// 条码值的对值的对齐方式
/// </summary>
private BarcodeTextAlign align = BarcodeTextAlign.Left;
/// <summary>
/// 条码的做图区域
/// </summary>
private Rectangle barcodeRect;
/// <summary>
/// 条码的值
/// </summary>
private String code = "0123456";
/// <summary>
/// 条码的高度
/// </summary>
private int height = 30;
/// <summary>
/// 条码的大小
/// </summary>
private BarCodeWeight weight = BarCodeWeight.Small;
/// <summary>
/// 旋转
/// </summary>
private BarcodeRotation Rotation = BarcodeRotation.Rotation90;
/// <summary>
/// 条码的字体
/// </summary>
private Font textFont = new Font("Courier", 8);
/// <summary>
/// 将条码数据编码
/// </summary>
private String encodedString = "";
/// <summary>