Click here to Skip to main content
13,351,050 members (29,709 online)
Click here to Skip to main content
Add your own
alternative version


78 bookmarked
Posted 19 Jul 2004

CURLLinkButton - Customizable Hyperlink Control

, 19 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A CButton-derived hyperlink control that contains a built-in ToolTip


The CURLLinkButton class extends the functionality of CButton by providing support for URL links. It displays the URL link and invokes the shell when clicked. It can be used in your project to link to any URL such as your website, an application, a folder or your email. You also can use it like other buttons to show a messagebox, dialogbox or anything you like.

This is a hyperlink control which really act like the ones used in Internet Explorer® with following features:

  1. Can be plugged into any dialog, form or view
  2. Link to the any URL and email
  3. Contains a built-in ToolTip
  4. Customize the displayed text, URL-Prefix, URL, Tooltip text.
  5. Customize the colors of Hyperlink (regular, hover, visited) and Tooltip (text color, background color)
  6. Use a custom cursor or use the standard Hand cursor
  7. Resize a URL link button to the size of the button's caption
  8. Can be focused, navigated and activated using the keyboard
  9. Send a message to parent when clicked
  10. Easy to understand, easy to use

Thanks to Niek Albers for _TrackMouseEvent(). Thanks to Paul DiLascia for default hand cursor from WinHlp32.

Using the code

The code is quite short, reuseable and easy to understand. For using this control in your project, you need to do :

  1. Add URLLinkButton.h and URLLinkButton.cpp to your project.
  2. Include URLLinkButton.h in the header file where the controls are defined
  3. Add some buttons to the dialog or form. Add a member variable for each button you want to customize as a hyperlink control. Replace the type of those variables from CButton to CURLLinkButton.
  4. Use following operators to customize the control:
//Resize a URL link button to the size of the button's caption
void SizeToContent();

//Customize the colors of Hyperlink 
void SetLinkColor(COLORREF clrRegular, COLORREF clrHover, COLORREF clrVisited);

//Customize the colors of the Tooltip
void SetToolTipColor(COLORREF clrTextColor, COLORREF clrBkColor);

//Customize the tooltip text. Use default tooltip if sTip is empty
void SetToolTipText(CString sTip=_T(""));

// Set URL. By default, window text will be used
void SetURL (LPCTSTR lpszURL);

//Set URL prefix. For example "mailto:"
void SetURLPrefix (LPCTSTR lpszPrefix); 

If you have a cursor resource in you project, you can customize the cursor or you can use default hand cursor

#if(WINVER >= 0x0500)
    //Load system hand cursor
    m_hCursorHand = AfxGetApp()->LoadCursor (IDC_HAND); 
    // Use a custom Hand cursor
    // Must add a cursor resourse in the project with ID: IDC_CURSOR_HAND
    //m_hCursorHand = AfxGetApp()->LoadCursor (IDC_CURSOR_HAND);

    // If you haven't the cursor resourse in your project
    // load default hand cursor from WinHlp32 module with ID=106
    TCHAR szWindowsDir[MAX_PATH];
    GetWindowsDirectory(szWindowsDir ,MAX_PATH);
    HMODULE hModule = LoadLibrary(szWindowsDir); 
    if (hModule)
        m_hCursorHand = ::LoadCursor(hModule, MAKEINTRESOURCE(106));

When the link button is clicked, ShellExecute is called to open the URL. If this fails, it sends a registered message to the parent window.

const UINT WM_LINK_CLICKED = ::RegisterWindowMessage (_T ("WM_LINK_CLICKED"));

You can create a message handler of the parent window to do anything you want when the hyperlink is clicked. For example:

afx_msg LRESULT OnLinkCliked(WPARAM wParam, LPARAM lParam);


LRESULT CURLLinkDlg::OnLinkCliked(WPARAM wParam, LPARAM lParam)
   UINT nLinkID = (UINT)wParam;
     case IDOK:
          MessageBox(_T("Hope you find this code useful!"));

   return 0;


  • July 18, 2004:
    Initial public release to The Code Project.
  • March 02, 2005:
    Fix memory leak.
    Fix infinite loop problem when invoke the return key on the link to show a dialogbox
    You now don't have to check the option “Owner draw” of the link button


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer
Vietnam Vietnam
Nguyen Duc Thanh is a software developer from Vietnam, a lovely and peaceful country.
He graduated from Faculty of Technology and is doing a master degree at College of Technology, Vietnam National University, Hanoi.
He's interested in image processing, cloud computing, enterprise software development, website application developement.

You may also be interested in...

Comments and Discussions

GeneralGreat control Pin
Hans Dietrich12-May-08 10:11
mvpHans Dietrich12-May-08 10:11 
GeneralThis ctornol and /clr Pin
hickory19-Feb-07 3:45
memberhickory19-Feb-07 3:45 
GeneralSmall enhancement Pin
hickory10-Feb-06 1:01
memberhickory10-Feb-06 1:01 
GeneralRestrictive license Pin
funvill30-Nov-05 11:18
memberfunvill30-Nov-05 11:18 
GeneralRe: Restrictive license Pin
DucThanh30-Nov-05 17:36
memberDucThanh30-Nov-05 17:36 
GeneralRe: Restrictive license Pin
hickory30-Jan-06 4:19
memberhickory30-Jan-06 4:19 
Generalshould have sub-classed CStatic Pin
jtorjo1-Aug-04 23:19
memberjtorjo1-Aug-04 23:19 
GeneralRe: should have sub-classed CStatic Pin
James R. Twine25-Jul-05 5:48
memberJames R. Twine25-Jul-05 5:48 
jtorjo wrote:
Since it's a hyperlink (it's some text, which is drawn with some custom color) and with an extra function (on click), I think subclassing CStatic would have been wiser and more straight-forward.

   The only problem with the normal Static control is that it does not behave like a normal "input capable" control, like an edit control or a button.  It cannot normally be focused, navigated to via the keyboard, dragged and dropped like a normal link control, etc.  When doing serious development, this becomes an issue for handicapped users that cannot use a mouse, and/or use screen readers, or when you want a link control that works much like the link control in Internet Explorer, which users are already familiar with.

   Yes, using a static control "works" for some cases, and is simpler to do.  But like most other things in this world that are basically half-thought-out (half-assed), "works" is not the same as "correct".

   Also note how many of those so-called hyperlink controls activate on the button-down instead of the button-up, for example, which is unlike just about every other "clickable" control out there.  Implementations like that just are demonstrative of a lack of knowledge/understanding of how things really work.


-=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites
(Please rate this post!)
GeneralMemory leak detected Pin
geri23127130-Jul-04 13:03
membergeri23127130-Jul-04 13:03 
GeneralNice article : Infinite loop Pin
JOHN1123-Jul-04 11:30
memberJOHN1123-Jul-04 11:30 
GeneralRe: Nice article : Infinite loop Pin
DucThanh4-Mar-05 20:10
memberDucThanh4-Mar-05 20:10 
GeneralTry this Pin
Anonymous20-Jul-04 8:50
sussAnonymous20-Jul-04 8:50 
GeneralRe: Try this Pin
DucThanh23-Jul-04 0:41
memberDucThanh23-Jul-04 0:41 
GeneralRe: Try this Pin
James R. Twine25-Jul-05 5:52
memberJames R. Twine25-Jul-05 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 20 Jul 2004
Article Copyright 2004 by DucThanh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid