绪论

为了方便毕设的一个代码复用。

控件重绘

圆形标签:Lable

右键、添加、用户控件

修改基类为Lable,

public partial class CircleLable : Label

修改函数InitializeComponent()为
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}

绘制操作

  1. 初始化设置:ControlStyles标志设置、初始尺寸、工作区、背景色
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //ControlStyles设置
    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;
  2. 属性扩展:背景色、边框粗细、边框色
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    //背景色   边框色   边框粗细
    private Color bgColor = Color.LightGray;
    /// <summary>
    /// 背景画刷
    /// </summary>
    public Color BgColor
    {
    get { return bgColor; }
    set
    {
    bgColor = value;
    Invalidate();
    }
    }
  3. 控件重绘:标签的背景、边框、文本 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();
//ControlStyles设置
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;
/// <summary>
/// 背景画刷
/// </summary>
public Color BgColor
{
get { return bgColor; }
set
{
bgColor = value;
Invalidate();
}
}

private Color borderColor = Color.Gray;
/// <summary>
/// 边框画刷
/// </summary>
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();
}
}

private int borderWidth = 0;
/// <summary>
/// 边框粗细
/// </summary>
public int BorderWidth
{
get { return borderWidth; }
set
{
borderWidth = value;
Invalidate();
}
}

protected override void OnSizeChanged(EventArgs e)
{
rect = ClientRectangle;
rect.Width -= 1;
rect.Height -= 1;
}

/// <summary>
/// 重绘控件
/// </summary>
/// <param name="e"></param>
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);
}
}

}

实现步骤

  1. 新建类比如“USwitch.cs”;
  2. 引用命名控件
    using System.Windows.Forms;
  3. 派生与Control
    public class USwitch :Control