With the event of web enabled applications on the desktop one may expect a natural evolution for
CListCtrls to become web friendly and accept URL entries with the same level of support it accepts images. By calling
SetExtendedStyle with some extra parameters such as
LVS_EX_UNDERLINEHOT, it is indeed possible to make
CListCtrls have entries that look like URL links. Unfortunately, this change in the control style forces all entries to look like web links and most likely only some entries in the control may need (or even make sense) to be a web link. To overcome this problem I created
CListCtrl derived class that allows you to include a URL to some entries in a list view control as displayed in figure on the top of the page.
URL links in
CListCtrlLinks use the
ShellExecute API call to do the trick. This makes the links able to process all known protocols on the system (HTTP, FTP, MAILTO, CALLTO…) by calling the system default application to handle the operation. In other words, if you click on a HTTP link, the default web browser will be called to open the desired page for you. If you click on an e-mail address, the default mail reader application will be invoked.
How to use CListCtrlLink
You can start using
CListCtrl in your application with as little as 4 steps, assuming you already have an application with a list control with the
view property set to
report and a
CListCtrl member variable in the class.
- Insert the following files in your project: LinkItem.cpp, LinkProperties.cpp and ListCtrlLink.cpp and their respective header files.
- Replace your current
CListCtrl member variable with a
CListCtrlLink member variable. Do not forget to insert a
#include "ListCtrlLink.h" in your class that hosts the list control.
- In your
OnInitDialog() add a call to
Init() to properly initialize the control like in the code snippet below:
SetItemText were overwritten to take an extra parameter to support the URL link. If you don’t pass any parameter to those calls, no link will be displayed. The code below was extracted from the sample project and by looking at the first two rows in the figure on the top of the page, you can see how the result looks like.
m_listCtrl.InsertItem( 0, _T("Ana"));
m_listCtrl.SetItemText(0, 1, _T("26"));
m_listCtrl.SetItemText(0, 2, _T("Stanford"),
m_listCtrl.SetItemText(0, 3, _T("Unpublished"));
m_listCtrl.InsertItem( 1, _T("John"));
m_listCtrl.SetItemText(1, 1, _T("21"));
m_listCtrl.SetItemText(1, 2, _T("Yahoo"),
m_listCtrl.SetItemText(1, 3, _T("email@example.com"),
Even when “stuff works”, it’s nice to have some understanding on how a component works. Let’s start our small journey by looking at the
CListCtrlLink URL class diagram in figure 1.
Figure 1: The UML diagram from
CListCtrlLink is derived from MFC’s
CListCtrl and adds only 3 new member functions:
Init and the extended
SetItem. The class has two member variables of type
CLinkProperties which will be explained shortly.
CListCtrlLink uses a custom draw technique to make some entries look like links and others like normal list control items. Custom drawing is explained in great detail on Michael Dunn’s Neat Stuff to do in List Controls Using Custom Draw. Once a user clicks on a link,
OnItemchanging is invoked and handles the URL processing.
CLinkProperties is responsible for how a link should look like. In case you want to change visited links to yellow and unvisited links to green, this is the place to go. Note that entries without a link are in fact black links with no underscore and no URLs associated.
CLinkItem stores the URL, whether the link was visited or not and a pointer to a
CLinkProperties. By having a pointer to either
m_noLink, memory requirements for each link are minimized and link settings affect all links at once.
CListCtrlLink is responsible to maintain a list of all
CLinkItems in use, including creation and deletion.