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); // topleftpath.AddLine(l + radius, t, l + w - radius, t); // toppath.AddArc(l + w - d, t, d, d, 270, 90); // toprightpath.AddLine(l + w, t + radius, l + w, t + h - radius); // rightpath.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomrightpath.AddLine(l + w - radius, t + h, l + radius, t + h); // bottompath.AddArc(l, t + h - d, d, d, 90, 90); // bottomleftpath.AddLine(l, t + h - radius, l, t + radius); // leftpath.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); // topleftpath.AddLine(l + radius, t, l + w - radius, t); // toppath.AddArc(l + w - d, t, d, d, 270, 90); // toprightpath.AddLine(l + w, t + radius, l + w, t + h); // rightpath.AddLine(l + w, t + h, l, t + h); // bottompath.AddLine(l, t + h, l, t + radius); // leftpath.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); // toppath.AddLine(l + w, t + radius, l + w, t + h - radius); // rightpath.AddArc(l + w - d, t + h - d, d, d, 0, 90); // bottomrightpath.AddLine(l + w - radius, t + h, l + radius, t + h); // bottompath.AddArc(l, t + h - d, d, d, 90, 90); // bottomleftpath.AddLine(l, t + h - radius, l, t + radius); // leftpath.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>/// 条码值的长度/// </summary>private int strLength = 0;/// <summary>/// 条码的比率/// </summary>private int wideToNarrowRatio = 2;#endregion#region Base String ValueString alphabet39 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*";String[] coded39Char = {/* 0 */ "000110100", /* 1 */ "100100001", /* 2 */ "001100001", /* 3 */ "101100000",/* 4 */ "000110001", /* 5 */ "100110000", /* 6 */ "001110000", /* 7 */ "000100101",/* 8 */ "100100100", /* 9 */ "001100100", /* A */ "100001001", /* B */ "001001001",/* C */ "101001000", /* D */ "000011001", /* E */ "100011000", /* F */ "001011000",/* G */ "000001101", /* H */ "100001100", /* I */ "001001100", /* J */ "000011100",/* K */ "100000011", /* L */ "001000011", /* M */ "101000010", /* N */ "000010011",/* O */ "100010010", /* P */ "001010010", /* Q */ "000000111", /* R */ "100000110",/* S */ "001000110", /* T */ "000010110", /* U */ "110000001", /* V */ "011000001",/* W */ "111000000", /* X */ "010010001", /* Y */ "110010000", /* Z */ "011010000",/* - */ "010000101", /* . */ "110000100", /*' '*/ "011000100",/* $ */ "010101000",/* / */ "010100010", /* + */ "010001010", /* % */ "000101010", /* * */ "010010100" };#endregion#region Barcode attribute/// <summary>/// get or set Barcode Height/// </summary>public int BarcodeHeight{get { return height; }set { height = value; }}/// <summary>/// Get or Set Barcode Value/// </summary>public String BarcodeValue{get { return code; }set{code = value.ToUpper();EncodeBarcodeValue();}}/// <summary>/// get or set baroce weight/// </summary>public BarCodeWeight BarcodeWeight{get { return weight; }set { weight = value; }}/// <summary>/// get or set barocde draw range/// </summary>public Rectangle BarcodeRect{get { return barcodeRect; }set { barcodeRect = value; }}/// <summary>/// get or set show value text/// </summary>public bool ShowText{get { return bShowText; }set { bShowText = value; }}/// <summary>/// get or set show value text on barcode top/// </summary>public bool ShowTextOnTop{get { return bShowTextOnTop; }set { bShowTextOnTop = value; }}/// <summary>/// get or set value text font/// </summary>public Font ValueTextFont{get { return textFont; }set { textFont = value; }}/// <summary>/// get or set value text align/// </summary>public BarcodeTextAlign barcodeTextAlign{get { return align; }set { align = value; }}/// <summary>/// get or set barcode rotation/// </summary>public BarcodeRotation barcodeRotation{get { return Rotation; }set { Rotation = value; }}#endregion#region Encode barcode string/// <summary>/// 对条码的值进行编码/// </summary>private void EncodeBarcodeValue(){try{String intercharacterGap = "0";String str = '*' + code.ToUpper() + '*';strLength = str.Length;encodedString = "";for (int i = 0; i < strLength; i++){if (i > 0)encodedString += intercharacterGap;encodedString += coded39Char[alphabet39.IndexOf(str[i])];}}catch{throw new Exception("无法编码!");}}#endregion#region Create Barcodepublic void CreateBarcode(Graphics g){//g.DrawString("Code 39", new Font("Vendana", 9), Brushes.Blue, barcodeRect);// g.SetClip(barcodeRect);GraphicsState state = g.Save();Matrix RotationTransform = new Matrix(1, 0, 0, 1, 1, 1); //rotation matrixPointF TheRotationPoint;switch (Rotation){case BarcodeRotation.Rotation90:TheRotationPoint = new PointF(barcodeRect.X, barcodeRect.Y);RotationTransform.RotateAt(90, TheRotationPoint);g.Transform = RotationTransform;g.TranslateTransform(0, -barcodeRect.Width);break;case BarcodeRotation.Rotation270:TheRotationPoint = new PointF(barcodeRect.X, barcodeRect.Y + barcodeRect.Height);RotationTransform.RotateAt(270, TheRotationPoint);g.Transform = RotationTransform;g.TranslateTransform(0, barcodeRect.Height);break;}bool bchkValueText = false;for (int i = 0; i < code.Length; i++){if (alphabet39.IndexOf(code[i]) == -1 || code[i] == '*'){g.DrawString("条码文本无效!", textFont, Brushes.Red, barcodeRect.X, barcodeRect.Y);bchkValueText = true;break;}}if (!bchkValueText){int encodedStringLength = encodedString.Length;float x = barcodeRect.X;float wid = 0;int yTop = (int)barcodeRect.Y;if (bShowText){using (StringFormat sf = new StringFormat()){ switch (align){case BarcodeTextAlign.Left:sf.Alignment = StringAlignment.Near;break;case BarcodeTextAlign.Center:sf.Alignment = StringAlignment.Center;sf.FormatFlags = StringFormatFlags.FitBlackBox;break;case BarcodeTextAlign.Right:sf.Alignment = StringAlignment.Far;break;}if (bShowTextOnTop){//yTop += height;if (Rotation == BarcodeRotation.Rotation0){g.DrawString(code, textFont, Brushes.Black, new RectangleF(barcodeRect.X, yTop, barcodeRect.Width, g.MeasureString("s", textFont).Height), sf);}else{g.DrawString(code, textFont, Brushes.Black, new RectangleF(barcodeRect.X, yTop, barcodeRect.Height, g.MeasureString("s", textFont).Height), sf);}yTop += Convert.ToInt32(g.MeasureString("s", textFont).Height);}else{yTop += height;if (Rotation == BarcodeRotation.Rotation0){g.DrawString(code, textFont, Brushes.Black, new RectangleF(barcodeRect.X, yTop, barcodeRect.Width, barcodeRect.Height - height), sf);}else{g.DrawString(code, textFont, Brushes.Black, new RectangleF(barcodeRect.X, yTop, barcodeRect.Height, barcodeRect.Width), sf);}yTop -= height;}}}for (int i = 0; i < encodedStringLength; i++){if (encodedString[i] == '1')wid = (wideToNarrowRatio * (int)weight);elsewid = (int)weight;g.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White, x, yTop, wid, height);x += wid;}}g.Restore(state);//g.ResetClip();}#endregion#region Barcode draw rectanglepublic Rectangle GetBarcodeRectangle(){int wid = 0, RectW = 0, RectH = 0;//for (int i = 0; i < code.Length; i++)//{// if (alphabet39.IndexOf(code[i]) == -1 || code[i] == '*')// { // return new RectangleF(0, 0, 100, 100);// }//} for (int i = 0; i < encodedString.Length; i++){if (encodedString[i] == '1')wid = wideToNarrowRatio * (int)weight;elsewid = (int)weight;RectW += wid;}if (bShowText){using (Graphics g = Graphics.FromImage(new Bitmap(10, 10))){RectH = (int)g.MeasureString("S", textFont).Height + height;}}else{RectH = height;}switch (Rotation){case BarcodeRotation.Rotation90:case BarcodeRotation.Rotation270:return new Rectangle(barcodeRect.X, barcodeRect.Y, RectH, RectW);// break;}return new Rectangle(barcodeRect.X, barcodeRect.Y, RectW, RectH);}#endregion}
}
4、设计项目存档代码实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Drawing.Printing;
using CustomControls.IO;
using CustomControls.DrawItem;
using CustomControls.ToolBox;namespace CustomControls.Control
{public partial class DocumentSpace : UserControl{public DocumentSpace(){SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);InitializeComponent();//绑定工具切换事件drawToolBox1.ToolChanged += new EventHandler(drawToolBox1_ToolChanged);//通过属性显示框,显示出所选对像的属性designer1.OnShowProperityInfo += new ShowProperityInfo(designer1_OnShowProperityInfo);//工具变换事件designer1.OnActiveToolChange += new ActiveToolChange(designer1_OnActiveToolChange);//添加或删除item事件designer1.OnItemChange += new ItemChange(designer1_OnItemChange);//添加工具栏按钮drawToolBox1.AddButton();numHeight.Value = (int)CommonSettings.PixelConvertMillimeter(designer1.Height);numWidth.Value = (int)CommonSettings.PixelConvertMillimeter(designer1.Width);}/// <summary>/// 获取design的宽和高/// </summary>private float design_width, design_height;/// <summary>/// 标尺数字的字体/// </summary>private Font scaleLabel = new Font("Verdana", 7);//private Point mouseLocationXs, mouseLocationXe, mouseLocationYs, mouseLocationYe;/// <summary>/// 通过此能数检测是打开还是新建的设计/// 在保存的时候是否可以直接保存/// </summary>private string openFileName = "";/// <summary>/// 画标尺刻度/// </summary>/// <param name="g"></param>private void DrawScale(Graphics g){design_width = designer1.Width;design_height = designer1.Height;int width_mm = (int)CommonSettings.PixelConvertMillimeter(design_width);int heigth_mm = (int)CommonSettings.PixelConvertMillimeter(design_height);float percent = design_width / width_mm;PointF ps, pe;int scale;for (int i = 0; i < width_mm; i++){if (i % 10 == 0){string x = (i / 10).ToString();SizeF fontSize = g.MeasureString(x, scaleLabel);scale = 15;PointF pf = new PointF(designer1.Location.X + i * percent - fontSize.Width / 2, designer1.Location.Y - scale - fontSize.Height);g.DrawString(x, scaleLabel, Brushes.Blue, pf);pf = new PointF(designer1.Location.X + i * percent - fontSize.Width / 2, designer1.Location.Y + scale + designer1.Height);g.DrawString(x, scaleLabel, Brushes.Blue, pf);}else if (i % 5 == 0){scale = 10;}else{scale = 5;}ps = new PointF(designer1.Location.X + i * percent, designer1.Location.Y);pe = new PointF(designer1.Location.X + i * percent, designer1.Location.Y - scale);g.DrawLine(Pens.Blue, ps, pe);ps = new PointF(designer1.Location.X + i * percent, designer1.Location.Y + designer1.Height);pe = new PointF(designer1.Location.X + i * percent, designer1.Location.Y + designer1.Height + scale);g.DrawLine(Pens.Blue, ps, pe);}for (int j = 0; j < heigth_mm; j++){if (j % 10 == 0){string x = (j / 10).ToString();scale = 15;SizeF fontSize = g.MeasureString(x, scaleLabel);g.DrawString(x, scaleLabel, Brushes.Blue, designer1.Location.X - fontSize.Width - scale, designer1.Location.Y + j * percent - fontSize.Height / 2);g.DrawString(x, scaleLabel, Brushes.Blue, designer1.Location.X + designer1.Width + scale, designer1.Location.Y + j * percent - fontSize.Height / 2);}else if (j % 5 == 0){scale = 10;}else{scale = 5;}ps = new PointF(designer1.Location.X, designer1.Location.Y + j * percent);pe = new PointF(designer1.Location.X - scale, designer1.Location.Y + j * percent);g.DrawLine(Pens.Blue, ps, pe);ps = new PointF(designer1.Location.X, designer1.Location.Y + j * percent);pe = new PointF(designer1.Location.X + designer1.Width + scale, designer1.Location.Y + j * percent);g.DrawLine(Pens.Blue, ps, pe);}//if (mouseLocationXs != null)//{// g.DrawLine(Pens.Blue, mouseLocationXs, mouseLocationXe);//}}/// <summary>/// 当工具更换后,通知ToolBox更换工具/// </summary>/// <param name="tool"></param>protected void designer1_OnActiveToolChange(ToolBase tool){drawToolBox1.SelectTool = tool;}/// <summary>/// 设计器是对像删除事件/// </summary>protected void designer1_OnItemChange(){itemList1.DrawItems = designer1.Items;designer1_OnShowProperityInfo(designer1.Items.GetSelectItem(0));}/// <summary>/// 将对像的属性显示出来/// </summary>/// <param name="sender"></param>protected void designer1_OnShowProperityInfo(DrawItemBase sender){propertyGrid1.SelectedObject = sender;}/// <summary>/// 设置设计器当前的工具是?/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void drawToolBox1_ToolChanged(object sender, EventArgs e){designer1.ActiveTool = drawToolBox1.SelectTool;}/// <summary>/// 画出设计器的标尺/// </summary>/// <param name="sender"></param>/// <param name="e"></param>//private void panel1_Paint(object sender, PaintEventArgs e)//{// DrawScale(e.Graphics);//}/// <summary>/// 当在属性列表中更新数据后及时更新设计器内容,让更改的内容显示出来/// </summary>/// <param name="s"></param>/// <param name="e"></param>private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e){//如果所选的对像为DrawText修改值的时候要设置编辑输入框为不可见if (propertyGrid1.SelectedObject.GetType().Name == "DrawText"){designer1.textBox.Visible = false;designer1.textBox.Enabled = false;}designer1.Refresh();}#region 新建,打开,保存,删除,打印........../// <summary>/// 新建设计/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsNew_Click(object sender, EventArgs e){//先检查设计器里对像有没有修改,有则提示保存if (designer1.ChangeFlage){DialogResult dr = MessageBox.Show("是否保存设计?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);if (dr == DialogResult.Yes){FileDialog fd = new SaveFileDialog();fd.Filter = "CraxyMouse file (*.CraxyMouse)|*.CraxyMouse";fd.InitialDirectory = Application.StartupPath;if (fd.ShowDialog() == DialogResult.OK){openFileName = fd.FileName;InOutPut.SaveFile(designer1, fd.FileName);}}else if (dr == DialogResult.Cancel){return;}//将状态设为没有修改过的designer1.ChangeFlage = false;}//清空对像designer1.Items.Clear();designer1.Refresh();openFileName = "";}/// <summary>/// 打开设计器中的对像/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsOpen_Click(object sender, EventArgs e){if (designer1.ChangeFlage){DialogResult dr = MessageBox.Show("是否保存更改?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);if (dr == DialogResult.Yes){//如果有没有保存文件//则先保存文件if (string.IsNullOrEmpty(openFileName)){//检查当前的文件是否是保存过的//如没有保存则重新保存FileDialog fd = new SaveFileDialog();fd.Filter = "CraxyMouse file (*.CraxyMouse)|*.CraxyMouse";fd.InitialDirectory = Application.StartupPath;if (fd.ShowDialog() == DialogResult.OK){openFileName = fd.FileName;designer1.ChangeFlage = false;InOutPut.SaveFile(designer1, fd.FileName);designer1.Items.Clear();designer1.Refresh();}}else{//保存过的直接保存InOutPut.SaveFile(designer1, openFileName);}}else if (dr == DialogResult.Cancel){//如果用户取消了则什么也不做return;}designer1.ChangeFlage = false;}FileDialog fod = new OpenFileDialog();fod.Filter = "CraxyMouse file (*.CraxyMouse)|*.CraxyMouse";fod.InitialDirectory = Application.StartupPath;if (fod.ShowDialog() == DialogResult.OK){openFileName = fod.FileName;bool open = InOutPut.OpenFile(designer1, openFileName);//打开文件是否成功//如果打开成功是重新绘标尺if (open){panel1.Refresh();designer1_OnItemChange();numHeight.Value = (int)CommonSettings.PixelConvertMillimeter(designer1.Height);numWidth.Value = (int)CommonSettings.PixelConvertMillimeter(designer1.Width);if (designer1.RowHeight > 0){numRowHeight.Value = (int)CommonSettings.PixelConvertMillimeter(designer1.RowHeight);}if (designer1.PageRows > 0){numRows.Value = designer1.PageRows;}}}}/// <summary>/// 保存设计器中的对像/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsSave_Click(object sender, EventArgs e){if (string.IsNullOrEmpty(openFileName)){FileDialog fd = new SaveFileDialog();fd.Filter = "CraxyMouse file (*.CraxyMouse)|*.CraxyMouse";fd.InitialDirectory = Application.StartupPath;if (fd.ShowDialog() == DialogResult.OK){openFileName = fd.FileName;designer1.RowHeight = (int)CommonSettings.MillimeterConvertPixel((float)numRowHeight.Value);designer1.PageRows = (int)numRows.Value;InOutPut.SaveFile(designer1, fd.FileName);MessageBox.Show("保存文件成功!\n\r保存路径为:" + fd.FileName, "", MessageBoxButtons.OK, MessageBoxIcon.Information);//designer1.Items.Clear();designer1.Refresh();}}else{InOutPut.SaveFile(designer1, openFileName);}designer1.ChangeFlage = false;}/// <summary>/// 删除设计器中选中的对像/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsDelete_Click(object sender, EventArgs e){designer1.DeleteSelectedItem();}/// <summary>/// 置顶/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsTop_Click(object sender, EventArgs e){designer1.Items.SendFront();designer1.Refresh();designer1_OnItemChange();}/// <summary>/// 置底/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsBottom_Click(object sender, EventArgs e){designer1.Items.SendBlow();designer1.Refresh();designer1_OnItemChange();}/// <summary>/// 退出/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsExit_Click(object sender, EventArgs e){if (designer1.ChangeFlage && (MessageBox.Show("退出程序之前要保存设计吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)){tsSave_Click(sender, e);}if (MessageBox.Show("你确定要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK){Application.Exit();}}/// <summary>/// 如果用户在对像列表中选择了对像,则更新到设计器中/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void itemList1_SelectedIndexChanged(object sender, EventArgs e){designer1.Items.UnSelectAll();designer1.Items[itemList1.SelectedIndex].Selected = true;propertyGrid1.SelectedObject = designer1.Items[itemList1.SelectedIndex];designer1.Refresh();}/// <summary>/// 打印预览/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsView_Click(object sender, EventArgs e){designer1.PrintView();}/// <summary>/// 关于/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsInfo_Click(object sender, EventArgs e){}/// <summary>/// 打印/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsPrint_Click(object sender, EventArgs e){DataTable dt = new DataTable();dt.Columns.Add("Pay");dt.Columns.Add("Receive");dt.Columns.Add("TaxNum");dt.Columns.Add("Item");dt.Columns.Add("Amount");dt.Columns.Add("Desc");DataRow dr = dt.NewRow();dr[0] = "徐春晓";dr[1] = "徐春晓";dr[2] = "9658258";dr[3] = "电脑-001";dr[4] = "10000";dr[5] = "已发货";dt.Rows.Add(dr);dr = dt.NewRow();dr[0] = "徐春晓1";dr[1] = "徐春晓1";dr[2] = "96582581";dr[3] = "电脑-002";dr[4] = "10000";dr[5] = "已发货1";dt.Rows.Add(dr);dr = dt.NewRow();dr[0] = "徐春晓2";dr[1] = "徐春晓2";dr[2] = "9658258";dr[3] = "电脑-002";dr[4] = "100002";dr[5] = "已发货2";dt.Rows.Add(dr);dr = dt.NewRow();dr[0] = "徐春晓3";dr[1] = "徐春晓3";dr[2] = "96582583";dr[3] = "电脑-003";dr[4] = "100003";dr[5] = "已发货3";dt.Rows.Add(dr);dr = dt.NewRow();dr[0] = "徐春晓4";dr[1] = "徐春晓4";dr[2] = "96582585";dr[3] = "电脑-0014";dr[4] = "100003";dr[5] = "已发货4";dt.Rows.Add(dr);designer1.DataSource = dt;designer1.PrintPage(1);}#endregion/// <summary>/// 改变设计的宽度/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void numWidth_ValueChanged(object sender, EventArgs e){//改变设计器的尺寸并重绘标尺designer1.Width = CommonSettings.MillimeterConvertPixel((float)numWidth.Value);panel1.Refresh();}private void numHeight_ValueChanged(object sender, EventArgs e){//改变设计器的尺寸并重绘标尺designer1.Height = CommonSettings.MillimeterConvertPixel((float)numHeight.Value);panel1.Refresh();}/// <summary>/// 设计每页有多少行/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void numRows_ValueChanged(object sender, EventArgs e){designer1.PageRows = (int)numRows.Value;}/// <summary>/// 设置行高/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void numRowHeight_ValueChanged(object sender, EventArgs e){designer1.RowHeight = CommonSettings.MillimeterConvertPixel((float)numRowHeight.Value);}/// <summary>/// 打印设置 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsSet_Click(object sender, EventArgs e){frmSetting fs = new frmSetting(designer1.PrinterConfig);fs.ShowDialog();}/// <summary>/// 对像列表按键事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void itemList1_KeyDown(object sender, KeyEventArgs e){if (e.KeyCode == Keys.Delete){designer1.DeleteSelectedItem();designer1.Refresh();}}private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){}/// <summary>/// 滚动条拖动时更新机标尺/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void panel3_Scroll(object sender, ScrollEventArgs e){if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)//水平尺{ruler1.StartValue = (int)CommonSettings.PixelConvertMillimeter(GetScrollValue(e.NewValue, e.OldValue));}else//垂直尺{ruler2.StartValue = (int)CommonSettings.PixelConvertMillimeter(GetScrollValue(e.NewValue, e.OldValue));}}/// <summary>/// 获取拖动的最小值做为标尺的起动值/// </summary>/// <param name="newvalue"></param>/// <param name="oldvalue"></param>/// <returns></returns>private int GetScrollValue(int newvalue, int oldvalue){if (newvalue > oldvalue){return oldvalue;}return newvalue;}/// <summary>/// 显示标尺/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void tsGrid_Click(object sender, EventArgs e){designer1.ShowGrid = !designer1.ShowGrid;if (designer1.ShowGrid){tsGrid.CheckState = CheckState.Indeterminate;}else{tsGrid.CheckState = CheckState.Unchecked;}}}
}
5、打印设计软件运行界面及效果
项目资源获取
https://download.csdn.net/download/xdpcxq/89274784