看了“康忠鑫-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:可能有些童鞋覺得無意義。自己覺得喜歡就好。我拖拖拖,拖控件有益身體健康...