Introduction

For many years I have used Chris Maunder's excellent Hyperlink control. Recently I needed a hyperlink that would send a notification to the parent dialog. So I started by adding this feature, and then I found that I had added other features too. Here is a list of the new features:

Parent notification - the parent window is optionally sent a notification message.

New browser window - you can optionally tell the browser to open a new window (same as if you had held down SHIFT key).

Background color - you can override default background color and set your own.

Disable tooltip - you can optionally disable tooltips for the hyperlink. This makes sense where the hyperlink only sends a notification to the parent window, but does not navigate.

Disable URL - optionally disable the URL. Again, this makes sense where the hyperlink only sends a notification to the parent window.

Dynamic resizing - you can set new window text at any time and now the control will be resized.

Use of IDC_HAND - the MFC hand cursor is used when it is available, before defaulting to the cursor from Winhelp.

GoToURL() is now static - now you can say CXHyperLink::GoToURL("http://www.somedomain.com").

XHYPERLINK_REPORT_ERROR define - by default this symbol is not defined, which will remove all the strings and message box from ReportError().

How to use

To integrate XHyperLink() into your app, you first need to add following files to your project:

XHyperLink.cpp

XHyperLink.h

Then use the resource editor to add a static control to your dialog, and use Class Wizard to attach a member variable to that control. Note that when adding the static control, you must name it something other than IDC_STATIC.

Next, include the header file XHyperLink.h in the dialog's header file. Then replace the CStatic definition with CXHyperLink. Now you are ready to start using XHyperLink.

Demo app

The XHyperLinkTest.exe demo shows how to use CXHyperLink. The first hyperlink sends an email:

The next hyperlink sends a notification to the parent dialog:

and the dialog displays a message box:

The last hyperlink shows how the text and URL may be dynamically changed:

Acknowledgments

Revision history

Version 1.0 - 2003 October 17

Initial public release.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

Share

About the Author

I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.

I wonder if this source code can be used in GNU3 project? Header has a "can not be sold" stipulation, which technically contradicts with GNU3. If any of the authors could come forward and clarify if I can use this control in GNU3-licensed project (or any other open source project for the same matter), I would appreciate it. I tried to email authors, but none of the emails I could find is active. I want to use it in VVCap (www.vvcap.net) project

In the downloaded code the call to SetUnderline() has no effect because theunderline state has been hard coded in the function declaration. Here are my changes to give you full control over your hyperlink appearance.

1) in the XHyperLink.h file comment out the [= ulHover] in the function declaration.

2) in the XHyperLink.cpp file set the m_nUnderline variable to your desired default state in the constructor.

4) in the SetUnderline() function implementation change the first if statement to the following:

if (m_nUnderline == nUnderline && nUnderline != 1)
return;

This forces an "Always Underline" call to set the underline font. Otherwise, an "Always" call will not underline if it follows a previous "hover" call.

After making these changes now you can call m_mycontrol.SetUnderline(state) and set unique text states for each hyperlink. Of course, if you omit these calls, then the default state you set in the constructor will determine the state for all controls.

It occurs to me that if the length of the url to be set is large than a certain number not only the hyperlink will cease to function but also the dialog in which the hyperlink control is embedded will disappear.

The URL is passed in a CString variable, which as far as the class isconcerned could contain all the data of a text file or HTML page if you so needed. The limit would only be imposed if you set a limit at the time youcreated the variable (in ClassWizard for example).

As far as the static control is concerned, the idea is to NOT try to place the URL in the static control, but rather a meaningful abbreviated version -- such as www.microsoft.com

Use SetWindowText to set the text in your control, and SetURL to set the actual URL (they can--and probably should--be two separate strings).

Also see my posting in this forum on customized tooltips for setting and displaying custom tooltips rather than the URL (this modification was made due to long URLS).