Click here to Skip to main content
Click here to Skip to main content

CListCtrl With Web Links

, 20 Aug 2003
Rate this:
Please Sign up or sign in to vote.
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

About the Author

cgoldsch
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Generalhi, thank you! Pinmemberdamoying3-Nov-13 16:09 
QuestionHow can I change the url font. Pinmemberyaoohfox2-Feb-10 23:17 
AnswerFix: ShellExecute is called twice Pinmember_Jul_19-May-09 3:01 
GeneralCListCtrlLink Parent Method Pinmemberrlepine21-Feb-05 4:00 
GeneralRe: CListCtrlLink Parent Method Pinmembercgoldsch21-Feb-05 13:42 
GeneralRe: CListCtrlLink Parent Method Pinmemberrlepine22-Feb-05 8:32 
Thanks, that's the path I've being going down.
 
Appreciate the help,
 
Ron
GeneralShellExecute called twice, cursor changes to hand even if item is not a link item PinmemberDigambar K.15-Feb-05 23:12 
GeneralRe: ShellExecute called twice, cursor changes to hand even if item is not a link item PinmemberJonathan Wang9-Oct-07 12:00 
GeneralMake a click on link open a new window Pinmemberd00_ape2-Dec-04 22:55 
GeneralRe: Make a click on link open a new window Pinmembercgoldsch21-Feb-05 13:18 
GeneralRe: Make a click on link open a new window Pinmemberd00_ape21-Feb-05 18:16 
GeneralEhem FILE NOT FOUND!!!!!! Pinmembersnyp21-Aug-03 12:56 
GeneralRe: Ehem FILE NOT FOUND!!!!!! Pinmembercgoldsch21-Aug-03 17:37 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140718.1 | Last Updated 21 Aug 2003
Article Copyright 2003 by cgoldsch
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid