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.
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()
voidCMFCDemoDlg::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); } elseif(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:
voidCMFCDemoDlg::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:
CStringCMFCDemoDlg::GetClickText()const{ CString str; str.Format(L"clicked %d times",Clicks); return str;}
voidCMFCDemoDlg::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:
voidCMFCDemoDlg::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:
Hopefully the sample code that I put together will help you work with CLinkCtrl and the syslink control in VS 2008.