Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

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 52.1K   1.5K   25   4
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:

C++
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:

C++
#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:

C++
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, 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 15:47
SL0997-Jul-15 15:47 
QuestionColumns to the Right of the URL also get underlined Pin
goondoo273-Mar-10 9:57
goondoo273-Mar-10 9:57 
GeneralIs Not Work !!! Pin
Vovan_Sidorovich24-Jul-08 3:40
Vovan_Sidorovich24-Jul-08 3:40 
GeneralRe: Is Not Work !!! Pin
cqwerty10-Sep-08 7:13
cqwerty10-Sep-08 7: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.