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:
Thanks to Niek Albers for _TrackMouseEvent()
. Thanks to Paul DiLascia for default hand cursor from WinHlp32.
The code is quite short, reuseable and easy to understand. For using this control in your project, you need to do :
CButton
to CURLLinkButton
.//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); #else // 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); strcat(szWindowsDir,"\\Winhlp32.exe"); HMODULE hModule = LoadLibrary(szWindowsDir); if (hModule) m_hCursorHand = ::LoadCursor(hModule, MAKEINTRESOURCE(106)); #endif
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); ON_REGISTERED_MESSAGE(WM_LINK_CLICKED, OnLinkCliked) LRESULT CURLLinkDlg::OnLinkCliked(WPARAM wParam, LPARAM lParam) { UINT nLinkID = (UINT)wParam; switch(nLinkID) { case IDOK: OnOK(); break; case IDC_SHOW_MESSAGE: MessageBox(_T("Hope you find this code useful!")); break; } return 0; }