tx
2025-04-09 fa7510e1ed63df0366787fa4ed1b3db6426d2b46
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace WinCustControls
{
    public partial class UPanel : Panel
    {
        public UPanel()
        {
            InitializeComponent();
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);//忽略窗口消息,减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//绘制到缓冲区,减少闪烁
            SetStyle(ControlStyles.UserPaint, true);//控件由其自身而不是操作系统绘制
            SetStyle(ControlStyles.ResizeRedraw, true);//控件调整其大小时重绘
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);//支持透明背景
            r = this.ClientRectangle;
        }
 
        private Color bgColor = Color.LightGray;
        /// <summary>
        /// 背景色1 (渐变颜色1)
        /// </summary>
        [DefaultValue(typeof(Color), "LightGray"), Description("控件的背景色1")]
        public Color BgColor
        {
            get { return bgColor; }
            set
            {
                bgColor = value;
                //Refresh();//立即重绘
                Invalidate();//引发重绘,不会立即执行
            }
        }
 
        private Color bgColor2 = Color.Transparent;
        /// <summary>
        /// 背景色2 (渐变颜色2)
        /// </summary>
        [DefaultValue(typeof(Color), "Transparent"), Description("控件的背景色2")]
        public Color BgColor2
        {
            get { return bgColor2; }
            set
            {
                bgColor2 = value;
                Invalidate();
            }
        }
 
        private Color borderColor = Color.Gray;
        /// <summary>
        /// 边框颜色 
        /// </summary>
        [DefaultValue(typeof(Color), "Gray"), Description("控件的边框颜色")]
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                Invalidate();
            }
        }
 
        private int borderWidth = 0;
        /// <summary>
        /// 边框粗细 
        /// </summary>
        [DefaultValue(typeof(int), "0"), Description("控件的边框粗细")]
        public int BorderWidth
        {
            get { return borderWidth; }
            set
            {
                borderWidth = value;
                Invalidate();
            }
        }
 
        private int radius = 5;
        /// <summary>
        /// 边框圆角半径 
        /// </summary>
        [DefaultValue(typeof(int), "5"), Description("控件的边框圆角半径")]
        public int Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                Invalidate();
            }
        }
 
        private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
        /// <summary>
        /// 边框圆角半径 
        /// </summary>
        [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("控件的背景渐变模式")]
        public LinearGradientMode GradientMode
        {
            get { return gradientMode; }
            set
            {
                gradientMode = value;
                Invalidate();
            }
        }
 
        Rectangle r;//绘制区域
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            r = this.ClientRectangle;
            this.Region = new Region(r);
            r.Width -= 1;
            r.Height -= 1;
 
        }
 
        /// <summary>
        /// 重写绘制
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //绘制工作  边框   背景 
            Graphics g = e.Graphics;//绘图对象
            //呈现质量--高质量呈现 
            g.SmoothingMode = SmoothingMode.AntiAlias;
            GraphicsPath path1 = new GraphicsPath();//边框的圆角矩形
            GraphicsPath path2 = new GraphicsPath();//背景部分圆角矩形
            Rectangle rect;//内部填充的矩形
            if (radius > 0)
            {
                //生成外部矩形的圆角矩形路径
                path1 = PaintClass.GetRoundRectangle(r, radius);
                if (borderWidth > 0)
                {
                    g.FillPath(new SolidBrush(borderColor), path1);//填充边框矩形
                    //定义内部矩形结构
                    rect = new Rectangle(r.X + borderWidth, r.Y + borderWidth, r.Width - 2 * borderWidth, r.Height - 2 * borderWidth);
                    //生成内部矩形的圆角矩形路径
                    path2 = PaintClass.GetRoundRectangle(rect, radius - 1);
                }
                else
                {
                    path2 = path1;
                    rect = r;
                }
                //背景色填充
                if (bgColor2 != Color.Transparent)
                {
                    //渐变画刷
                    LinearGradientBrush bgBrush = new LinearGradientBrush(rect, bgColor, bgColor2, gradientMode);
                    g.FillPath(bgBrush, path2);//填充背景
                }
                else
                {
                    Brush b = new SolidBrush(bgColor);
                    g.FillPath(b, path2);
                }
            }
            else
            {
                if (borderWidth > 0)
                {
                    g.FillRectangle(new SolidBrush(borderColor), r);//填充边框矩形
                    //定义内部矩形结构
                    rect = new Rectangle(r.X + borderWidth, r.Y + borderWidth, r.Width - 2 * borderWidth, r.Height - 2 * borderWidth);
                }
                else
                {
                    rect = r;
                }
                //背景色填充
                if (bgColor2 != Color.Transparent)
                {
                    //渐变画刷
                    LinearGradientBrush bgBrush = new LinearGradientBrush(rect, bgColor, bgColor2, gradientMode);
                    g.FillRectangle(bgBrush, rect);//填充背景
                }
                else
                {
                    Brush b = new SolidBrush(bgColor);
                    g.FillRectangle(b, rect);
                }
            }
        }
    }
}