CHLinkCtrl v1.43
Welcome to CHLinkCtrl, A freeware MFC class that supports Internet Hyperlinks
from standard Windows applications just like they are displayed in Web browsers.
Copyright
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
Usage
- First off before you use the CHLinkCtrl class in your code, the experienced
MFC developer might be asking the question of why he or she should not just
use the built in MFC classes of CLinkCtrl or CMFCLinkCtrl? The answer is that
the built in MFC CLinkCtrl class is a wrapper for the XP or later SysLink control
and this requires your app to require the v6 ComCtrl32.dll which technically
is only supported for Unicode builds of your app and not ASCII. Alternatively
if you are targeting Visual Studio 2008 + the MFC Feature pack or later, then
you could and probably should just use the built in CMFCLinkCtrl class. The
issue with this class is that using these CMFC* classes pulls in all the MFC
Feature pack classes and bloats your exe quite a lot if you statically link
to MFC. This issue continues in newer versions of VC. So if either of these
constraints are an issue, then you might to consider using my CHLinkCtrl classes.
Also bear in mind that the CHLinkCtrl classes use the default per user internet
explorer colors for hyperlinks from the registry while the CMFCLinkCtrl does
not.
- As of v1.36, the classes are now designed for VC 2017 or later. They will
not compile on earlier releases of VC.
- To use the class in your code include HLinkCtrl.cpp in your project and
#include HLinkCtrl.h in which ever of your modules needs to make calls to the
class.
- You will need to define WINVER defined as >= 0x0500 and _WIN32_WINNT
defined as >= 0x0500 in your project.
- Next associate a CStatic instance with the static you want to turn into
a hyperlink using class wizard.
- Then change the declaration of the variable from CStatic to CHLinkCtrl in
your dialogs header file.
- In your dialogs OnInitDialog function call the SetHyperLink with the URL
you want to link to as follows:
BOOL CHLinkTestDlg::OnInitDialog()
{
//let the base class do its thing
__super::OnInitDialog();
m_ctrlWeb.SetHyperLink(_T("http://www.naughter.com"));
return TRUE;
}
- Now when you run the program you will notice that the icon changes to a
hand when you move over the edit controls and when you click on the text the
URL will be executed. The demo program shows how to link to web pages as well
as mailto links, in fact any URL such as "file://", "ftp://"
etc can be used.
History
v1.43 (21 March 2022)
- Updated copyright details.
- Updated the code to use C++ uniform initialization for all variable
declarations.
v1.42 (27 November 2021)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.41 (13 April 2020)
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.40 (1 March 2020)
- Updated copyright details.
- Fixed more Clang-Tidy static code analysis warnings in the code.
v1.39 (19 December 2019)
- Fixed various Clang-Tidy static code analysis warnings in the code.
v1.38 (11 September 2019)
- Replaced enum with enum class throughout the code
- Fixed a number of compiler warnings when the code is compiled with VS
2019 Preview
v1.37 (2 June 2019)
- Updated copyright details.
- Updated the code to clean compile on VC 2019
v1.36 (6 August 2018)
- Fixed a number of C++ core guidelines compiler warnings. These changes
mean that the code will now only compile on VC 2017 or later.
- Replaced enum with enum class throughout the codebase
v1.35 (17 February 2018)
- Updated copyright details.
- Reworked the class to use STN_CLICKED notifications for handling the
hyperlink click instead of via OnLButtonUp. Please note that the control now
requires the SS_NOTIFY style to be set. Thanks to "sen" for reporting this
issue.
- Added SAL annotations to all the code
v1.34 (15 October 2017)
- Updated copyright details.
- Replaced NULL with nullptr throughout the codebase. This means that the
code now requires VC 2010 at a minimum to compile.
- Removed some unnecessary defines from HLinkCtrl.cpp.
- Replaced LONG with LSTATUS for all registry functions.
- Replaced raw registry API calls throughout the code with ATL::CRegKey
class.
- Replaced "BOOL" values throughout the code with "bool".
- Replaced CString::operator LPC*STR() calls with CString::GetString
calls.
v1.33 (22 November 2015)
- Verified the code compiles cleanly on VC 2015.
- Updated the sample apps main icon.
v1.32 (18 January 2015)
- Updated copyright details.
- Updated the code and sample app to clean compile on VC 2013
v1.31 (28 June 2013)
- Updated the sample app in debug mode to draw a hatched background to verify
that the hyperlink class works correctly where the parent window has a custom
drawn background.
v1.30 (23 June 2013)
- Updated copyright details.
- Updated the sample app to clean compile on VC 2010 and later.
- Made a number of Getter methods of the CHLinkCtrl class const.
- Updated the zip file to include the correct project files. Thanks to "Franc"
for reporting this issue.
v1.29 (22 December 2008)
- The code now uses TrackMouseEvent to implement support for highlighting
instead of capturing the mouse.
v1.28 (22 December 2008)
- Updated copyright details
- Removed VC 6 style AppWizard comments from the code
- The code has now been updated to support VC 2005 or later only
- Code now compiles cleanly using Code Analysis (/analyze)
- Made the "Open" method virtual
- The verb passed to ShellExecute can now be customized.
- Notifications for the static control are now enabled via a WM_NCHITTEST
message handler
- All the default colors and behaviour for the hyperlink are now taken from
the users default settings at HKEY_CURRENT_USER\Software\Microsoft\Internet
Explorer\Settings. These settings include a new setting which controls the underline
behaviour. The programmer can still change these values if you so desire.
- Fixed a redraw glitch when the control is sent what looks like unsolicited
reflected CTLCOLOR messages from the parent even when it does not look like
the control needs repainting. The control now does not draw transparently, instead
it uses SetDCBrushColor and GetStockObject(DC_BRUSH). Please note that this
means that the control will only work on Windows 2000 or later and requires
_WIN32_WINNT to be defined >= 0x0500
v1.27 (2 February 2007)
- Fixed a small redraw glitch when you use "Use the System Font"
setting in the dialog resource editor in VC 2005.
- Fixed a small redraw glitch when the enabled state of the control is changed.
v1.26 (27 January 2007)
- Updated copyright details.
- Class now by default sports a tooltip with the hyperlink text as its data.
- Class now only shows the underline when the hyperlink is highlighted.
- The "EnableLink" property has now been removed. Instead you can
use the standard windows WS_DISABLED style. By also doing this, the code now
no longer requires the disabled cursor resource.
- Class now reverts to standard static control colors and style when disabled.
- Removed code which loads up the IDC_HLINKCTRL_ENABLED_CURSOR cursor. This
now means that the code has no dependency on any resources which you need to
add to your project.
v1.25 (21 December 2006)
- Updated copyright details
- Optimized CHLinkCtrl constructor code
- Optimized CHLinkPage constructor code
- Code now compiles cleanly on VC 2005
- Code now only uses ShellExecute approach to executing hyperlinks
- Stripped context menu support from the class. This provides for a more leaner
control implementation. Derived classes are of course free to implement their
own functionality
- Reviewed all TRACE statements for correctness
- Renamed module to be HLinkCtrl to be consistent with the name of the class
- Hyperlink is now executed on mouse up instead of on mouse down. This is
to maintain consistency with how a command button operates
v1.24 (18 February 2002)
- Fixed a problem where the underlined style was not being set for the control
when the "NO wrap" window style was applied.
- Now includes copyright message in the source code and documentation.
v1.23 (12 January 2000)
- The zip file lists disabled.cur in the home directory, not in the RES directory
where it belongs.
- When compiling for UNICODE, the copy-to-clipboard feature sets the incorrect
clipboard type CF_TEXT, not CF_UNICODETEXT. This results in only the first character
being available in the clipboard.
v1.22 (11 December 1999)
- Now supports a disabled hyperlink cursor similar to the offline cursor in
IE via the new Set / GetEnabledLink functions.
v1.21 (29 March 1999)
- Now uses IDC_HAND cursor (available on 98 and Windows 2000) in preference
to the build in cursor which CHLinkCtrl provides.
- Code is now Unicode compliant and build configurations are provided.
- Documentation is now provided in HTML (this file) similar to all of the
classes of the author.
- All the methods of the class are now fully documented in the help file
- Copy Shortcut context menu item now actually works !.
- Removed a number of used variables in the code.
v1.2 (6 November 1997)
- Improved drawing of control by handling WM_CTLCOLOR through MFC message
reflection
- class now derived from CStatic as it should have been from the beginning
- Dropped support for shrink to fit as now no need
- Description is now taken from controls window text
- Now using more standard visited and unvisited colours
- streamlined some of the functions declarations
- Wait cursor is now shown when a shortcut is being saved and when connected
to.
- Context menu for control can now be turned off if so desired
v1.1 (17 September 1997)
- Hot tracking look
- Drop shadow effect
- Context Menu
- Properties dialog ALA IE3
- Add to Favorites & Desktop
- Clipboard support
v1.02 (8 September 1997)
- Number of minor tweaks to the source code.
v1.01 (9 January 1997)
- Small bug fix relating to duplicate functions.
API Reference
The API consists of the following member functions of the class CHLinkCtrl
CHLinkCtrl::SetHyperLink
CHLinkCtrl::GetHyperLink
CHLinkCtrl::SetLinkColor
CHLinkCtrl::GetLinkColor
CHLinkCtrl::SetVisitedLinkColor
CHLinkCtrl::GetVisitedLinkColor
CHLinkCtrl::SetHighlightLinkColor
CHLinkCtrl::GetHighlightLinkColor
CHLinkCtrl::SetUseHighlightColor
CHLinkCtrl::GetVisited
CHLinkCtrl::Open
CHLinkCtrl::SetHyperLink
void CHLinkCtrl::SetHyperLink(const CString& sActualLink);
Parameters
sActualLink The actual hyperlink associated with the static.
Remarks
Associates a hyperlink with this static.
CHLinkCtrl::GetHyperLink
CString CHLinkCtrl::GetHyperLink();
Return Value
The actual hyperlink associated with the static.
Remarks
Returns the hyperlink with this static.
CHLinkCtrl::SetLinkColor
void CHLinkCtrl::SetLinkColor(const COLORREF& color);
Parameters
color The color of the hyperlink when not active.
Remarks
Sets the color of the hyperlink when not active. Initially this is set to pure
blue i.e. RGB(0, 0, 255)
CHLinkCtrl::GetLinkColor
COLORREF CHLinkCtrl::GetLinkColor();
Return Value
The color of the hyperlink when not active.
Remarks
Retrieves the color of the hyperlink when not active.
CHLinkCtrl::SetVisitedLinkColor
void CHLinkCtrl::SetVisitedLinkColor(const COLORREF& color);
Parameters
color The color of the hyperlink when it has been visited.
Remarks
Sets the color of the hyperlink when it has been visited. Initially this is set
to pale magenta i.e. RGB(128, 0, 128)
CHLinkCtrl::GetVisitedLinkColor
COLORREF CHLinkCtrl::GetVisitedLinkColor();
Return Value
The color of the hyperlink when it has been visited.
Remarks
Retrieves the color of the hyperlink when it has been visited.
CHLinkCtrl::SetHighlightLinkColor
void CHLinkCtrl::SetHighlightLinkColor(const COLORREF& color);
Parameters
color The color of the hyperlink when it is highlighted.
Remarks
Sets the color of the hyperlink when it is highlighted. Initially this is set
to pure red i.e. RGB(255, 0, 0)
CHLinkCtrl::GetHighlightLinkColor
COLORREF CHLinkCtrl::GetHighlightLinkColor();
Return Value
The color of the hyperlink when it is highlighted.
Remarks
Retrieves the color of the hyperlink when it is highlighted.
CHLinkCtrl::SetUseHighlightColor
void CHLinkCtrl::SetUseHighlightColor(bool bUseHighlight);
Parameters
bUseHighlight Whether or not this instance should use the highlight
hyperlink color.
Remarks
If bUseHighlight is set to true then the highlight color will be used for the
hyperlink, otherwise the normal visited or link color will be used.
CHLinkCtrl::GetVisited
bool CHLinkCtrl::GetVisited();
Return Value
TRUE if this hyperlink has been visited otherwise FALSE.
Remarks
Accessor for determining whether the hyperlink has been visited or not.
CHLinkCtrl::Open
bool CHLinkCtrl::Open();
Remarks
Actually executes or opens the hyperlink. In the case of a http URL your default
web browser will display the web page. In the case of a mailto link, your configured
internet email client will be displayed. Any URL protocols can also be used such
as "file://", "news://" and "ftp://".
Contacting the Author
PJ Naughter
Email: pjna@naughter.com
Web: http://www.naughter.com
21 March 2022