C#标签设计打印软件开发

news2024/12/26 23:46:50

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>
        /// 条码值的长度
        /// </summary>
        private int strLength = 0;
        /// <summary>
        /// 条码的比率
        /// </summary>
        private int wideToNarrowRatio = 2;
        #endregion

        #region Base String Value
        String 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 Barcode
        public 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 matrix

            PointF 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);
                    else
                        wid = (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 rectangle
        public 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;
                else
                    wid = (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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1665640.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

图论(洛谷刷题)

目录 前言&#xff1a; 题单&#xff1a; P3386 【模板】二分图最大匹配 P1525 [NOIP2010 提高组] 关押罪犯 P3385 【模板】负环 P3371 【模板】单源最短路径&#xff08;弱化版&#xff09; SPFA写法 Dij写法&#xff1a; P3385 【模板】负环 P5960 【模板】差分约束…

python的import导入规则

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pycharm只能看到当前工作路径父目录下所有文件和项目根目录下所有文件二、sys或者图形界面添加解释器路径&#xff08;搜寻路径&#xff09;三、import导入…

【ubuntu】ubuntu-18.04开机卡在Starting User Manager for UID 120....问题解决方案

错误截图 解决方案 启动系统&#xff0c;开机界面单击按键esc键&#xff0c;注意需要将鼠标定位到菜单界面&#xff0c;移动键盘上下键选择Advanced options for Ubuntu 进入如下菜单&#xff0c;选择recovery mode 回车之后会弹出如下界面&#xff0c;选择如下root&#xff0…

matlab使用教程(69)—创建包含多个 x 轴和 y 轴的图

此示例说明如何创建这样一张图&#xff0c;通过坐标区底部和左侧的轴放置第一个绘图&#xff0c;并通过坐标区顶部和右侧的轴放置第二个绘图。 使用 line 函数绘制一个红色线条。将 x 轴和 y 轴的轴线颜色设置为红色。 注意&#xff1a;从 R2014b 开始&#xff0c;您可以使用圆…

最大子序列的分数

题目链接 最大子序列的分数 题目描述 注意点 n nums1.length nums2.length从nums1和nums2中选一个长度为k的子序列对应的下标对nums1中下标对应元素求和&#xff0c;乘以nums2中下标对应元素的最小值得到子序列的分数0 < nums1[i], nums2[j] < 1000001 < k < …

精密机械设备运用弧形导轨中如何保持高精度?

导轨精度标准是对导轨的精度统一规定&#xff0c;无论是滑移运动、滑块运动还是旋转运动&#xff0c;都有一定的精度规格。而导轨精度标准是为了保证导轨运动时的精确度而设定的精度标准&#xff0c;它是规定各种导轨的精度统一标准&#xff0c;是机械设备的运动精度基础和保障…

SpringAI 技术解析

1. 发展历史 SpringAI 的发展历史可以追溯到对 Spring 框架的扩展和改进&#xff0c;以支持人工智能相关的功能。随着人工智能技术的快速发展&#xff0c;SpringAI 逐渐成为 Spring 生态系统中的一个重要组成部分&#xff0c;为开发者提供了便捷、灵活的解决方案。 项目的灵感来…

声明变量的六种方法

ES6 声明变量的六种方法 varfunctionletconstclassimport 顶层对象的属性 1. ES6 声明变量的六种方法 ES5 只有两种声明变量的方法&#xff1a; var 命令和 function 命令。 ES6 除了添加 let 和 const 命令&#xff0c;还有另外两种声明变量的方法&#xff1a; import 命令和…

[AutoSar]BSW_Diagnostic_002 DCM模块介绍

目录 关键词平台说明背景一、DCM所处架构位置二、DCM 与其他模块的交互三、DCM 的功能四、DCM的内部子模块4.1 关键词 嵌入式、C语言、autosar、OS、BSW、UDS、diagnostic 平台说明 项目ValueOSautosar OSautosar厂商vector &#xff0c; EB芯片厂商TI 英飞凌编程语言C&…

Realsense-Realman手眼标定

硬件设备 Realsense D405 Realman 65b 软件环境搭建 软件环境依赖&#xff1a; librealsensehttps://github.com/IntelRealSense/librealsense.git ROS1.0ros-noetic-arucosudo apt-get install ros-noetic-aruco*realsense_roshttps://github.com/IntelRealSense/realsens…

萤火虫优化算法(Firefly Algorithm)

注意&#xff1a;本文引用自专业人工智能社区Venus AI 更多AI知识请参考原站 &#xff08;[www.aideeplearning.cn]&#xff09; 算法背景 萤火虫优化算法&#xff0c;是由剑桥大学的Xin-She Yang在2009年提出的一种基于群体智能的优化算法。它的灵感来源于萤火虫在夜晚闪烁…

Python | Leetcode Python题解之第83题删除排序链表中的重复元素

题目&#xff1a; 题解&#xff1a; class Solution:def deleteDuplicates(self, head: ListNode) -> ListNode:if not head:return headcur headwhile cur.next:if cur.val cur.next.val:cur.next cur.next.nextelse:cur cur.nextreturn head

PDF文件恢复:四种实用方法全解析

如何恢复已删除的PDF文件&#xff1f; PDF是Portable Document Format&#xff08;便携式文档格式&#xff09;的缩写&#xff0c;是一种由Adobe Systems开发的文件格式。PDF文件可以包含文本、图形、链接、多媒体以及其他各种元素&#xff0c;并且能够在各种操作系统和设备上…

XXE-lab靶场搭建

源码下载地址 https://github.com/c0ny1/xxe-lab1.php_xxe 直接放在php web页面下即可运行。 2.java_xxe java_xxe是serlvet项目&#xff0c;直接导入eclipse当中即可部署运行。 3.python_xxe: 安装好Flask模块python xxe.py 4.Csharp_xxe 直接导入VS中运行 phpstudy…

树莓派遇到ping的奇葩问题解决办法

首先&#xff0c;先 ping raspberrypi 一下。获得树莓派的ip 然后开始配置静态ip winR后输入命令ipconfig查询当前网关ip 输入命令sudo nano /etc/dhcpcd.conf 在最末尾输入以下信息 -----------------------------------------------------------------------------------…

波动性悖论:为何低风险股票长期跑赢高风险对手?

从去年开始&#xff0c;“红利低波”类的产品净值稳步向上&#xff0c;不断新高&#xff0c;让很多人关注到了A股“分红高”、“波动率低”这两类股票。分红高的公司更受投资者青睐&#xff0c;这从基本面的角度很容易理解&#xff0c;那么波动率低的股票明明波动更小&#xff…

8、QT——QLabel使用小记2

前言&#xff1a;记录开发过程中QLabel的使用&#xff0c;持续更新ing... 开发平台&#xff1a;Win10 64位 开发环境&#xff1a;Qt Creator 13.0.0 构建环境&#xff1a;Qt 5.15.2 MSVC2019 64位 一、基本属性 技巧&#xff1a;对于Qlabel这类控件的属性有一些共同的特点&am…

使用html和css实现个人简历表单的制作

根据下列要求&#xff0c;做出下图所示的个人简历&#xff08;表单&#xff09; 表单要求 Ⅰ、表格整体的边框为1像素&#xff0c;单元格间距为0&#xff0c;表格中前六列列宽均为100像素&#xff0c;第七列 为200像素&#xff0c;表格整体在页面上居中显示&#xff1b; Ⅱ、前…

猜猜歇后语

页面 在输入框中填写你猜的答案&#xff0c;点击“显示答案”按钮&#xff0c;显示正确答案。 页面代码 function showAnswer(element){var elem$(element);elem.next().show();} //# // 初始化DataGrid对象 $(#dataGrid).dataGrid({searchForm: $(#searchForm),columnModel:…

Django性能之道:缓存应用与优化实战

title: Django性能之道&#xff1a;缓存应用与优化实战 date: 2024/5/11 18:34:22 updated: 2024/5/11 18:34:22 categories: 后端开发 tags: 缓存系统Redis优点Memcached优缺点Django缓存数据库优化性能监控安全实践 引言 在当今的互联网时代&#xff0c;用户对网站和应用…