22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Drawing ;
5+ using System . Drawing . Drawing2D ;
56using System . Linq ;
67using System . Text ;
78using System . Windows . Forms ;
@@ -20,12 +21,27 @@ class ToolTipForm : Form
2021 Win32Window _owner ;
2122 int _left ;
2223 int _top ;
24+ Brush _textBrush ;
25+ Pen _borderPen ;
26+ Dictionary < FontStyle , Font > _fonts ;
2327
2428 public ToolTipForm ( IntPtr hwndOwner )
2529 {
2630 Debug . Assert ( hwndOwner != IntPtr . Zero ) ;
2731 InitializeComponent ( ) ;
2832 _owner = new Win32Window ( hwndOwner ) ;
33+ // CONSIDER: Maybe make a more general solution that lazy-loads as needed
34+ _fonts = new Dictionary < FontStyle , Font >
35+ {
36+ { FontStyle . Regular , new Font ( "Segoe UI" , 9 , FontStyle . Regular ) } ,
37+ { FontStyle . Bold , new Font ( "Segoe UI" , 9 , FontStyle . Bold ) } ,
38+ { FontStyle . Italic , new Font ( "Segoe UI" , 9 , FontStyle . Italic ) } ,
39+ { FontStyle . Underline , new Font ( "Segoe UI" , 9 , FontStyle . Underline ) } ,
40+ { FontStyle . Bold | FontStyle . Italic , new Font ( "Segoe UI" , 9 , FontStyle . Bold | FontStyle . Italic ) } ,
41+
42+ } ;
43+ _textBrush = new SolidBrush ( Color . FromArgb ( 68 , 68 , 68 ) ) ;
44+ _borderPen = new Pen ( Color . FromArgb ( 205 , 205 , 205 ) ) ;
2945 //Win32Helper.SetParent(this.Handle, hwndOwner);
3046
3147 // _owner = new NativeWindow();
@@ -63,18 +79,20 @@ public void ShowToolTip()
6379 public void ShowToolTip ( FormattedText text , int left , int top )
6480 {
6581 _text = text ;
82+ _left = left ;
83+ _top = top ;
6684 if ( ! Visible )
6785 {
6886 Debug . Print ( $ "Showing ToolTipForm: { _text . ToString ( ) } ") ;
87+ Left = _left ;
88+ Top = _top ;
6989 ShowToolTip ( ) ;
7090 }
7191 else
7292 {
7393 Debug . Print ( $ "Invalidating ToolTipForm: { _text . ToString ( ) } ") ;
7494 Invalidate ( ) ;
7595 }
76- _left = left ;
77- _top = top ;
7896 }
7997
8098 public void MoveToolTip ( int left , int top )
@@ -88,7 +106,8 @@ public IntPtr OwnerHandle
88106 {
89107 get
90108 {
91- if ( _owner == null ) return IntPtr . Zero ;
109+ if ( _owner == null )
110+ return IntPtr . Zero ;
92111 return _owner . Handle ;
93112 }
94113 set
@@ -99,7 +118,7 @@ public IntPtr OwnerHandle
99118 //Win32Helper.SetParent(this.Handle, value);
100119 if ( Visible )
101120 {
102- // Rather just change Owner....
121+ // CONSIDER: Rather just change Owner....
103122 Hide ( ) ;
104123 ShowToolTip ( ) ;
105124 }
@@ -152,11 +171,12 @@ protected override void OnHandleDestroyed(EventArgs e)
152171
153172 protected override void OnPaint ( PaintEventArgs e )
154173 {
155- Debug . Print ( $ "Painting ToolTipForm: { _text . ToString ( ) } ") ;
156- const int leftPadding = 3 ;
174+ Debug . Assert ( _left != 0 || _top != 0 ) ;
175+ Logger . Display . Verbose ( $ "ToolTipForm OnPaint: { _text . ToString ( ) } @ ({ _left } ,{ _top } )") ;
176+ const int leftPadding = 6 ;
157177 const int linePadding = 2 ;
158178 const int widthPadding = 10 ;
159- const int heightPadding = 7 ;
179+ const int heightPadding = 3 ;
160180
161181 base . OnPaint ( e ) ;
162182 List < int > lineWidths = new List < int > ( ) ;
@@ -178,16 +198,13 @@ protected override void OnPaint(PaintEventArgs e)
178198 int lineHeight = 0 ;
179199 foreach ( var run in line )
180200 {
181- using ( var font = new Font ( "Segoe UI" , 9 , run . Style ) ) // TODO: Look this up or something ...?
182- {
183- // TODO: Empty strings are a problem....
184- var text = run . Text == "" ? " " : run . Text ;
185-
186- // TODO: Find the color SystemBrushes.ControlDarkDark
187- DrawString ( e . Graphics , SystemBrushes . WindowFrame , ref layoutRect , out textSize , format , text , font ) ;
188- totalWidth += textSize . Width ;
189- lineHeight = Math . Max ( lineHeight , textSize . Height ) ;
190- }
201+ var font = _fonts [ run . Style ] ;
202+ // TODO: Empty strings are a problem....
203+ var text = run . Text == "" ? " " : run . Text ;
204+
205+ DrawString ( e . Graphics , _textBrush , ref layoutRect , out textSize , format , text , font ) ;
206+ totalWidth += textSize . Width ;
207+ lineHeight = Math . Max ( lineHeight , textSize . Height ) ;
191208 }
192209 lineWidths . Add ( totalWidth ) ;
193210 totalWidth = 0 ;
@@ -196,7 +213,27 @@ protected override void OnPaint(PaintEventArgs e)
196213 }
197214 }
198215
199- SetBounds ( _left , _top , lineWidths . Max ( ) + widthPadding , totalHeight + heightPadding ) ;
216+ var width = lineWidths . Max ( ) + widthPadding ;
217+ var height = totalHeight + heightPadding ;
218+ //if (Left != _left ||
219+ // Top != _top ||
220+ // Width != width ||
221+ // Height != height)
222+ //{
223+ //var pRegion = Win32Helper.CreateRoundRectRgn(0, 0, width - 1, height - 1, 2, 2);
224+ //try
225+ //{
226+ // Region = Region.FromHrgn(pRegion);
227+ //}
228+ //finally
229+ //{
230+ // Win32Helper.DeleteObject(pRegion);
231+ //}
232+ SetBounds ( _left , _top , width , height ) ;
233+ //}
234+
235+ DrawRoundedRectangle ( e . Graphics , new RectangleF ( 0 , 0 , Width - 1 , Height - 1 ) , 2 , 2 ) ;
236+
200237// Size = new Size(lineWidths.Max() + widthPadding, totalHeight + heightPadding);
201238 }
202239
@@ -224,6 +261,36 @@ void DrawString(Graphics g, Brush brush, ref Rectangle rect, out Size used,
224261 }
225262 }
226263
264+
265+ public void DrawRoundedRectangle ( Graphics g , RectangleF r , float radiusX , float radiusY )
266+ {
267+ var oldMode = g . SmoothingMode ;
268+ g . SmoothingMode = SmoothingMode . AntiAlias ;
269+
270+ // Draw the truncated rectangle in white.
271+ using ( GraphicsPath path = new GraphicsPath ( ) )
272+ {
273+ path . StartFigure ( ) ;
274+
275+ // if (radiusX <= 0.0F || radiusY <= 0.0F)
276+ {
277+ path . AddRectangle ( r ) ;
278+ }
279+ //else
280+ {
281+ //arcs work with diameters (radius * 2)
282+ PointF d = new PointF ( Math . Min ( radiusX * 2 , r . Width ) , Math . Min ( radiusY * 2 , r . Height ) ) ;
283+ path . AddArc ( r . X , r . Y , d . X , d . Y , 180 , 90 ) ;
284+ path . AddArc ( r . Right - d . X , r . Y , d . X , d . Y , 270 , 90 ) ;
285+ path . AddArc ( r . Right - d . X , r . Bottom - d . Y , d . X , d . Y , 0 , 90 ) ;
286+ path . AddArc ( r . X , r . Bottom - d . Y , d . X , d . Y , 90 , 90 ) ;
287+ }
288+ path . CloseFigure ( ) ;
289+ g . DrawPath ( _borderPen , path ) ;
290+ }
291+ g . SmoothingMode = oldMode ;
292+ }
293+
227294 void InitializeComponent ( )
228295 {
229296 this . components = new System . ComponentModel . Container ( ) ;
@@ -264,7 +331,7 @@ void InitializeComponent()
264331 this . Controls . Add ( this . labelDna ) ;
265332 this . DoubleBuffered = true ;
266333 this . ForeColor = System . Drawing . Color . DimGray ;
267- this . FormBorderStyle = System . Windows . Forms . FormBorderStyle . FixedToolWindow ;
334+ this . FormBorderStyle = System . Windows . Forms . FormBorderStyle . None ;
268335 this . Name = "ToolTipForm" ;
269336 this . ShowInTaskbar = false ;
270337 this . ResumeLayout ( false ) ;
0 commit comments