66
77 public class ToolTip : Component
88 {
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+
914 internal static ToolTip instance ;
1015 internal int alphaState ; // 0: none; 1: 0 to 255; 2 : alphaWait; 3: 255 to 0
11-
16+
1217 private static readonly Queue < ToolTip > items = new Queue < ToolTip > ( ) ;
1318
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.
1622 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 ;
2127
2228 public ToolTip ( )
2329 {
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 ) ;
3036 }
3137
3238 public Color BackColor { get ; set ; }
33- public Color BorderColor { get ; set ; }
34- public Font Font { get ; set ; }
3539 public Color ForeColor { get ; set ; }
3640 public int InitialDelay
3741 {
3842 get { return initialDelay ; }
39- set
40- {
41- initialDelay = MathHelper . Clamp ( value , 0 , 32767 ) ;
42- }
43+ set { initialDelay = MathHelper . Clamp ( value , 0 , 32767 ) ; }
4344 }
44- public Padding Padding { get ; set ; }
45- public HorizontalAlignment TextAlign { get ; set ; }
4645
46+ internal Color uwfBorderColor { get ; set ; }
47+ internal Font uwfFont { get ; set ; }
48+ internal Padding uwfPadding { get ; set ; }
49+
4750 public void SetToolTip ( Control control , string caption )
4851 {
4952 this . control = control ;
@@ -86,7 +89,7 @@ internal static void OnPaint(PaintEventArgs e)
8689
8790 if ( instance != null )
8891 {
89- instance . Paint ( e ) ;
92+ instance . PaintInternal ( e ) ;
9093
9194 TryHideInstance ( ) ;
9295 }
@@ -133,86 +136,102 @@ private void control_Disposed(object sender, EventArgs e)
133136 {
134137 ForceHideInstance ( ) ;
135138 }
136- private void Paint ( PaintEventArgs e )
139+
140+ private void PaintInternal ( PaintEventArgs e )
137141 {
138142 if ( waitToShow > 0 )
139143 {
140144 waitToShow -= 1000 * swfHelper . GetDeltaTime ( ) ;
141145 return ;
142146 }
147+
148+ UpdateAlpha ( ) ;
143149
144150 var g = e . Graphics ;
145151
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 ) ;
156164 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+ {
183193 switch ( alphaState )
184194 {
185195 case 0 :
186196 alphaState = 1 ;
187197 break ;
198+
188199 case 1 :
200+ // 0 to 255.
189201 if ( alphaF < 255 )
190- alphaF += swfHelper . GetDeltaTime ( ) * 510f ; // .5f sec.
202+ alphaF += swfHelper . GetDeltaTime ( ) * ALPHA_SPEED ;
191203 else
192204 {
193205 alphaF = 255 ;
194206 alphaState = 2 ;
195207 }
208+
196209 break ;
210+
197211 case 2 :
212+ // Wait.
198213 if ( alphaWait > 0 )
199214 alphaWait -= swfHelper . GetDeltaTime ( ) ;
200215 else
201216 {
202217 alphaWait = 0 ;
203218 alphaState = 3 ;
204219 }
220+
205221 break ;
222+
206223 case 3 :
224+ // 255 to 0.
207225 if ( alphaF > 0 )
208- alphaF -= swfHelper . GetDeltaTime ( ) * 510f ; // .5f sec.
226+ alphaF -= swfHelper . GetDeltaTime ( ) * ALPHA_SPEED ;
209227 else
210228 {
211229 alphaF = 0 ;
212230 instance = null ;
213231 }
232+
214233 break ;
215234 }
216235 }
217236 }
218- }
237+ }
0 commit comments