Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CListCtrl With Web Links

0.00/5 (No votes)
20 Aug 2003 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here