FlatText.cs
5.8 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
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.Theme
{
[DefaultValue("Text")]
[DefaultEvent("TextChanged")]
public partial class FlatText : FlatBase
{
private bool _find;
private bool _down;
private bool _over;
private Rectangle _borderRect = new Rectangle();
private Rectangle _findRect = new Rectangle();
public delegate void FindClickDelegate(object sender, string str);
private const int BORDER_W = 2;
//private const int FIND_WIDTH = 24;
private readonly SolidBrush FIND_OVER = new SolidBrush(Common.OVER_COLOR);
private readonly SolidBrush FIND_DOWN = new SolidBrush(Common.BLUE_COLOR);
private readonly Pen FIND_FORE = new Pen(Common.FORE_COLOR, 2);
public FlatText()
{
InitializeComponent();
Font = new Font("宋体", 9f);
Txt.BackColor = Common.BACK_COLOR;
Txt.ForeColor = Common.FORE_COLOR;
CalcSize();
}
[Browsable(true), Category(""), Description("查找按钮"), DefaultValue(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public bool FindButton
{
set
{
_find = value;
CalcSize();
Refresh();
}
get
{
return _find;
}
}
[Browsable(true), Category(""), Description("显示的文本"), DefaultValue("")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get => base.Text;
set
{
base.Text = value;
Txt.Text = value;
}
}
[Browsable(true), Category(""), Description("显示的文本的字体")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override Font Font
{
set
{
base.Font = value;
Txt.Font = value;
CalcSize();
Refresh();
}
get
{
return base.Font;
}
}
[Browsable(true), Category(""), Description("查找按钮单击事件")]
public event FindClickDelegate FindClick;
[Browsable(true), Category(""), Description("文本改变事件")]
public new event EventHandler TextChanged;
protected override void CalcSize()
{
Txt.Left = BORDER_W + 3;
Txt.Width = Width - BORDER_W * 2 - 6 - (_find ? Common.RBUTTON_W : 0);
Txt.Top = (Height - Txt.Height) / 2;
_borderRect = new Rectangle(BORDER_W / 2, BORDER_W / 2, Width - BORDER_W, Height - BORDER_W);
if (_find)
{
_findRect.X = Width - BORDER_W - Common.RBUTTON_W;
_findRect.Y = BORDER_W;
_findRect.Width = Common.RBUTTON_W;
_findRect.Height = Height - BORDER_W * 2;
}
}
private void FlatText_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.Clear(Common.BACK_COLOR);
if (Inside)
{
if(_down)
e.Graphics.FillRectangle(FIND_DOWN, _findRect);
else if (_over)
e.Graphics.FillRectangle(FIND_OVER, _findRect);
e.Graphics.DrawRectangle(Common.BLUE_PEN, _borderRect);
e.Graphics.DrawLine(Common.BLUE_PEN, _findRect.X, _findRect.Y, _findRect.X, _findRect.Bottom);
//e.Graphics.DrawEllipse(FIND_FOCUS, _find.X + 10, _find.Y + _find.Height / 2f - 8, 9, 9);
//e.Graphics.DrawLine(FIND_FOCUS, _find.X + 11, _find.Y + _find.Height / 2f, _find.X + 5, _find.Y + _find.Height / 2f + 6);
}
else
{
e.Graphics.DrawRectangle(Common.BORDER_PEN, _borderRect);
}
if (_find)
{
e.Graphics.DrawEllipse(FIND_FORE, _findRect.X + 10, _findRect.Y + _findRect.Height / 2f - 8, 9, 9);
e.Graphics.DrawLine(FIND_FORE, _findRect.X + 11, _findRect.Y + _findRect.Height / 2f, _findRect.X + 5, _findRect.Y + _findRect.Height / 2f + 6);
}
}
private void FlatText_MouseDown(object sender, MouseEventArgs e)
{
if (_find && _over)
{
_down = true;
Refresh();
}
}
private void FlatText_MouseUp(object sender, MouseEventArgs e)
{
if (_find)
{
_down = false;
Refresh();
if (_over) FindClick?.Invoke(this, Text);
}
}
private void FlatText_MouseMove(object sender, MouseEventArgs e)
{
bool bln = _findRect.Contains(e.Location);
if (bln != _over)
{
_over = bln;
Refresh();
}
}
private void Txt_MouseEnter(object sender, EventArgs e)
{
Inside = true;
Refresh();
}
private void Txt_TextChanged(object sender, EventArgs e)
{
if (Text == Txt.Text) return;
Text = Txt.Text;
TextChanged?.Invoke(this, new EventArgs());
}
}
}