Click here to Skip to main content
Click here to Skip to main content
Go to top

Simple CListCtrl with HyperLink Function

, 2 Mar 2008
Rate this:
Please Sign up or sign in to vote.
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 <span class="code-keyword"><map></span>
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

liuxiao

China China
No Biography provided

Comments and Discussions

 
QuestionColumns to the Right of the URL also get underlined Pinmembergoondoo273-Mar-10 9:57 
GeneralIs Not Work !!! PinmemberVovan_Sidorovich24-Jul-08 3:40 
GeneralRe: Is Not Work !!! Pinmembercqwerty10-Sep-08 7:13 

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
Web02 | 2.8.140922.1 | Last Updated 2 Mar 2008
Article Copyright 2008 by liuxiao
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid