Using the good old SysLink/CLinkCtrl class

In MFC and in Win32 we have a dedicated control for managing/displaying hyperlinks, the control is pretty old(I guess the MFC feature pack has come up with a new control). In Win32 its called SysLink and in MFC the wrapper class is called CLinkCtrl.

Using the control is pretty straight forward but in my opinion its usage is not well documented. There are several users who are struggling to find out a way to change the caption of a SysLink control. Of course we do have functions for setting URL (well its URLs) and retrieving it.

As most of us know one SysLink control can display multiple URLs. The control is aware of anchor tag: <a></a>. So it is able to parse out information from the anchor tag. Each URL has a unique index, we’ll have to use this index for setting/retrieving a URL.

So how do we display multiple URL’s in a syslink/CLinkCtrl class. Here is a screenshot of how the syslink control looks…

image

We accomplished this via the following one line of code…

m_SysLinkCtrl.SetWindowTextW(
_T("<a href="ntcoder.com">First URL</a>    <a href="ntcoder.com/bab">Second URL</a>"));

Note the spaces between URLs in the above call. You’ll see the same identical spaces in the caption of the control as well. Now ‘Nibu’ and ‘Rediff’ are two different URLs in the same control. Since we have mutliple URLs hence the control provides an index to access them. So at index 0 the URL will be http://ntcoder.com while at index 1 the URL will be http://ntcoder.com/bab.

To handle clicks on the control we’ll have to provide a message map entry in MFC…

ON_NOTIFY(NM_CLICK, IDC_SYSLINK1, &CSysLinkDlg::OnNMClickSyslink1)

The notification details are stored in the structure NMLINK. The message handler function definition mostly looks like this…

void CSysLinkDlg::OnNMClickSyslink1(NMHDR *pNMHDR, LRESULT *pResult)
{
    PNMLINK pClick = (PNMLINK)pNMHDR;
    MessageBox(pClick->item.szUrl, _T("You clicked on...";), MB_ICONINFORMATION);

    *pResult = 0;
}

Now when we click on the URLs in the control, this is what we get…

image image

Both URLs have separate focus rectangles as well! The only way (from what I’ve found so far) to change the text of the control is call SetWindowText again but of course you can change the URL by calling CLinkCtrl::SetItemUrl. Few examples on changing URL’s…

m_SysLinkCtrl.SetItemUrl(0, _T(“http://www.msn.com“));
m_SysLinkCtrl.SetItemUrl(1, _T(http://www.hotmail.com));

So the next time you click on the URLs in the control this is what we get…

image image

You can also provide ids to these URL’s, these can be done via m_SysLinkCtrl.SetItemID(0, _T(“Custom ID”));. These ids can retrieved via m_SysLInkCtrl.GetItemID.

2 thoughts on “Using the good old SysLink/CLinkCtrl class

Appreciate your comments...