Click here to Skip to main content
14,208,761 members
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 3 Jun 2000
Licenced CPOL

Adding Hyperlink support to the MFC Grid Control

, , 3 Jun 2000
Rate this:
Please Sign up or sign in to vote.
A new class that adds hyperlink support to the MFC Grid Control
  • Download source files - 3 Kb
  • Introduction

    This article introduces a new cell class for the MFC Grid Control that provides hyperlinks in cells.

    The class

    To create the class we simply derive a cell from CGridCell. Deriving a cell CGridCell allows us to maintain the functionality of the normal cell, add add our own url support. For our new url cell, we will need to handle the draw, click, cursor, URL color, and URL parsing. We add a COLORREF variable to store the URL color. m_clrUrl will store our URL set by SetUrlColor(COLORREF clr) or the defualt color of GetSysColor(COLOR_HIGHLIGHT). To handle the draw function, we declare:

    #include "GridCtrl_Src\GridCell.h"
    virtual BOOL Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd = TRUE); 

    We then we parse the cell text for a URL, and if it has a URL we draw the cell with our highlight color, otherwise we draw the cell as normal. We store the cell rect before drawing so that we know our bounds when trying to figure out if a URL was clicked.

    BOOL CGridURLCell::Draw(CDC* pDC, int nRow, int nCol, CRect rect, BOOL bEraseBkgnd)
    	// If URL is present then change text color
    	if (HasUrl(GetText()))
    	// Good a place as any to store the bounds of the rect
    	m_Rect = rect;
    	return CGridCell::Draw(pDC, nRow, nCol, rect, bEraseBkgnd);

    HasUrl(CString str) searches the cell text for one of the known URL prefixes. We store the known prefixes in a URLStruct with the prefixes and string length. The URLStruct contains the prefixes in order from most to least commonly used. HasUrl() retreives one prefix at a time, then searches the cell text before retreiving the next prefix. If a URL prefix is found, then TRUE is returned, else we keep searching and otherwise return FALSE.

    // Possible prefixes that indicate a hyperlink
    URLStruct CGridURLCell::g_szURIprefixes[] = { 
    	{ _T("www."), _tcslen(_T("www.")) },
    	{ _T("http:"), _tcslen(_T("http:")) },
    	{ _T("mailto:"), _tcslen(_T("mailto:")) },
    	{ _T("ftp:"), _tcslen(_T("ftp:")) },
    	{ _T("ftp."), _tcslen(_T("ftp.")) },
    	{ _T("https:"), _tcslen(_T("https:")) },
    	{ _T("news:"), _tcslen(_T("news:")) },
    	{ _T("gopher:"), _tcslen(_T("gopher:")) },
    	{ _T("telnet:"), _tcslen(_T("telnet:")) },
    	{ _T("url:"), _tcslen(_T("url:")) },
    	{ _T("file:"), _tcslen(_T("file:")) }
    BOOL CGridURLCell::HasUrl(CString str)
    	int nNumPrefixes = sizeof(g_szURIprefixes) / sizeof(g_szURIprefixes[0]);
    	for (int i = 0; i < nNumPrefixes; i++)
    		if (str.Find(g_szURIprefixes[i].szURLPrefix) >= 0)
    			return TRUE;
    	return FALSE;

    To handle the cursor we call GetHandCursor() in our initialization and store the result in HCURSOR g_hLinkCursor. We override OnSetCursor() so that if we are over a URL then we show the hand cursor, otherwise the normal cursor if shown. This allows us to show the normal cursor over our URL cell, if a URL is not present. This is done by first getting the cursor point, then calling OverURL(CPoint& pt, CString& strURL). OverUrl() translates the point into cell text, then searches to cell if we clicked a URL or normal text, and returns TRUE with the URL in strUrl or else FALSE with the cell text in strUrl.

    // Return TRUE if you set the cursor
    BOOL CGridURLCell::OnSetCursor()
    #ifndef _WIN32_WCE
    	CString strURL;
    	CPoint pt(GetMessagePos());
    	pt = pt - m_Rect.TopLeft();
    	if (OverURL(pt, strURL))
    		return TRUE;
    		return CGridCell::OnSetCursor();
    #ifndef _WIN32_WCE
    // "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
    HCURSOR CGridURLCell::GetHandCursor()
    	if (g_hLinkCursor == NULL) // No cursor handle - load our own
    		// Get the windows directory
    		CString strWndDir;
    		GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
    		strWndDir += _T("\\winhlp32.exe");
    		// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
    		HMODULE hModule = LoadLibrary(strWndDir);
    		if( hModule )
    			HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
    			if( hHandCursor )
    				g_hLinkCursor = CopyCursor(hHandCursor);
    	return g_hLinkCursor;
    // here we figure out if we are over a URL or not
    BOOL CGridURLCell::OverURL(CPoint& pt, CString& strURL)
    	BOOL bOverURL = FALSE;
    	CSize size = GetTextExtent(GetText());
    	// Add left of cell so we know if we clicked on text or not
    	pt.x += m_Rect.left;
    	CPoint center = m_Rect.CenterPoint();
    	if ((m_nFormat & DT_RIGHT) && pt.x >= (m_Rect.right -
    		bOverURL = TRUE;
    	else if ((m_nFormat & DT_CENTER) && 
    		((center.x - ( <= pt.x) && (pt.x <= (center.x + ( )
    		bOverURL = TRUE;
    	else if (pt.x <= ( + m_Rect.left))
    		bOverURL = TRUE;
    	if (!bOverURL)
    		return FALSE;
    	// We are over text - but are we over a URL?
    	bOverURL = FALSE;
    	strURL = GetText();
    	// Use float, otherwise we get an incorrect letter from the point
    	float width = (float);
    	// remove left of cell so we have original point again 
    	pt.x -= m_Rect.left;
    	if (m_nFormat & DT_RIGHT)
    		int wide = m_Rect.Width() -;
    		pt.x -= wide;
    		if (pt.x <= 0)
    			return FALSE;
    	if (m_nFormat & DT_CENTER)
    		int wide = m_Rect.Width() -;
    		pt.x -= (wide/2);
    		if (pt.x <= 0 || pt.x > ( + (wide/2)))
    			return FALSE;
    	// Turn point into a letter
    	int ltrs = (int)((float)pt.x/width);
    	// Find spaces before and after letter, process text between
    	int endSpace = strURL.Find(_T(' '), ltrs);
    	if (endSpace != -1)
    		strURL.Delete(endSpace, strURL.GetLength()-endSpace);
    	int beginSpace = strURL.ReverseFind(_T(' '));
    	if (beginSpace != -1)
    		strURL.Delete(0, ++beginSpace);
    	// Does text have URL
    	return HasUrl(strURL);

    In OnClick(CPoint PointCellRelative) we check to see if we are supposed to launch a URL automatically, if so we call OverUrl() to retreive the URL to launch, then pass the URL for ShellExecute to launch the defualt browser.

    void CGridURLCell::OnClick(CPoint PointCellRelative)
    #ifndef _WIN32_WCE
    	CString strURL;
    	if (GetAutoLaunchUrl() && OverURL(PointCellRelative, strURL))
    		ShellExecute(NULL, _T("open"), strURL, NULL,NULL, SW_SHOW);

    GetAutoLaunchUrl() and SetAutoLaunchUrl(BOOL bLaunch = TRUE) have been included to allow us to toggle the luanching of URLs. By defualt, URLs are automatically launched when clicked.

    To use the cell

    To use the new cell class either set a particular cell's cell-type using CGridCtrl::SetCellType, or you can set the entire grid to use the URL cell by calling CGridCtrl::SetDefCellType

    For instance, to set cell (1,1) as a URL cell, you would ensure the CGridURLCell class header file was included, and then:

    // Set the cell type
    m_Grid.SetCellType(1,1, RUNTIME_CLASS(CGridURLCell));
    // Set the cell properties
    CGridURLCell* cell = (CGridURLCell *)m_Grid.GetCell(1, 1);


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


    About the Authors

    Chris Maunder
    Founder CodeProject
    Canada Canada
    Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

    His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

    He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

    Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

    Fred Ackers
    Web Developer
    United States United States
    Programming using MFC and ATL for almost 12 years now. Currently studying Operating System implementation as well as Image processing. Previously worked on DSP and the use of FFT for audio application. Programmed using ADO, ODBC, ATL, COM, MFC for shell interfacing, databasing tasks, Internet items, and customization programs.

    Comments and Discussions

    GeneralHand cursor Pin
    Griffter UK14-Jul-06 3:23
    memberGriffter UK14-Jul-06 3:23 
    GeneralRe: Hand cursor Pin
    Mattias G25-Nov-09 5:13
    memberMattias G25-Nov-09 5:13 
    GeneralHyperlink support in Virtual Mode Pin
    Vinicius Pontes26-Feb-04 5:23
    memberVinicius Pontes26-Feb-04 5:23 
    GeneralFind Spaces within a line of text Pin
    cypher2529-Oct-03 7:34
    membercypher2529-Oct-03 7:34 
    QuestionHow to deal with <a href=link.htm>LinkText</a> ? Pin
    Ming Liu17-Aug-02 17:56
    memberMing Liu17-Aug-02 17:56 
    AnswerRe: How to deal with &lt;a href=link.htm&gt;LinkText&lt;/a&gt; ? Pin
    Ming Liu17-Aug-02 18:00
    memberMing Liu17-Aug-02 18:00 
    GeneralRe: How to deal with <a href=link.htm>LinkText</a> ? Pin
    Ming Liu17-Aug-02 18:11
    memberMing Liu17-Aug-02 18:11 
    QuestionHow to get hyperlink on a column in listcontrol? Pin
    20-Mar-02 21:54
    suss20-Mar-02 21:54 
    AnswerRe: How to get hyperlink on a column in listcontrol? Pin
    Fred Ackers25-Mar-02 2:56
    memberFred Ackers25-Mar-02 2:56 

    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.

    Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
    Web03 | 2.8.190612.1 | Last Updated 4 Jun 2000
    Article Copyright 2000 by Chris Maunder, Fred Ackers
    Everything else Copyright © CodeProject, 1999-2019
    Layout: fixed | fluid