Click here to Skip to main content
15,882,163 members
Articles / Desktop Programming / MFC
Article

CListCtrl With Web Links

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

Sample Image - CListCtrlLink.jpg

Introduction

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()
    {
       CDialog::OnInitDialog();
        .
        .
        .
        //Initializes CListCtrlLink. If you don’t call 
        //this method you won’t get links in your list control.
        m_listCtrl.Init();
        .
        .
        .
    }
  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"),
                         _T("http://www.stanford.edu/"));
    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"),
                               _T("http://www.yahoo.com/"));
    m_listCtrl.SetItemText(1, 3, _T("john@ficticious.edu"),
                               _T("mailto:john@ficticious.edu"));
    

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.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhi, thank you! Pin
damoying3-Nov-13 16:09
damoying3-Nov-13 16:09 
QuestionHow can I change the url font. Pin
yaoohfox2-Feb-10 23:17
yaoohfox2-Feb-10 23:17 
AnswerFix: ShellExecute is called twice Pin
_Jul_19-May-09 3:01
_Jul_19-May-09 3:01 
GeneralCListCtrlLink Parent Method Pin
rlepine21-Feb-05 4:00
rlepine21-Feb-05 4:00 
GeneralRe: CListCtrlLink Parent Method Pin
cgoldsch21-Feb-05 13:42
cgoldsch21-Feb-05 13:42 
GeneralRe: CListCtrlLink Parent Method Pin
rlepine22-Feb-05 8:32
rlepine22-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
Member 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
Jonathan Wang9-Oct-07 12:00 
GeneralMake a click on link open a new window Pin
anderslundsgard2-Dec-04 22:55
anderslundsgard2-Dec-04 22:55 
Hi. Great and clean article.

Howto do if I want to open a link in a new IE-window eaven if I already have one open?

_____________________________

...and justice for all

APe
GeneralRe: Make a click on link open a new window Pin
cgoldsch21-Feb-05 13:18
cgoldsch21-Feb-05 13:18 
GeneralRe: Make a click on link open a new window Pin
anderslundsgard21-Feb-05 18:16
anderslundsgard21-Feb-05 18:16 
GeneralEhem FILE NOT FOUND!!!!!! Pin
Snyp21-Aug-03 12:56
Snyp21-Aug-03 12:56 
GeneralRe: Ehem FILE NOT FOUND!!!!!! Pin
cgoldsch21-Aug-03 17:37
cgoldsch21-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.