Click here to Skip to main content
15,567,221 members
Articles / Desktop Programming / MFC
Posted 2 Mar 2008


25 bookmarked

Simple CListCtrl with HyperLink Function

Rate me:
Please Sign up or sign in to vote.
4.91/5 (8 votes)
2 Mar 2008CPOL2 min read
A way of adding hyperlink in CListCtrl


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.


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.


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


Using the Code

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

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

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 <span class="code-keyword"><map></span>
using namespace std;
// CLinkListCtrl
typedef struct {
BOOL IsHoverOn;
CString strURL;
CString strTooltip;
class CLinkListCtrl : public CListCtrl
virtual ~CLinkListCtrl();
BOOL SetItemURL(int nItem, int nSubItem, CString strURL, CString strTooltip);
void ClearItemURL(int nItem, int nSubItem);
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);
map<int, stURLTAG*> m_mapURL;
CFont m_ftUnderline;
CFont m_ftURL;
CToolTipCtrl m_toolTip;

virtual void PreSubclassWindow();
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.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, "", "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, "", "search website - google");

Enjoy the code!
If there is a problem with the code, you can contact me at


  • 2 March, 2008: Article posted


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

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

Comments and Discussions

QuestionGood article. Pin
SL0997-Jul-15 16:47
SL0997-Jul-15 16:47 
QuestionColumns to the Right of the URL also get underlined Pin
goondoo273-Mar-10 10:57
goondoo273-Mar-10 10:57 
GeneralIs Not Work !!! Pin
Vovan_Sidorovich24-Jul-08 4:40
Vovan_Sidorovich24-Jul-08 4:40 
GeneralRe: Is Not Work !!! Pin
cqwerty10-Sep-08 8:13
cqwerty10-Sep-08 8:13 

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.