在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 打印模板生成界面拖动生成XML文件

C# 打印模板生成界面拖动生成XML文件

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:1.63M
  • 下载次数:182
  • 浏览次数:1948
  • 发布时间:2018-02-01
  • 实例类别:C#语言基础
  • 发 布 人:aries
  • 文件格式:.zip
  • 所需积分:2
 相关标签: Xml 文件 打印 界面 模板

同类人气实例

实例介绍

【实例简介】可拖动打印的内容,自定义打印内容

【实例截图】

from clipboard


from clipboard


【核心代码】


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DragControl
{
    public class XMLHelper
    {
        private static string filepath = AppDomain.CurrentDomain.BaseDirectory "Data\\PrintTpls.xml";

        public static List<PrintTpl> GetPrintTpl()
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(filepath);
            XmlNodeList xmlNoteList = xd.GetElementsByTagName("PrintTpl");

            List<PrintTpl> tpls = new List<PrintTpl>();
            foreach (XmlElement item in xmlNoteList)
            {
                PrintTpl pt = new PrintTpl();
                pt.name = item.GetAttribute("name");
                foreach (XmlElement xn in item.ChildNodes)
                {
                    LabelContent lc = new LabelContent();
                    lc.text = xn.GetAttribute("text");
                    lc.size = int.Parse(xn.GetAttribute("size"));
                    lc.left = int.Parse(xn.GetAttribute("left"));
                    lc.right = int.Parse(xn.GetAttribute("right"));
                    lc.nWidthTimes = int.Parse(xn.GetAttribute("nWidthTimes"));
                    lc.nHeightTimes = int.Parse(xn.GetAttribute("nHeightTimes"));
                    lc.nFontStyle = int.Parse(xn.GetAttribute("nFontStyle"));
                    if (pt.lc == null) { pt.lc = new List<LabelContent>(); }
                    pt.lc.Add(lc);
                }
                tpls.Add(pt);
            }
            return tpls;
        }

        public static void CreatePrintTpl(PrintTpl ptl)
        {
            //XmlDocument xmlDoc = new XmlDocument();
            //XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            //xmlDoc.AppendChild(header);
            ////创建一级节点
            //XmlElement rootNode = xmlDoc.CreateElement("PrintTpls");

            //此处在原有文件添加节点信息,以上注释是新建一个XML进行节点添加
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlElement rootNode = xmlDoc.DocumentElement;
            //一级节点添加子节点
            rootNode.AppendChild(InserPrintTpl(ptl, xmlDoc));
            xmlDoc.AppendChild(rootNode);
            xmlDoc.Save(filepath);
        }

        public static void DelPrintTpl(string nodename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlElement rootNode = xmlDoc.DocumentElement;
            foreach (XmlElement xn in rootNode)
            {
                if (xn.GetAttribute("name").Equals(nodename))
                {
                    rootNode.RemoveChild(xn);
                }
            }
            xmlDoc.Save(filepath);
        }

        private static XmlElement InserPrintTpl(PrintTpl pt, XmlDocument xmlDoc)
        {
            XmlElement xn = xmlDoc.CreateElement("PrintTpl");
            xn.SetAttribute("name", pt.name "");
            foreach (LabelContent lc in pt.lc)
            {
                xn.AppendChild(GetXmlNode(xmlDoc, lc));
            }
            return xn;
        }

        private static XmlElement GetXmlNode(XmlDocument xmlDoc, LabelContent lc)
        {
            XmlElement xn = xmlDoc.CreateElement("Label");
            xn.SetAttribute("text", lc.text "");
            xn.SetAttribute("size", lc.size "");
            xn.SetAttribute("left", lc.left "");
            xn.SetAttribute("right", lc.right "");
            xn.SetAttribute("nWidthTimes", lc.nWidthTimes "");
            xn.SetAttribute("nHeightTimes", lc.nHeightTimes "");
            xn.SetAttribute("nFontStyle", lc.nFontStyle "");
            return xn;
        }
    }

    /// <summary>
    /// 打印模板
    /// </summary>
    public class PrintTpl
    {
        /// <summary>
        /// 模板名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 模板标签内容
        /// </summary>
        public List<LabelContent> lc { get; set; }
    }

    /// <summary>
    /// 标签内容
    /// </summary>
    public class LabelContent
    {
        /// <summary>
        /// 显示文本
        /// </summary>
        public string text { get; set; }
        /// <summary>
        /// 文本大小
        /// </summary>
        public int size { get; set; }
        /// <summary>
        /// 文本左边距
        /// </summary>
        public int left { get; set; }
        /// <summary>
        /// 文本右边距
        /// </summary>
        public int right { get; set; }
        /// <summary>
        /// 字体宽度
        /// </summary>
        public int nWidthTimes { get; set; }
        /// <summary>
        /// 字体高度
        /// </summary>
        public int nHeightTimes { get; set; }
        /// <summary>
        /// 字体样式
        /// </summary>
        public int nFontStyle { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DragControl
{
    public class FrameControl : UserControl
    {
        #region Constructors
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrameControl(Control ctrl)
        {
            baseControl = ctrl;
            AddEvents();
            CreateBounds();
        }
        #endregion

        #region Fields
        const int Band = 6; //调整大小的响应边框
        private int MinWidth = 10; //最小宽度
        private int MinHeight = 10;//最小高度
        Size square = new Size(Band, Band);//小矩形大小
        Control baseControl; //基础控件,即被包围的控件
        Rectangle[] smallRects = new Rectangle[8];//边框中的八个小圆圈
        Rectangle[] sideRects = new Rectangle[4];//四条边框,用来做响应区域
        Point[] linePoints = new Point[5];//四条边,用于画虚线
        Graphics g; //画图板
        Rectangle ControlRect; //控件包含边框的区域  
        private Point pPoint; //上个鼠标坐标
        private Point cPoint; //当前鼠标坐标
        private MousePosOnCtrl mpoc;
        #endregion

        #region Properties
        /// <summary>
        /// 鼠标在控件中位置
        /// </summary>
        enum MousePosOnCtrl
        {
            NONE = 0,
            TOP = 1,
            RIGHT = 2,
            BOTTOM = 3,
            LEFT = 4,
            TOPLEFT = 5,
            TOPRIGHT = 6,
            BOTTOMLEFT = 7,
            BOTTOMRIGHT = 8,
        }
        #endregion

        #region Methods
        /// <summary>
        /// 加载事件
        /// </summary>
        private void AddEvents()
        {
            this.Name = "FrameControl" baseControl.Name;
            this.MouseDown = new MouseEventHandler(FrameControl_MouseDown);
            this.MouseMove = new MouseEventHandler(FrameControl_MouseMove);
            this.MouseUp = new MouseEventHandler(FrameControl_MouseUp);
        }

        #region 创建边框
        /// <summary>
        /// 建立控件可视区域
        /// </summary>
        private void CreateBounds()
        {
            //创建边界
            int X = baseControl.Bounds.X - square.Width - 1;
            int Y = baseControl.Bounds.Y - square.Height - 1;
            int Height = baseControl.Bounds.Height (square.Height * 2) 2;
            int Width = baseControl.Bounds.Width (square.Width * 2) 2;
            this.Bounds = new Rectangle(X, Y, Width, Height);
            this.BringToFront();
            SetRectangles();
            //设置可视区域
            this.Region = new Region(BuildFrame());
            g = this.CreateGraphics();
        }

        /// <summary>
        /// 设置定义8个小矩形的范围
        /// </summary>
        void SetRectangles()
        {
            //左上
            smallRects[0] = new Rectangle(new Point(0, 0), square);
            //右上
            smallRects[1] = new Rectangle(new Point(this.Width - square.Width - 1, 0), square);
            //左下
            smallRects[2] = new Rectangle(new Point(0, this.Height - square.Height - 1), square);
            //右下
            smallRects[3] = new Rectangle(new Point(this.Width - square.Width - 1, this.Height - square.Height - 1), square);
            //上中
            smallRects[4] = new Rectangle(new Point(this.Width / 2 - 1, 0), square);
            //下中
            smallRects[5] = new Rectangle(new Point(this.Width / 2 - 1, this.Height - square.Height - 1), square);
            //左中
            smallRects[6] = new Rectangle(new Point(0, this.Height / 2 - 1), square);
            //右中
            smallRects[7] = new Rectangle(new Point(square.Width baseControl.Width 1, this.Height / 2 - 1), square);

            //四条边线
            //左上
            linePoints[0] = new Point(square.Width / 2, square.Height / 2);
            //右上
            linePoints[1] = new Point(this.Width - square.Width / 2 - 1, square.Height / 2);
            //右下
            linePoints[2] = new Point(this.Width - square.Width / 2 - 1, this.Height - square.Height / 2);
            //左下
            linePoints[3] = new Point(square.Width / 2, this.Height - square.Height / 2 - 1);
            //左上
            linePoints[4] = new Point(square.Width / 2, square.Height / 2);

            //整个包括周围边框的范围
            ControlRect = new Rectangle(new Point(0, 0), this.Bounds.Size);
        }

        /// <summary>
        /// 设置边框控件可视区域
        /// </summary>
        /// <returns></returns>
        private GraphicsPath BuildFrame()
        {
            GraphicsPath path = new GraphicsPath();
            //上边框
            sideRects[0] = new Rectangle(0, 0, this.Width - square.Width - 1, square.Height 1);
            //左边框
            sideRects[1] = new Rectangle(0, square.Height 1, square.Width 1, this.Height - square.Height - 1);
            //下边框
            sideRects[2] = new Rectangle(square.Width 1, this.Height - square.Height - 1, this.Width - square.Width - 1, square.Height 1);
            //右边框
            sideRects[3] = new Rectangle(this.Width - square.Width - 1, 0, square.Width 1, this.Height - square.Height - 1);

            path.AddRectangle(sideRects[0]);
            path.AddRectangle(sideRects[1]);
            path.AddRectangle(sideRects[2]);
            path.AddRectangle(sideRects[3]);
            return path;
        }
        #endregion

        /// <summary>
        /// 绘图
        /// </summary>
        public void Draw()
        {
            this.BringToFront();
            //g.FillRectangles(Brushes.LightGray, sideRects); //填充四条边框的内部
            Pen pen = new Pen(Color.Black);
            pen.DashStyle = DashStyle.Dot;//设置为虚线,用虚线画四边,模拟微软效果
            g.DrawLines(pen, linePoints);//绘制四条边线
            g.FillRectangles(Brushes.White, smallRects); //填充8个小矩形的内部
            foreach (Rectangle smallRect in smallRects)
            {
                g.DrawEllipse(Pens.Black, smallRect);    //绘制8个小椭圆
            }
            //g.DrawRectangles(Pens.Black, smallRects);  //绘制8个小矩形的黑色边线
        }

        /// <summary>
        /// 设置光标状态
        /// </summary>
        public bool SetCursorShape(int x, int y)
        {
            Point point = new Point(x, y);
            if (!ControlRect.Contains(point))
            {
                Cursor.Current = Cursors.Arrow;
                return false;
            }
            else if (smallRects[0].Contains(point))
            {
                Cursor.Current = Cursors.SizeNWSE;
                mpoc = MousePosOnCtrl.TOPLEFT;
            }
            else if (smallRects[1].Contains(point))
            {
                Cursor.Current = Cursors.SizeNESW;
                mpoc = MousePosOnCtrl.TOPRIGHT;
            }
            else if (smallRects[2].Contains(point))
            {
                Cursor.Current = Cursors.SizeNESW;
                mpoc = MousePosOnCtrl.BOTTOMLEFT;
            }
            else if (smallRects[3].Contains(point))
            {
                Cursor.Current = Cursors.SizeNWSE;
                mpoc = MousePosOnCtrl.BOTTOMRIGHT;
            }
            else if (sideRects[0].Contains(point))
            {
                Cursor.Current = Cursors.SizeNS;
                mpoc = MousePosOnCtrl.TOP;
            }
            else if (sideRects[1].Contains(point))
            {
                Cursor.Current = Cursors.SizeWE;
                mpoc = MousePosOnCtrl.LEFT;
            }
            else if (sideRects[2].Contains(point))
            {
                Cursor.Current = Cursors.SizeNS;
                mpoc = MousePosOnCtrl.BOTTOM;
            }
            else if (sideRects[3].Contains(point))
            {
                Cursor.Current = Cursors.SizeWE;
                mpoc = MousePosOnCtrl.RIGHT;
            }
            else
            {
                Cursor.Current = Cursors.Arrow;
            }
            return true;
        }

        /// <summary>
        /// 控件移动
        /// </summary>
        private void ControlMove()
        {
            cPoint = Cursor.Position;
            int x = cPoint.X - pPoint.X;
            int y = cPoint.Y - pPoint.Y;
            switch (this.mpoc)
            {
                case MousePosOnCtrl.TOP:
                    if (baseControl.Height - y > MinHeight)
                    {
                        baseControl.Top = y;
                        baseControl.Height -= y;                        
                    }
                    else
                    {
                        baseControl.Top -= MinHeight - baseControl.Height;
                        baseControl.Height = MinHeight;
                    }
                    break;
                case MousePosOnCtrl.BOTTOM:
                    if (baseControl.Height y > MinHeight)
                    {
                        baseControl.Height = y;
                    }
                    else
                    {
                        baseControl.Height = MinHeight;
                    }
                    break;
                case MousePosOnCtrl.LEFT:
                    if (baseControl.Width - x > MinWidth)
                    {
                        baseControl.Left = x;
                        baseControl.Width -= x;                        
                    }
                    else
                    {
                        baseControl.Left -= MinWidth - baseControl.Width;
                        baseControl.Width = MinWidth;
                    }
                    
                    break;
                case MousePosOnCtrl.RIGHT:
                    if (baseControl.Width x > MinWidth)
                    {
                        baseControl.Width = x;
                    }
                    else
                    {
                        baseControl.Width = MinWidth;
                    }
                    break;
                case MousePosOnCtrl.TOPLEFT:
                    if (baseControl.Height - y > MinHeight)
                    {
                        baseControl.Top = y;
                        baseControl.Height -= y;
                    }
                    else
                    {
                        baseControl.Top -= MinHeight - baseControl.Height;
                        baseControl.Height = MinHeight;
                    }
                    if (baseControl.Width - x > MinWidth)
                    {
                        baseControl.Left = x;
                        baseControl.Width -= x;
                    }
                    else
                    {
                        baseControl.Left -= MinWidth - baseControl.Width;
                        baseControl.Width = MinWidth;
                    }
                    break;
                case MousePosOnCtrl.TOPRIGHT:
                    if (baseControl.Height - y > MinHeight)
                    {
                        baseControl.Top = y;
                        baseControl.Height -= y;
                    }
                    else
                    {
                        baseControl.Top -= MinHeight - baseControl.Height;
                        baseControl.Height = MinHeight;
                    }
                    if (baseControl.Width x > MinWidth)
                    {
                        baseControl.Width = x;
                    }
                    else
                    {
                        baseControl.Width = MinWidth;
                    }
                    break;               
                case MousePosOnCtrl.BOTTOMLEFT:
                    if (baseControl.Height y > MinHeight)
                    {
                        baseControl.Height = y;
                    }
                    else
                    {
                        baseControl.Height = MinHeight;
                    }
                    if (baseControl.Width - x > MinWidth)
                    {
                        baseControl.Left = x;
                        baseControl.Width -= x;
                    }
                    else
                    {
                        baseControl.Left -= MinWidth - baseControl.Width;
                        baseControl.Width = MinWidth;
                    }
                    break;
                case MousePosOnCtrl.BOTTOMRIGHT:
                    if (baseControl.Height y > MinHeight)
                    {
                        baseControl.Height = y;
                    }
                    else
                    {
                        baseControl.Height = MinHeight;
                    }
                    if (baseControl.Width x > MinWidth)
                    {
                        baseControl.Width = x;
                    }
                    else
                    {
                        baseControl.Width = MinWidth;
                    }
                    break;
                
            }
            pPoint = Cursor.Position;
        }        

        #endregion

        #region Events
        /// <summary>
        /// 鼠标按下事件:记录当前鼠标相对窗体的坐标
        /// </summary>
        void FrameControl_MouseDown(object sender, MouseEventArgs e)
        {
            pPoint = Cursor.Position;
        }

        /// <summary>
        /// 鼠标移动事件:让控件跟着鼠标移动
        /// </summary>
        void FrameControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = false;
                MoveControl.DrawDragBound(baseControl);
                ControlMove();
            }
            else
            {
                this.Visible = true;
                SetCursorShape(e.X, e.Y); //更新鼠标指针样式
            }
        }

        /// <summary>
        /// 鼠标弹起事件:让自定义的边框出现
        /// </summary>
        void FrameControl_MouseUp(object sender, MouseEventArgs e)
        {
            this.baseControl.Refresh(); //刷掉黑色边框
            this.Visible = true;
            CreateBounds();
            Draw();
        }
        #endregion

        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // FrameControl
            //
            this.Name = "FrameControl";
            this.Size = new System.Drawing.Size(80, 18);
            this.ResumeLayout(false);

        }
    }
}

实例下载地址

C# 打印模板生成界面拖动生成XML文件

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 511524565 发表于: 2018-11-30 22:55 36
好坑,严重性 代码 说明 项目 文件 行 禁止显示状态 错误 CS0246 未能找到类型或命名空间名“PrintManage”(是否缺少 using 指令或程序集引用?) DragControl C:\Users\Dev\Desktop\小票打印模板生成DragControl\DragControl\MainForm.cs 2 活动

支持(0) 盖楼(回复)

第 2 楼 用心做 发表于: 2018-12-01 16:39 57
好坑,严重性 代码 说明 项目 文件 行 禁止显示状态 错误 CS0246 未能找到类型或命名空间名“PrintManage”(是否缺少 using 指令或程序集引用?) DragControl C:\Users\Dev\Desktop\小票打印模板生成DragControl\DragControl\MainForm.cs 2 活动

511524565 2018-11-30 22:55 36

此问题已修复,请重新下载 就可以使用了

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警