静态超链接控件






4.42/5 (17投票s)
关于使用 CStatic 派生的超链接控件的文章。
引言
本文描述了一个简单的 CStatic
派生的超链接控件,它可以用于您的项目中,链接到任何 URL,例如您的公司网站或电子邮件。此外,该控件可以向父对话框触发一个事件,可以用来弹出另一个对话框,或者执行您想要做的任何其他操作。
该控件的功能包括:-
- 链接到 URL 和电子邮件,并可以链接到另一个子对话框
- 自定义链接、已访问和鼠标悬停时的颜色(如果您没有指定,它们将使用标准样式)
- 启用/禁用工具提示,并自定义工具提示的背景色和文本颜色
使用代码
要在您的项目中使用的这个类,您需要执行以下操作:-
- 将 MyHyperlink.cpp 和 MyHyperlink.h 添加到项目中。
- 在定义控件的头文件中包含 MyHyperlink.h
- 为要自定义的每个按钮创建一个(或编辑)成员变量,类型为
CMyHyperlink
。如果类向导没有显示CMyHyperlink
类型,请选择CStatic
,然后手动编辑代码。
在演示项目中,m_Static1
、m_Static2
、m_Static3
是 CMyHyperlink
控件:-
//Set the target URL m_Static1.SetLinkUrl("www.codeproject.com"); //Enable showing the Tooltip m_Static1.ActiveToolTip(1); //Set the Tooltiptext m_Static1.SetTootTipText("Click Here to go Codeproject"); //Set the tooltip Background Color m_Static1.SetToolTipBgColor(RGB(0, 0, 0)); //Set the Tooltip Text Color m_Static1.SetToolTipTextColor(RGB(0, 255, 0)); // Change the default Linktext, HoverText, Visited Colors // if you don't specify the colors the control will use the //defaults.. m_Static1.SetLinkColor(RGB(255, 0, 0)); m_Static1.SetHoverColor(RGB(0, 0, 255)); m_Static1.SetVisitedColor(RGB(0, 13, 0)); m_Static2.SetLinkUrl("mailto:renjith_sree@hotmail.com"); m_Static2.ActiveToolTip(1); m_Static2.SetTootTipText("Click here to Email Me.."); //The SetFireChild(1) method will enable the //event firing to the parent dialog m_Static3.SetFireChild(1); m_Static3.ActiveToolTip(1); m_Static3.SetTootTipText("Click Here to Fire An event to parent");
要捕获来自 CMyHyperlink
控件的事件,您必须在对话框的消息映射中添加 ON_MESSAGE(_HYPERLINK_EVENT,OnChildFire)
来捕获消息,函数的定义如下:
void CControlContainerDlg::OnChildFire(WPARAM wparam, LPARAM lparam) { //... }
WPARAM
包含事件来自哪个控件的 ID。
关注点
在开发过程中,我遇到了一个问题,无法在鼠标悬停在控件上时获得标准的 Windows 手型光标。我搜索了各种资源,最终在一个网站上找到了描述如何从文件中加载手型光标资源的方法,但这里我也包含了一个 ID 为 IDC_CURSOR_HAND
的手型光标资源。