Jun 132013

What’s a SysLink/CLinkCtrl?

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.

Pain points of using SysLink/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.

Can SysLink/CLinkCtrl display multiple URL’s?

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

SysLink/CLinkCtrl demo

We accomplished this via the following one line of code…

m_SysLinkCtrl.SetWindowTextW( _T("<a href=\"ntcoder.com\">First URL</a></pre>
<pre><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.

How to handle events from SysLink/CLinkCtrl?

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


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)
    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…

Clicking SysLink/CLinkCtrl Clicking SysLink/CLinkCtrl


How to modify captions and URL’s in a SysLink/CLinkCtrl?

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("<a href="http://www.msn.com">http://www.msn.com</a>"));
m_SysLinkCtrl.SetItemUrl(1, _T(<a href="http://www.hotmail.com">http://www.hotmail.com</a>));

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

Clicking SysLink/CLinkCtrl Clicking SysLink/CLinkCtrl

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.