SurPanel.cs
4.9 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Asa.Face
{
public partial class SurPanel : BasePnl
{
private bool _showTitle = true;
private int _titleHeight = 24;
private HorizontalAlignment _textAlign = HorizontalAlignment.Center;
private Rectangle _titleRect = new Rectangle();
private RectangleF _textRect = new RectangleF();
private Font _titleFont = new Font("宋体", 12f);
public SurPanel()
{
InitializeComponent();
}
[Browsable(false)]
public override Font Font { get => base.Font; set => base.Font = value; }
/// <summary>
/// 显示标题
/// </summary>
[Browsable(true), Category(""), Description("显示标题"), DefaultValue(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool ShowTitle
{
set
{
_showTitle = value;
//if (_showTitle)
// Padding = new Padding(6, 6 + _titleHeight, 6, 6);
//else
// Padding = new Padding(6, 6, 6, 6);
CalcSize();
Refresh();
}
get
{
return _showTitle;
}
}
[Browsable(true)]
public Font TitleFont
{
get => _titleFont;
set
{
_titleFont = value;
CalcSize();
Refresh();
}
}
/// <summary>
/// 显示的文本的对齐方式
/// </summary>
[Browsable(true), Category(""), Description("显示的文本的对齐方式"), DefaultValue(HorizontalAlignment.Center)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public HorizontalAlignment TextAlign
{
set
{
_textAlign = value;
CalcSize();
Refresh();
}
get
{
return _textAlign;
}
}
/// <summary>
/// 显示的标题的高度
/// </summary>
[Browsable(true), Category(""), Description("显示的标题的高度"), DefaultValue(24)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int TitleHeight
{
set
{
_titleHeight = value < 0 ? 0 : value;
//if (_showTitle)
// Padding = new Padding(6, 6 + _titleHeight, 6, 6);
CalcSize();
Refresh();
}
get
{
return _titleHeight;
}
}
protected override void CalcSize()
{
base.CalcSize();
_titleRect = new Rectangle(0, 0, Width, _titleHeight + BorderWidth);
if (_titleHeight == 0) return;
SizeF sf = CreateGraphics().MeasureString(Text, _titleFont);
_textRect = new RectangleF(0, (_titleRect.Height - sf.Height) / 2f, sf.Width, sf.Height);
switch (TextAlign)
{
case HorizontalAlignment.Left:
_textRect.X = BorderWidth;
break;
case HorizontalAlignment.Center:
_textRect.X = (Width - sf.Width) / 2f;
break;
case HorizontalAlignment.Right:
_textRect.X = Width - BorderWidth - sf.Width;
break;
}
}
private void SurPanel_Paint(object sender, PaintEventArgs e)
{
BufferedGraphicsContext current = BufferedGraphicsManager.Current;
BufferedGraphics bg = current.Allocate(e.Graphics, e.ClipRectangle);
Graphics g = bg.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Common.BACK_COLOR); //背景
if (Inside)
{
if (BorderWidth > 0)
g.DrawRectangle(BorderInsidePen, BorderRect);
}
else
{
if (BorderWidth > 0)
g.DrawRectangle(BorderOutsidePen, BorderRect);
}
if (_showTitle && _titleHeight > 0)
{
g.FillRectangle(Common.BLUE_BRUSH, _titleRect);
g.DrawString(Text, _titleFont, Common.FORE_BRUSH, _textRect);
}
bg.Render();
bg.Dispose();
}
}
}