88
99namespace ExcelDna . IntelliSense
1010{
11+ // CONSIDER: Maybe some ideas from here: http://codereview.stackexchange.com/questions/55916/lightweight-rich-link-label
1112
1213 // TODO: Drop shadow: http://stackoverflow.com/questions/16493698/drop-shadow-on-a-borderless-winform
1314 class ToolTipForm : Form
@@ -19,10 +20,14 @@ class ToolTipForm : Form
1920 int _left ;
2021 int _top ;
2122 Brush _textBrush ;
23+ Brush _linkBrush ;
2224 Pen _borderPen ;
2325 Pen _borderLightPen ;
24- private ToolTip tipDna ;
26+ ToolTip tipDna ;
2527 Dictionary < FontStyle , Font > _fonts ;
28+ Rectangle _linkRect ;
29+ bool _linkActive ;
30+ string _linkAddress ;
2631
2732 public ToolTipForm ( IntPtr hwndOwner )
2833 {
@@ -40,6 +45,7 @@ public ToolTipForm(IntPtr hwndOwner)
4045
4146 } ;
4247 _textBrush = new SolidBrush ( Color . FromArgb ( 68 , 68 , 68 ) ) ;
48+ _linkBrush = new SolidBrush ( Color . Blue ) ;
4349 _borderPen = new Pen ( Color . FromArgb ( 195 , 195 , 195 ) ) ;
4450 _borderLightPen = new Pen ( Color . FromArgb ( 225 , 225 , 225 ) ) ;
4551 //Win32Helper.SetParent(this.Handle, hwndOwner);
@@ -144,6 +150,7 @@ protected override bool ShowWithoutActivation
144150
145151 // Sometimes has Invalid Handle error when calling base.CreateParams (called from Invalidate() for some reason)
146152 CreateParams _createParams ;
153+
147154 protected override CreateParams CreateParams
148155 {
149156 get
@@ -199,11 +206,32 @@ protected override void OnPaint(PaintEventArgs e)
199206 int lineHeight = 16 ;
200207 foreach ( var run in line )
201208 {
202- var font = _fonts [ run . Style ] ;
209+ // We support only a single link, for now
210+
211+ Font font ;
212+ Brush brush ;
213+ if ( run . IsLink && _linkActive )
214+ {
215+ font = _fonts [ FontStyle . Underline ] ;
216+ brush = _linkBrush ;
217+ }
218+ else
219+ {
220+ font = _fonts [ run . Style ] ;
221+ brush = _textBrush ;
222+ }
223+
203224 // TODO: Empty strings are a problem....
204225 var text = run . Text == "" ? " " : run . Text ;
205226
206- DrawString ( e . Graphics , _textBrush , ref layoutRect , out textSize , format , text , font ) ;
227+ DrawString ( e . Graphics , brush , ref layoutRect , out textSize , format , text , font ) ;
228+
229+ if ( run . IsLink )
230+ {
231+ _linkRect = new Rectangle ( layoutRect . X - textSize . Width , layoutRect . Y , textSize . Width , textSize . Height ) ;
232+ _linkAddress = run . LinkAddress ;
233+ }
234+
207235 totalWidth += textSize . Width ;
208236 lineHeight = Math . Max ( lineHeight , textSize . Height ) ;
209237 }
@@ -291,6 +319,8 @@ void InitializeComponent()
291319 this . Name = "ToolTipForm" ;
292320 this . ShowInTaskbar = false ;
293321 this . tipDna . SetToolTip ( this , "IntelliSense by Excel-DNA" ) ;
322+ this . MouseClick += new System . Windows . Forms . MouseEventHandler ( this . ToolTipForm_MouseClick ) ;
323+ this . MouseMove += new System . Windows . Forms . MouseEventHandler ( this . ToolTipForm_MouseMove ) ;
294324 this . ResumeLayout ( false ) ;
295325
296326 }
@@ -308,5 +338,36 @@ public Win32Window(IntPtr handle)
308338 Handle = handle ;
309339 }
310340 }
341+
342+ void ToolTipForm_MouseMove ( object sender , MouseEventArgs e )
343+ {
344+ var inLink = _linkRect . Contains ( e . Location ) ;
345+ if ( ( inLink && ! _linkActive ) ||
346+ ( ! inLink && _linkActive ) )
347+ {
348+ _linkActive = ! _linkActive ;
349+ Invalidate ( ) ;
350+ }
351+ }
352+
353+ void ToolTipForm_MouseClick ( object sender , MouseEventArgs e )
354+ {
355+ var inLink = _linkRect . Contains ( e . Location ) ;
356+ if ( inLink )
357+ {
358+ LaunchLink ( _linkAddress ) ;
359+ }
360+ }
361+
362+ void LaunchLink ( string address )
363+ {
364+ // Check :
365+ // http://stackoverflow.com/questions/11076952/open-chm-file-at-specific-page-topic-using-command-line-arguments
366+ // http://stackoverflow.com/questions/27556808/excel-2010-help-on-this-function-does-not-launch-hh-exe-with-mapid-parameter/27571805#27571805
367+ // http://www.help-info.de/en/Help_Info_HTMLHelp/hh_command.htm
368+ // https://support.microsoft.com/en-us/kb/224816
369+
370+ Process . Start ( address ) ;
371+ }
311372 }
312373}
0 commit comments