Click here to Skip to main content
15,301,841 members
Articles / Desktop Programming / MFC
Posted 20 Aug 2003


56 bookmarked

CListCtrl With Web Links

Rate me:
Please Sign up or sign in to vote.
4.08/5 (13 votes)
20 Aug 2003MIT3 min read
Extended ListCtrl class that supports web links.

Sample Image - CListCtrlLink.jpg


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_ONECLICKACTIVATE, LVS_EX_UNDERLINECOLD and 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 CListCtrlLink, a 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.

  1. Insert the following files in your project: LinkItem.cpp, LinkProperties.cpp and ListCtrlLink.cpp and their respective header files.
  2. 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.
  3. In your OnInitDialog() add a call to Init() to properly initialize the control like in the code snippet below:
    BOOL CListCtrlLinkTestDlg::OnInitDialog()
        //Initializes CListCtrlLink. If you don’t call 
        //this method you won’t get links in your list control.
  4. CListCtrl methods InsertItem and 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(""),

Component structure

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.

The UML diagram for CListCtrlLink

Figure 1: The UML diagram from CListCtrlLink

CListCtrlLink is derived from MFC’s CListCtrl and adds only 3 new member functions: Init and the extended InsertItem and 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.

Finally CLinkItem stores the URL, whether the link was visited or not and a pointer to a CLinkProperties. By having a pointer to either m_link or 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.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

Generalhi, thank you! Pin
damoying3-Nov-13 16:09
Memberdamoying3-Nov-13 16:09 
QuestionHow can I change the url font. Pin
yaoohfox2-Feb-10 23:17
Memberyaoohfox2-Feb-10 23:17 
AnswerFix: ShellExecute is called twice Pin
_Jul_19-May-09 3:01
Member_Jul_19-May-09 3:01 
GeneralCListCtrlLink Parent Method Pin
rlepine21-Feb-05 4:00
Memberrlepine21-Feb-05 4:00 
GeneralRe: CListCtrlLink Parent Method Pin
cgoldsch21-Feb-05 13:42
Membercgoldsch21-Feb-05 13:42 
GeneralRe: CListCtrlLink Parent Method Pin
rlepine22-Feb-05 8:32
Memberrlepine22-Feb-05 8:32 
GeneralShellExecute called twice, cursor changes to hand even if item is not a link item Pin
Member 81356015-Feb-05 23:12
MemberMember 81356015-Feb-05 23:12 
GeneralRe: ShellExecute called twice, cursor changes to hand even if item is not a link item Pin
Jonathan Wang9-Oct-07 12:00
MemberJonathan Wang9-Oct-07 12:00 
GeneralMake a click on link open a new window Pin
anderslundsgard2-Dec-04 22:55
Memberanderslundsgard2-Dec-04 22:55 
GeneralRe: Make a click on link open a new window Pin
cgoldsch21-Feb-05 13:18
Membercgoldsch21-Feb-05 13:18 
GeneralRe: Make a click on link open a new window Pin
anderslundsgard21-Feb-05 18:16
Memberanderslundsgard21-Feb-05 18:16 
GeneralEhem FILE NOT FOUND!!!!!! Pin
Snyp21-Aug-03 12:56
MemberSnyp21-Aug-03 12:56 
GeneralRe: Ehem FILE NOT FOUND!!!!!! Pin
cgoldsch21-Aug-03 17:37
Membercgoldsch21-Aug-03 17:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.