绪论 为了方便毕设的一个代码复用。
控件重绘 圆形标签:Lable 右键、添加、用户控件 修改基类为Lable , public partial class CircleLable : Label
修改函数InitializeComponent()为 private void InitializeComponent() { components = new System.ComponentModel.Container(); }
绘制操作
初始化设置:ControlStyles标志设置、初始尺寸、工作区、背景色1 2 3 4 5 6 7 8 9 10 11 SetStyle(ControlStyles.UserPaint, true ); SetStyle(ControlStyles.AllPaintingInWmPaint, true ); SetStyle(ControlStyles.OptimizedDoubleBuffer, true ); SetStyle(ControlStyles.ResizeRedraw, true ); SetStyle(ControlStyles.Selectable, true ); SetStyle(ControlStyles.SupportsTransparentBackColor, true ); this .Size = new Size(80 , 60 );BackColor = Color.Transparent; rect = this .ClientRectangle;
属性扩展:背景色、边框粗细、边框色1 2 3 4 5 6 7 8 9 10 11 12 13 14 private Color bgColor = Color.LightGray; public Color BgColor { get { return bgColor; } set { bgColor = value ; Invalidate(); } }
控件重绘:标签的背景、边框、文本 Paint
完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace UIDesign.UControls { public partial class CircleLable : Label { public CircleLable () { InitializeComponent(); SetStyle(ControlStyles.UserPaint, true ); SetStyle(ControlStyles.AllPaintingInWmPaint, true ); SetStyle(ControlStyles.OptimizedDoubleBuffer, true ); SetStyle(ControlStyles.ResizeRedraw, true ); SetStyle(ControlStyles.Selectable, true ); SetStyle(ControlStyles.SupportsTransparentBackColor, true ); this .Size = new Size(80 , 60 ); BackColor = Color.Transparent; rect = this .ClientRectangle; } Rectangle rect; private Color bgColor = Color.LightGray; public Color BgColor { get { return bgColor; } set { bgColor = value ; Invalidate(); } } private Color borderColor = Color.Gray; public Color BorderColor { get { return borderColor; } set { borderColor = value ; Invalidate(); } } private int borderWidth = 0 ; public int BorderWidth { get { return borderWidth; } set { borderWidth = value ; Invalidate(); } } protected override void OnSizeChanged (EventArgs e ) { rect = ClientRectangle; rect.Width -= 1 ; rect.Height -= 1 ; } protected override void OnPaint (PaintEventArgs e ) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.HighQuality; Rectangle rectBg; if (borderWidth > 0 ) { g.FillEllipse(new SolidBrush(BorderColor), rect); rectBg = new Rectangle(rect.X + borderWidth, rect.Y + borderWidth, rect.Width - 2 * borderWidth, rect.Height - 2 * borderWidth); } else rectBg = rect; g.FillEllipse(new SolidBrush(BgColor), rectBg); StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Center; SolidBrush fontBrush = new SolidBrush(this .ForeColor); g.DrawString(this .Text, this .Font, fontBrush, rect, format); } } }
实现步骤
新建类比如“USwitch.cs”;
引用命名控件 using System.Windows.Forms;
派生与Control public class USwitch :Control