SurChoose.cs
6.2 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
{
/// <summary>
/// 选择控件
/// </summary>
[DefaultProperty("Text")]
[DefaultEvent("SelectedIndexChanged")]
public partial class SurChoose : BaseCtl
{
private RectangleF[] _btnRect;
private string[] _btnText;
private RectangleF[] _btnTextRect;
private int _indexOver = -1;
private int _index = -1;
private const int SPACE = 3;
/// <summary>
/// 选择控件
/// </summary>
public SurChoose()
{
InitializeComponent();
}
//==========事件==========
/// <summary>
/// SelectedIndex属性更改时发生
/// </summary>
[Browsable(true), Category(""), Description("SelectedIndex属性更改时发生")]
public event MyEvent.Changed SelectedIndexChanged;
//==========属性==========
/// <summary>
/// 当前选定项从零开始的索引
/// </summary>
[Browsable(false), Category(""), Description("当前选定项从零开始的索引")]
public int SelectedIndex
{
set
{
_index = value;
Refresh();
if (Enabled)
SelectedIndexChanged?.Invoke(this);
}
get => _index;
}
/// <summary>
/// 当前选定项的文本
/// </summary>
[Browsable(false), Category(""), Description("当前选定项的文本")]
public string SelectedText
{
set
{
if (_btnText != null)
{
int idx = Array.FindIndex(_btnText, s => s == value);
if (idx > -1) SelectedIndex = idx;
}
}
get
{
if (_btnText == null)
return "";
else if (_index < 0)
return "";
else
return _btnText[_index];
}
}
//==========私有==========
/// <summary>
/// 计算位置大小
/// </summary>
protected override void CalcSize()
{
base.CalcSize();
_btnText = Text.Split(',');
int count = _btnText.Length;
_btnRect = new RectangleF[count];
_btnTextRect = new RectangleF[count];
float w = Width - BorderWidth - BorderWidth;
float h = Height - BorderWidth - BorderWidth;
float ww = (w - SPACE * (count + 1)) / count;
float hh = h - SPACE - SPACE;
Graphics g = CreateGraphics();
SizeF sf;
for (int i = 0; i < count; i++)
{
_btnRect[i] = new RectangleF(BorderWidth + SPACE * (i + 1) + ww * i, BorderWidth + SPACE, ww, hh);
sf = g.MeasureString(_btnText[i], Font);
_btnTextRect[i] = new RectangleF(_btnRect[i].X + (_btnRect[i].Width - sf.Width) / 2, _btnRect[i].Y + (_btnRect[i].Height - sf.Height) / 2, sf.Width, sf.Height);
}
}
private void SurChoose_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 && Enabled)
{
if (BorderWidth > 0)
g.DrawRectangle(BorderInsidePen, BorderRect);
}
else
{
_indexOver = -1; //避免移动太快没有执行MouseMove
if (BorderWidth > 0)
g.DrawRectangle(BorderOutsidePen, BorderRect);
}
for (int i = 0; i < _btnRect.Length; i++)
{
if (Enabled)
{
if (i == _index)
g.FillRectangle(Common.BLUE_BRUSH, _btnRect[i]);
else if (i == _indexOver)
g.FillRectangle(Common.OVER_BRUSH, _btnRect[i]);
g.DrawString(_btnText[i], Font, Common.FORE_BRUSH, _btnTextRect[i]);
}
else
{
g.DrawString(_btnText[i], Font, Common.BORDER_BRUSH, _btnTextRect[i]);
}
}
bg.Render();
bg.Dispose();
}
private void SurChoose_Load(object sender, EventArgs e)
{
_index = -1;
}
private void SurChoose_MouseMove(object sender, MouseEventArgs e)
{
if (!Enabled) return;
int move = -1;
for (int i = 0; i < _btnRect.Length; i++)
{
if (_btnRect[i].Contains(e.Location))
{
move = i;
break;
}
}
if (_indexOver != move)
{
_indexOver = move;
Refresh();
}
}
private void SurChoose_MouseDown(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Button == MouseButtons.Left)
{
if (_indexOver > -1)
{
_index = _indexOver;
Refresh();
}
}
}
private void SurChoose_MouseUp(object sender, MouseEventArgs e)
{
if (!Enabled) return;
if (e.Button == MouseButtons.Left)
{
if (_indexOver > -1 && _index > -1)
{
SelectedIndexChanged?.Invoke(this);
}
}
}
}
}