印章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:可能有些童鞋觉得无意义。自己觉得喜欢就好。我拖拖拖,拖控件有益身体健康...
作者:数据酷软件
出处:https://www.cnblogs.com/datacool/archive/2013/06/04/VisualSealDesignControl.html
关于作者:20年编程从业经验,持续关注MES/ERP/POS/WMS/工业自动化
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明。
联系方式: qq:71008973;wx:6857740733
基于人脸识别的考勤系统 地址: https://gitee.com/afeng124/viewface_attendance_ext
自己开发安卓应用框架 地址: https://gitee.com/afeng124/android-app-frame
WPOS(warehouse+pos) 后台演示地址: http://47.239.106.75:8080/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?