分享

用CLinkControl给MFC程序添加超链接

 xue_dong5437 2011-01-05

several important updates for VC++ and MFC. Among them the possibility to create syslink controls, command or split buttons and network address controls. In this post I will show how you can work with the syslink control. The control provides a way to embed hypertext links in a window. It is actually a window that renders marked-up text just as hyperlinks in a web browser. Multiple links can be put in a single control, and are accessed by a zero-based index.

Currently it supports the anchor tag (<A>) with the HREF and ID attributes. HREF is used to specify a URL of any protocol (http, ftp, mailto, etc.). On the other hand ID specifies an unique name within the control, associated with an individual link.

The content is available in the toolbar, so you can simply drag and drop syslink controls onto you dialog template.

Syslink controls on dialog

You can specify the text, including the anchor tag from the properties page, or you can use the SetWindowText function to set it at run-time.

GetDlgItem(IDC_SYSLINK1)->SetWindowText(
   L
"Visit my web site"
   L
" and check my blog.");



You have to handle the NM_CLICK notification, check which link was clicked and take the appropriate action:

BEGIN_MESSAGE_MAP(CMFCDemoDlg, CDialog)
        ON_NOTIFY
(NM_CLICK, IDC_SYSLINK1, &CMFCDemoDlg::OnNMClickSyslink1)
END_MESSAGE_MAP
()  

void CMFCDemoDlg::OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult)
{
        PNMLINK pNMLink
= (PNMLINK) pNMHDR;  

       
if (wcscmp(pNMLink->item.szUrl, WEB_SITE) == 0)
       
{
               
ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
       
}
       
else if(wcscmp(pNMLink->item.szUrl, BLOG_LINK) == 0)
       
{
               
ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL);
       
}  

       
*pResult = 0;
}

In MFC 9.0 (version that will ship with Visual Studio 2008) class CLinkCtrl is a wrapper over the Windows API for working with the syslink control.

You can associate an instance of CLinkCtrl with a syslink control through the DDX mechanism:

void CMFCDemoDlg::DoDataExchange(CDataExchange* pDX)
{
       
CDialog::DoDataExchange(pDX);
        DDX_Control
(pDX, IDC_SYSLINK2, Link2);
}

In my demo application that you can download from here I used a second syslink with an ID attribute. Each time the link is clicked it increments a counter, which is shown. For that I created two helper functions first, one to generate part of the text and the second to set the text to the link control:

CString CMFCDemoDlg::GetClickText() const
{
       
CString str;
        str
.Format(L"clicked %d times", Clicks);
       
return str;
}  

void CMFCDemoDlg::SetLink2Text()
{
       
Link2.SetWindowText(L"Link was " + GetClickText() + L"");
}

When handling the NM_CLICK notification, I checked the szID member of structure LITEM and took the appropriate action:

void CMFCDemoDlg::OnNMClickSyslink2(NMHDR *pNMHDR, LRESULT *pResult)
{
        PNMLINK pNMLink
= (PNMLINK) pNMHDR;  

       
if (wcscmp(pNMLink->item.szID, L"clicked") == 0)
       
{
               
Clicks++;
               
SetLink2Text();
       
}  

       
*pResult = 0;
}

The result is shown here:

Syslink controls on dialog

Hopefully the sample code that I put together will help you work with CLinkCtrl and the syslink control in VS 2008.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多