CURLLinkButton - Customizable Hyperlink Control

Introduction

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);
#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;
}

History

  • 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

转载于:https://www.cnblogs.com/junzhongxu/archive/2008/08/13/1266774.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值