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:
- Can be plugged into any dialog, form or view
- Link to the any URL and email
- Contains a built-in ToolTip
- Customize the displayed text, URL-Prefix, URL, Tooltip text.
- Customize the colors of Hyperlink (regular, hover, visited) and Tooltip (text color, background color)
- Use a custom cursor or use the standard Hand cursor
- Resize a URL link button to the size of the button's caption
- Can be focused, navigated and activated using the keyboard
- Send a message to parent when clicked
- 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 :
- Add URLLinkButton.h and URLLinkButton.cpp to your project.
- Include URLLinkButton.h in the header file where the controls are defined
- 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
- Use following operators to customize the control:
void SetLinkColor(COLORREF clrRegular, COLORREF clrHover, COLORREF clrVisited);
void SetToolTipColor(COLORREF clrTextColor, COLORREF clrBkColor);
void SetToolTipText(CString sTip=_T(""));
void SetURL (LPCTSTR lpszURL);
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)
m_hCursorHand = AfxGetApp()->LoadCursor (IDC_HAND);
HMODULE hModule = LoadLibrary(szWindowsDir);
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;
MessageBox(_T("Hope you find this code useful!"));
- 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
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, software UI design and mobile software development.