65.9K
CodeProject 正在变化。 阅读更多。
Home

静态超链接控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.42/5 (17投票s)

2002年12月27日

Ms-PL

2分钟阅读

viewsIcon

176522

downloadIcon

4586

关于使用 CStatic 派生的超链接控件的文章。

引言

本文描述了一个简单的 CStatic 派生的超链接控件,它可以用于您的项目中,链接到任何 URL,例如您的公司网站或电子邮件。此外,该控件可以向父对话框触发一个事件,可以用来弹出另一个对话框,或者执行您想要做的任何其他操作。

该控件的功能包括:-

  1. 链接到 URL 和电子邮件,并可以链接到另一个子对话框
  2. 自定义链接、已访问和鼠标悬停时的颜色(如果您没有指定,它们将使用标准样式)
  3. 启用/禁用工具提示,并自定义工具提示的背景色和文本颜色

使用代码

要在您的项目中使用的这个类,您需要执行以下操作:-

  1. MyHyperlink.cppMyHyperlink.h 添加到项目中。
  2. 在定义控件的头文件中包含 MyHyperlink.h
  3. 为要自定义的每个按钮创建一个(或编辑)成员变量,类型为 CMyHyperlink。如果类向导没有显示 CMyHyperlink 类型,请选择 CStatic,然后手动编辑代码。

在演示项目中,m_Static1m_Static2m_Static3CMyHyperlink 控件:-

//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 的手型光标资源。

© . All rights reserved.