6
6
7
7
public class ToolTip : Component
8
8
{
9
+ private const int SCREEN_MIN_MARGIN = 2 ;
10
+ private const int SHADOW_STRENGTH = 12 ;
11
+ private const float ALPHA_TIME_SEC = .5f ;
12
+ private const float ALPHA_SPEED = 255f / ALPHA_TIME_SEC ;
13
+
9
14
internal static ToolTip instance ;
10
15
internal int alphaState ; // 0: none; 1: 0 to 255; 2 : alphaWait; 3: 255 to 0
11
-
16
+
12
17
private static readonly Queue < ToolTip > items = new Queue < ToolTip > ( ) ;
13
18
14
- private float alphaF ;
15
- private float alphaWait ; // seconds. Wait time before hide.
19
+ private readonly Pen borderPen = new Pen ( Color . Black ) ;
20
+ private float alphaF ;
21
+ private float alphaWait ; // seconds. Wait time before hide.
16
22
private Control control ;
17
- private int initialDelay = 1000 ;
18
- private Point location ;
19
- private string text ;
20
- private float waitToShow ;
23
+ private int initialDelay = 1000 ;
24
+ private Point location ;
25
+ private string text ;
26
+ private float waitToShow ;
21
27
22
28
public ToolTip ( )
23
29
{
24
- BackColor = Color . White ;
25
- BorderColor = Color . FromArgb ( 118 , 118 , 118 ) ;
26
- Font = new Font ( "Arial" , 12 ) ;
27
- ForeColor = Color . FromArgb ( 118 , 118 , 118 ) ;
28
- Padding = new Padding ( 4 ) ;
29
- TextAlign = HorizontalAlignment . Center ;
30
+ BackColor = SystemColors . Info ;
31
+ ForeColor = SystemColors . uwfInfoText ;
32
+
33
+ uwfBorderColor = Color . FromArgb ( 118 , 118 , 118 ) ;
34
+ uwfFont = new Font ( "Arial" , 12 ) ;
35
+ uwfPadding = new Padding ( 8 , 2 , 8 , 2 ) ;
30
36
}
31
37
32
38
public Color BackColor { get ; set ; }
33
- public Color BorderColor { get ; set ; }
34
- public Font Font { get ; set ; }
35
39
public Color ForeColor { get ; set ; }
36
40
public int InitialDelay
37
41
{
38
42
get { return initialDelay ; }
39
- set
40
- {
41
- initialDelay = MathHelper . Clamp ( value , 0 , 32767 ) ;
42
- }
43
+ set { initialDelay = MathHelper . Clamp ( value , 0 , 32767 ) ; }
43
44
}
44
- public Padding Padding { get ; set ; }
45
- public HorizontalAlignment TextAlign { get ; set ; }
46
45
46
+ internal Color uwfBorderColor { get ; set ; }
47
+ internal Font uwfFont { get ; set ; }
48
+ internal Padding uwfPadding { get ; set ; }
49
+
47
50
public void SetToolTip ( Control control , string caption )
48
51
{
49
52
this . control = control ;
@@ -86,7 +89,7 @@ internal static void OnPaint(PaintEventArgs e)
86
89
87
90
if ( instance != null )
88
91
{
89
- instance . Paint ( e ) ;
92
+ instance . PaintInternal ( e ) ;
90
93
91
94
TryHideInstance ( ) ;
92
95
}
@@ -133,86 +136,102 @@ private void control_Disposed(object sender, EventArgs e)
133
136
{
134
137
ForceHideInstance ( ) ;
135
138
}
136
- private void Paint ( PaintEventArgs e )
139
+
140
+ private void PaintInternal ( PaintEventArgs e )
137
141
{
138
142
if ( waitToShow > 0 )
139
143
{
140
144
waitToShow -= 1000 * swfHelper . GetDeltaTime ( ) ;
141
145
return ;
142
146
}
147
+
148
+ UpdateAlpha ( ) ;
143
149
144
150
var g = e . Graphics ;
145
151
146
- var size = g . MeasureString ( text , Font ) + new SizeF ( 16 , 4 ) ;
147
-
148
- Point loc = location ;
149
-
150
- if ( loc . X + size . Width + 2 > Screen . PrimaryScreen . WorkingArea . Width )
151
- loc = new Point ( Screen . PrimaryScreen . WorkingArea . Width - ( int ) size . Width - 2 , loc . Y ) ;
152
- if ( loc . Y + size . Height + 2 > Screen . PrimaryScreen . WorkingArea . Height )
153
- loc = new Point ( loc . X , Screen . PrimaryScreen . WorkingArea . Height - ( int ) size . Height - 2 ) ;
154
-
155
- int shadowAlpha = 12 - 255 + ( int ) alphaF ;
152
+ var screenSize = Screen . PrimaryScreen . WorkingArea ;
153
+ var textSize = g . MeasureString ( text , uwfFont ) ;
154
+
155
+ var renderingW = ( int ) textSize . Width + uwfPadding . Horizontal ;
156
+ var renderingH = ( int ) textSize . Height + uwfPadding . Vertical ;
157
+ var renderingX = MathHelper . Clamp ( location . X , 0 , screenSize . Width - SCREEN_MIN_MARGIN - renderingW ) ;
158
+ var renderingY = MathHelper . Clamp ( location . Y , 0 , screenSize . Height - SCREEN_MIN_MARGIN - renderingH ) ;
159
+
160
+ var alpha = MathHelper . Clamp ( ( int ) alphaF , 1 , 255 ) ;
161
+
162
+ // Shadow.
163
+ var shadowAlpha = SHADOW_STRENGTH / ( 255 / alpha ) ;
156
164
var shadowColor = Color . FromArgb ( shadowAlpha , 64 , 64 , 64 ) ;
157
-
158
- int stringHeight = ( int ) size . Height ;
159
-
160
- var locX = loc . X ;
161
- var locY = loc . Y ;
162
-
163
- g . uwfFillRectangle ( shadowColor , locX + 1 , locY + 1 , size . Width + 3 , stringHeight + 3 ) ;
164
- g . uwfFillRectangle ( shadowColor , locX + 2 , locY + 2 , size . Width + 1 , stringHeight + 1 ) ;
165
- g . uwfFillRectangle ( shadowColor , locX + 3 , locY + 3 , size . Width - 1 , stringHeight - 1 ) ;
166
-
167
- var borderColor = Color . FromArgb ( ( int ) alphaF , BorderColor ) ;
168
- var textColor = Color . FromArgb ( ( int ) alphaF , ForeColor ) ;
169
- var textFont = Font ;
170
-
171
- g . uwfFillRectangle ( Color . FromArgb ( ( int ) alphaF , BackColor ) , locX , locY , size . Width , stringHeight ) ;
172
- g . DrawRectangle ( new Pen ( borderColor ) , locX , locY , size . Width , stringHeight ) ;
173
- g . uwfDrawString (
174
- text ,
175
- textFont ,
176
- textColor ,
177
- locX + Padding . Left ,
178
- locY + Padding . Top ,
179
- size . Width - Padding . Bottom ,
180
- stringHeight - Padding . Right ,
181
- TextAlign ) ;
182
-
165
+
166
+ g . uwfFillRectangle ( shadowColor , renderingX + 1 , renderingY + 1 , renderingW + 3 , renderingH + 3 ) ;
167
+ g . uwfFillRectangle ( shadowColor , renderingX + 2 , renderingY + 2 , renderingW + 1 , renderingH + 1 ) ;
168
+ g . uwfFillRectangle ( shadowColor , renderingX + 3 , renderingY + 3 , renderingW - 1 , renderingH - 1 ) ;
169
+
170
+ // Background.
171
+ var backColor = Color . FromArgb ( alpha , BackColor ) ;
172
+
173
+ g . uwfFillRectangle ( backColor , renderingX , renderingY , renderingW , renderingH ) ;
174
+
175
+ // Border.
176
+ borderPen . Color = Color . FromArgb ( alpha , uwfBorderColor ) ;
177
+
178
+ g . DrawRectangle ( borderPen , renderingX , renderingY , renderingW , renderingH ) ;
179
+
180
+ // Text.
181
+ var textColor = Color . FromArgb ( alpha , ForeColor ) ;
182
+ var textFont = uwfFont ;
183
+ var textX = renderingX + uwfPadding . Left ;
184
+ var textY = renderingY + uwfPadding . Top ;
185
+ var textWidth = renderingW - uwfPadding . Horizontal ;
186
+ var textHeight = renderingH - uwfPadding . Vertical ;
187
+
188
+ g . uwfDrawString ( text , textFont , textColor , textX , textY , textWidth , textHeight ) ;
189
+ }
190
+
191
+ private void UpdateAlpha ( )
192
+ {
183
193
switch ( alphaState )
184
194
{
185
195
case 0 :
186
196
alphaState = 1 ;
187
197
break ;
198
+
188
199
case 1 :
200
+ // 0 to 255.
189
201
if ( alphaF < 255 )
190
- alphaF += swfHelper . GetDeltaTime ( ) * 510f ; // .5f sec.
202
+ alphaF += swfHelper . GetDeltaTime ( ) * ALPHA_SPEED ;
191
203
else
192
204
{
193
205
alphaF = 255 ;
194
206
alphaState = 2 ;
195
207
}
208
+
196
209
break ;
210
+
197
211
case 2 :
212
+ // Wait.
198
213
if ( alphaWait > 0 )
199
214
alphaWait -= swfHelper . GetDeltaTime ( ) ;
200
215
else
201
216
{
202
217
alphaWait = 0 ;
203
218
alphaState = 3 ;
204
219
}
220
+
205
221
break ;
222
+
206
223
case 3 :
224
+ // 255 to 0.
207
225
if ( alphaF > 0 )
208
- alphaF -= swfHelper . GetDeltaTime ( ) * 510f ; // .5f sec.
226
+ alphaF -= swfHelper . GetDeltaTime ( ) * ALPHA_SPEED ;
209
227
else
210
228
{
211
229
alphaF = 0 ;
212
230
instance = null ;
213
231
}
232
+
214
233
break ;
215
234
}
216
235
}
217
236
}
218
- }
237
+ }
0 commit comments