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

Simple CListCtrl with HyperLink Function

0.00/5 (No votes)
2 Mar 2008 1  
A way of adding hyperlink in CListCtrl

Introduction

I wrote an article on this topic before. However, the article was not descriptive enough. The code was complex as it was modified based on someone else's code, and it included a lot of useless code for the function. So I rewrote this article to describe the details of the implementation, and I provide simple code here.

Background

When I searched on The Code Project for a method to do this, I noticed that most of the code can provide the function, but is not perfect. When the cursor moves on the cell of the grid, not the words, the hyperlink works. So I decided to write code to provide the function with the exact location of the cursor, i.e. when the cursor moves on the words in the cell, the hyperlink works, then you click the mouse and the Web site opens.

When the cursor is in the cell of the hyperlink subitem, but not on the words, nothing happens.

1.jpg

When the cursor moves on the hyperlink, the tooltip works. Click it and the Web site opens.

2.jpg

Using the Code

The base struct for the cell data which has the hyperlink is stURLTAG:

typedef struct {
BOOL IsHoverOn;
CString strURL;
CString strTooltip;
} stURLTAG;

The base class here is named CLinkListCtrl, and it's derived from CListCtrl.

The CLinkListCtrl has an stl::map member (map<int, stURLTAG*> m_mapURL) for recording the URL cells in it. The key of the map is of the integer type, which contains the information of the item and subitem of the cell, calculated as : item*100 + subitem. Then the stl::map can provide a good retrieving function by the key.

Given below is LinkListCtrl.h:

#pragma once
#include <map>
using namespace std;
// CLinkListCtrl
typedef struct {
BOOL IsHoverOn;
CString strURL;
CString strTooltip;
} stURLTAG;
class CLinkListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CLinkListCtrl)
public:
CLinkListCtrl();
virtual ~CLinkListCtrl();
protected:
DECLARE_MESSAGE_MAP()
public:
BOOL SetItemURL(int nItem, int nSubItem, CString strURL, CString strTooltip);
void ClearItemURL(int nItem, int nSubItem);
protected:
CRect GetTextRect(int nItem, int nSubItem);
BOOL PtInText(CPoint pt, int nItem, int nSubItem);
BOOL IsURL(int nItem, int nSubItem);
void RedrawSubItem(int nItem, int nSubItem, BOOL IsHoverOn);
private:
map<int, stURLTAG*> m_mapURL;
CFont m_ftUnderline;
CFont m_ftURL;
CToolTipCtrl m_toolTip;

protected:
virtual void PreSubclassWindow();
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
virtual BOOL PreTranslateMessage(MSG* pMsg);
};

The usage of CLinkListCtrl is shown below:

m_list.InsertColumn(0, "NO", LVCFMT_LEFT, 50);
m_list.InsertColumn(1, "Name", LVCFMT_LEFT, 100);
m_list.InsertColumn(2, "Enter IN", LVCFMT_LEFT, 80);
m_list.SetExtendedStyle
    (m_list.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 
m_list.InsertItem( 0 , _T(""));
m_list.SetItemText(0, 0, "1");
m_list.SetItemText(0, 1, "bd");
m_list.SetItemText(0, 2, "baidu");
m_list.SetItemURL(0, 2, "www.baidu.com", "search website - baidu");
m_list.InsertItem(1, _T(""));
m_list.SetItemText(1, 0, "2");
m_list.SetItemText(1, 1, "gg");
m_list.SetItemText(1, 2, "google");
m_list.SetItemURL(1, 2, "www.google.com", "search website - google");

Enjoy the code!
If there is a problem with the code, you can contact me at iamliuxiao@qq.com

History

  • 2 March, 2008: Article posted

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