Syslink Control in MFC 9.0

Visual Studio 2008 (formely code-named ‘Orcas’) has 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.

  1. 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值