印章WinForm自定义控件封装,提供源码下载

看了“康忠鑫-Stephen”的文章(http://www.cnblogs.com/axing/archive/2013/06/04/3116328.html)知道了C#如何通过gdi+绘制图章。但是嫌原来的代码封装不够,不能实现拖控件,改属性自定义自己的印章。于是下载了代码研究了一下,准备把这玩意儿封装成控件。下面看我如何实现!

分析:

1、WinForm控件的PictureBox比较适合来展示印章图片,于是自定义控件继承了PictureBox。

2、把常用属性进行封装。

总结:封装成控件没啥难度。ACTION!

成品观赏:

1、设计时

2、运行时

 

 

图章水印效果:

上代码:

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

namespace Com.DataCool.SealDesignLib
{
    /// <summary>
    /// 可视化图章控件继承PictureBox
    /// </summary>
    public class VisualSealDesignControl : PictureBox
    {
        private string _SealOrgText;
        /// <summary>
        /// SealOrgText 圆形排列环绕五角星的机构全称等文字
        /// </summary>
        [DefaultValue(typeof(string), "datacool.cnblogs.com"), Category("印章元素"), Description("图章的机构中英文名称、网址等.")]
        public string SealOrgText
        {
            get
            {
                return _SealOrgText;
            }
            set
            {
                _SealOrgText = value;
                this.Invalidate();
            }
        }

        private  string _SealOrgCnText;
        /// <summary>
        /// 机构中文简称
        /// </summary>
        [DefaultValue(typeof(string), "数据酷软件"), Category("印章元素"), Description("图章的机构中文简称名称等.")]
        public string SealOrgCnText
        {
            get
            {
                return _SealOrgCnText;
            }
            set
            {
                _SealOrgCnText = value;
                this.Invalidate();
            }
        }
               
        [Category("SealDesign"), Description("图章的机构中文简称名称等.")]
        private Font _SealFont = new Font("黑体",16,FontStyle.Bold);
        public Font SealFont
        {
            get
            {
                return _SealFont;
            }
            set
            {
                _SealFont = value;
                this.Invalidate();
            }
        }

        private Color _SealTextColor;
        [DefaultValue(typeof(Color), "Red"), Category("印章元素"), Description("图章文字颜色.")]
        public Color SealTextColor
        {
            get
            {
                return _SealTextColor;
            }
            set
            {
                _SealTextColor = value;
                this.Invalidate();
            }
        }

        private int _SealSize = 180;
        [DefaultValue(typeof(int), "180"), Category("印章元素"), Description("图章大小,直径(像素).")]   
        public int SealSize
        {
            get
            {
                return _SealSize;
            }
            set
            {
                _SealSize = value;
            }
        }
        
        public VisualSealDesignControl()
        {
            _SealOrgText = "datacool.cnblogs.com";
            _SealOrgCnText = "数据酷软件";
            SealSize = 180;
            _SealFont = new Font("宋体", 14, FontStyle.Bold);
            _SealTextColor = Color.Red;
            this.Size = new Size(_SealSize, _SealSize);
            this.SizeMode = PictureBoxSizeMode.Zoom;  
        }

        /// <summary>/
        /// 重绘出默认图片
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            if (!DesignMode)
            {
                TextOnSeal _top = new TextOnSeal();
                _top.TextFont = SealFont;
                _top.FillColor = SealTextColor;
                _top.ColorTOP = Color.Black;
                _top.Text = SealOrgText;
                _top.BaseString = SealOrgCnText;
                _top.ShowPath = true;
                _top.LetterSpace = 2;
                _top.SealSize = 180;
                _top.CharDirection = Char_Direction.Center;
                _top.SetIndent(20);
                pe.Graphics.DrawImage(_top.TextOnPathBitmap(), 0, 0);
            }
        }    
    }

    public enum Char_Direction
    {
        Center = 0,
        OutSide = 1,
        ClockWise = 2,
        AntiClockWise = 3,
    }
}

关键点解释:

1、重写PictureBox的OnPaint实际调用TextOnSeal生成图片。

  /// <summary>/
        /// 重绘出默认图片
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            if (!DesignMode)
            {
                TextOnSeal _top = new TextOnSeal();
                _top.TextFont = SealFont;
                _top.FillColor = SealTextColor;
                _top.ColorTOP = Color.Black;
                _top.Text = SealOrgText;
                _top.BaseString = SealOrgCnText;
                _top.ShowPath = true;
                _top.LetterSpace = 2;
                _top.SealSize = 180;
                _top.CharDirection = Char_Direction.Center;
                _top.SetIndent(20);
                pe.Graphics.DrawImage(_top.TextOnPathBitmap(), 0, 0);
            }
        }    

 2、属性变化时,重绘图片进行刷新

在属性的Set里强制控件进行刷新,触发OnPaint事件进行重画。

完整源码下载:请猛击这里喔!

PS:可能有些童鞋觉得无意义。自己觉得喜欢就好。我拖拖拖,拖控件有益身体健康...

posted @ 2013-06-04 18:01  数据酷软件  阅读(3093)  评论(4编辑  收藏  举报