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

A Static Hyperlink control

, 26 Dec 2002
Rate this:
Please Sign up or sign in to vote.
An article describing the usage of a CStatic derived Hyperlink control

Introduction

This article describes a simple CStatic derived Hyperlink control, which can be used in your project to link to any URL such as your company's website or EMail. In addition to that, this control can fire an event  to the parent dialog, it can be used to popup another dialog, or whatever else you want to do.

This controls features include :-

  1. Link to the URL  and EMails ,and can be link in to another child dialog
  2. Customize the colors of link, visited and hover (if you do not specify, they use the standard style)
  3. Enable/disable the Tooltip, and customize the back-color and text-color of the tool-tip

Using the code

For using this class in your project, you have to do :-

  1. Add MyHyperlink.cpp and MyHyperlink.h to the project.
  2. Include MyHyperlink.h in the header file where the controls are defined
  3. Create (or edit) a member variable for each button you want to customize as CMyHyperlink. If the Class Wizard doesn't show the CMyHyperlink type, select CStatic and then edit the code manually.

In the demo project, m_Static1, m_Static2, m_Static3 are the CMyHyperlink controls :-

//Set the target URL 
m_Static1.SetLinkUrl("www.codeproject.com");
//Enable showing the Tooltip
m_Static1.ActiveToolTip(1);
//Set the Tooltiptext
m_Static1.SetTootTipText("Click Here to go Codeproject");
//Set the tooltip Background Color
m_Static1.SetToolTipBgColor(RGB(0, 0, 0));
//Set the Tooltip Text Color
m_Static1.SetToolTipTextColor(RGB(0, 255, 0));

// Change the default Linktext, HoverText, Visited Colors 
// if you don't specify the colors the control will use the 
//defaults..

m_Static1.SetLinkColor(RGB(255, 0, 0)); 
m_Static1.SetHoverColor(RGB(0, 0, 255));
m_Static1.SetVisitedColor(RGB(0, 13, 0));

m_Static2.SetLinkUrl("mailto:renjith_sree@hotmail.com");
m_Static2.ActiveToolTip(1);
m_Static2.SetTootTipText("Click here to Email Me..");

//The SetFireChild(1) method will enable the 
//event firing to the parent dialog
m_Static3.SetFireChild(1);
m_Static3.ActiveToolTip(1);
m_Static3.SetTootTipText("Click Here to Fire An event to parent");

For trapping the event from the CMyHyperlink control you must trap the message in your dialog by adding the ON_MESSAGE(_HYPERLINK_EVENT,OnChildFire) in the MESSAGE MAP of the dialog, the definition of the function is like

void CControlContainerDlg::OnChildFire(WPARAM wparam, LPARAM lparam)           
{
    //...
}

The WPARAM contains the ID of the control from where the Event coming from.

Points of Interest

During the development, I faced a problem to get the standard windows  hand cursor when the mouse is over the controls. I searched for that  in various resources and at last I found that from a site, they described how to load the hand cursor resource from a file, but here I also included a resource of hand cursor with id IDC_CURSOR_HAND

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Simplicity is the ultimate sophistication. Believing in application, then the technology.

Comments and Discussions

 
GeneralTo make Hand appeir Pinmembertetedanus21-Feb-11 5:28 
Generalsmall problem Pinmemberwcccodeproject30-Dec-10 20:52 
GeneralUnicode Pinmemberdima polyakov22-May-07 13:31 
Here is a UNICODE compatible code.
 
Dima
www.mmexplorer.com
 

// MyHyperLink.cpp : implementation file
//
 
// Written By : Renjith.R
// Email : renji12renji@m2comsys.com
// Details :Derived from MFC CStatic
// Date :Nov 25 2002
 
#include "stdafx.h"
#include "MyHyperLink.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
 

 

/////////////////////////////////////////////////////////////////////////////
// CMyHyperLink
 
CMyHyperLink::CMyHyperLink()
{
m_sLinkColor = RGB(0, 0 ,255);
m_sHoverColor = RGB(255, 0, 0);
m_sVisitedColor = RGB(5, 34, 143);
 
m_bFireChild = false;
m_bMouseOver = false;
m_bEnableToolTip = false;
m_bVisited = false;

//Create Tooltip
}
 
CMyHyperLink::~CMyHyperLink()
{
}
 

BEGIN_MESSAGE_MAP(CMyHyperLink, CStatic)
//{{AFX_MSG_MAP(CMyHyperLink)
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
 

ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
/////////////////////////////////////////////////////////////////////////////
// CMyHyperLink message handlers
 
//Sets the Link Color
void CMyHyperLink::SetLinkColor(COLORREF sLinkColor)
{
m_sLinkColor = sLinkColor;
 
}
 
//open the URL by Windows ShellExecute()
bool CMyHyperLink::GoToLinkUrl(CString csLink)
{
 
HINSTANCE hInstance = (HINSTANCE)ShellExecute(NULL, _T("open"), csLink.operator LPCTSTR(), NULL, NULL, 2);
 
if ((UINT)hInstance < HINSTANCE_ERROR){
return false;
}else
return true;
}
 
//User can Active/Inactive the Tooltip already they set
void CMyHyperLink::ActiveToolTip(int nFlag)
{
if (nFlag)
m_bEnableToolTip = true;
else
m_bEnableToolTip = false;
}
 
//change The Tooltip text
void CMyHyperLink::SetTootTipText(LPCTSTR szToolTip)
{
if (m_bEnableToolTip )
{
m_ToolTip.UpdateTipText(szToolTip,this,1001);
}
 
}
 
//The Mouse Move Message
void CMyHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
CStatic::OnMouseMove(nFlags, point);
if (m_bMouseOver)
{
CRect oRect;
GetClientRect(&oRect);
 
//check if the mouse is in the rect
if (oRect.PtInRect(point) == false)
{
m_bMouseOver = false;
//Release the Mouse capture previously take
ReleaseCapture();
RedrawWindow();
return;
}
}else
{
m_bMouseOver = true;
RedrawWindow();
//capture the mouse
SetCapture();
}
}
 
//before Subclassing
void CMyHyperLink::PreSubclassWindow()
{
 
//Enable the Static to send the Window Messages To its parent
DWORD dwStyle = GetStyle();
SetWindowLong(GetSafeHwnd() ,GWL_STYLE ,dwStyle | SS_NOTIFY);
 
TCHAR szCurretText[MAX_PATH];
GetWindowText(szCurretText, MAX_PATH);
if ((szCurretText) == NULL){
SetWindowText(m_csLinkText.operator LPCTSTR());
}

LOGFONT sLogFont;
GetFont()->GetLogFont(&sLogFont);
//Set the Link UnderLined
sLogFont.lfUnderline = true;
//Set the Font to the Control
m_oTextFont.CreateFontIndirect(&sLogFont);
this->SetFont(&m_oTextFont, true);

//Adjust the window
//IsValidURL();
 
//Set the Cursor Hand
//WinHlp32.exe in windows folder ResourceID 106
//is a standard window HAND cursor
//courtesy www.codeguru.com
//you can use a custom Hand cursor resourse also
// i added that as a resourse in this project with
// ID - IDC_CURSOR_HAND
 
TCHAR szWindowsDir[MAX_PATH*2];
GetWindowsDirectory(szWindowsDir ,MAX_PATH*2);
_tcscat(szWindowsDir, _T("\\Winhlp32.exe"));
HMODULE hModule = LoadLibrary(szWindowsDir);

if (hModule){
m_hHyperCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
}
 
this->SetCursor(m_hHyperCursor);
 
//free the module
if (hModule)
FreeLibrary(hModule);
 
CStatic::PreSubclassWindow();
this->SetCursor(m_hHyperCursor);

m_ToolTip.Create(this,TTS_ALWAYSTIP);
CRect oRect;
GetClientRect(&oRect);
m_ToolTip.AddTool(this, _T(""),oRect,1001);
m_ToolTip.ShowWindow(SW_HIDE);
}
 
void CMyHyperLink::SetLinkText(CString csLinkText)
{
m_csLinkText = csLinkText;
this->SetWindowText(csLinkText.operator LPCTSTR());
 
}
 
BOOL CMyHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
 

BOOL CMyHyperLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
 
::SetCursor(m_hHyperCursor);
return true;
//return CStatic::OnSetCursor(pWnd, nHitTest, message);
}
 
//////////////////EVENT WILL GET HERE //////////////////////
 

void CMyHyperLink::OnClicked()
{
if (m_bFireChild){
//Fire the Event to Parent Window
CWnd *pParent;
pParent = GetParent();
int nCtrlID = GetDlgCtrlID();
::SendMessage(pParent->m_hWnd, _HYPERLINK_EVENT, (WPARAM)nCtrlID, 0);
//::PostMessage(pParent->m_hWnd, __EVENT_ID_, (WPARAM)nCtrlID, 0);
 
}else
{
GoToLinkUrl(m_csUrl);
}
 
m_bVisited = true;
//reddraw the control
this->Invalidate(true);
}
 
HBRUSH CMyHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
if (m_bMouseOver){
if (m_bVisited)
pDC->SetTextColor(m_sVisitedColor);
else
pDC->SetTextColor(m_sHoverColor);
}else {
if (m_bVisited)
pDC->SetTextColor(m_sVisitedColor);
else
pDC->SetTextColor(m_sLinkColor);
}
pDC->SetBkMode(TRANSPARENT);
return((HBRUSH)GetStockObject(NULL_BRUSH));
}
 
void CMyHyperLink::SetToolTipTextColor(COLORREF sToolTipText) {
m_ToolTip.SetTipTextColor(sToolTipText);
}
 
void CMyHyperLink::SetToolTipBgColor(COLORREF sToolTipBgColor)
{
m_ToolTip.SetTipBkColor(sToolTipBgColor);
 
}
 
CString CMyHyperLink::GetLinkText() {
if (m_csLinkText.IsEmpty())
return CString("");
return m_csLinkText;
}
 
void CMyHyperLink::SetLinkUrl(CString csUrl) {
m_csUrl= csUrl;
}
 
CString CMyHyperLink::GetLinkUrl() {
return m_csUrl;
}
 
void CMyHyperLink::SetVisitedColor(COLORREF sVisitedColor) {
m_sVisitedColor = sVisitedColor ;
}
 
void CMyHyperLink::SetHoverColor(COLORREF cHoverColor) {
m_sHoverColor = cHoverColor;
}
 
void CMyHyperLink::SetFireChild(int nFlag) {
if (nFlag)
m_bFireChild = true;
else
m_bFireChild = false;
}
 
BOOL CMyHyperLink::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
NMHDR* pMsgHdr;
pMsgHdr = (NMHDR*) lParam;
 
switch (pMsgHdr->code){
case NM_RCLICK:
break;
default:
;
}

return CStatic::OnNotify(wParam, lParam, pResult);
}

 

////////////////////////////////////////////////////////
// Class Name : CMyHyperLink
// Written By : Renjith.R
// Email : renjith_sree@hotmail.com
// Details :Derived from MFC CStatic
// Date :Nov 25 2002
// This can be used as a Hyperlink
//Feel free to use this class in your project
 
///////////////////////////////////////////////////////////
 

#if !defined(AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_)
#define AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_
 
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyHyperLink.h : header file
//
 
//This is the EventID , Which Will send to the Parent
//by the hyperlink control
 
# define _HYPERLINK_EVENT WM_USER + 101
 
/////////////////////////////////////////////////////////////////////////////
// CMyHyperLink window
 
class CMyHyperLink : public CStatic
{
// Construction
public:
CMyHyperLink();
 
// Attributes
public:
 
// Operations
public:
 
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyHyperLink)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
//}}AFX_VIRTUAL
 
// Implementation
public:
void SetFireChild(int nFlag);
CString GetLinkText();
CString GetLinkUrl();

bool GoToLinkUrl(CString csLink);

void SetHoverColor(COLORREF cHoverColor);
void SetVisitedColor(COLORREF sVisitedColor);
void SetLinkUrl(CString csUrl);
void SetToolTipBgColor(COLORREF sToolTipBgColor);
void SetToolTipTextColor(COLORREF sToolTipText);
void SetLinkText(CString csLinkText);
void SetTootTipText(LPCTSTR szToolTip);
void ActiveToolTip(int nFlag);
void SetLinkColor(COLORREF sLinkColor);

virtual ~CMyHyperLink();
 
// Generated message map functions
protected:
bool m_bFireChild;

HCURSOR m_hHyperCursor;

bool m_bEnableToolTip;
bool m_bMouseOver;
bool m_bVisited;
 
CFont m_oTextFont;
CToolTipCtrl m_ToolTip;
 
CString m_csToolTipText;
CString m_csLinkText;
CString m_csUrl;
 
COLORREF m_sHoverColor;
COLORREF m_sLinkColor;
COLORREF m_sVisitedColor;
 
//{{AFX_MSG(CMyHyperLink)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnClicked();
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
//}}AFX_MSG
 
DECLARE_MESSAGE_MAP()
};
 
/////////////////////////////////////////////////////////////////////////////
 
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
 
#endif // !defined(AFX_MYHYPERLINK_H__699B2FB4_0C03_4B12_B117_210A97860E0D__INCLUDED_)

GeneralGood class, but bad code quality Pinmembermath|fourier11-Oct-06 4:41 
GeneralRe: Good class, but bad code quality PinmemberRenjith Ramachandran23-Nov-06 18:43 
GeneralGood Control Pinmemberbaiju.km2-Nov-05 2:38 
Generalwithout using MFC Pinmembermaharsoft6-Jan-04 2:17 
QuestionIncorrect colors? PinmemberdennisV3-Dec-03 14:03 
AnswerRe: Incorrect colors? Pinmemberfoxbat3-Jan-04 2:19 
GeneralHyperlink Color in Win XP PinmemberSVarnavsky18-Oct-03 11:40 
GeneralRe: Hyperlink Color in Win XP Pinmemberrenjith_sree6-Feb-06 7:11 
GeneralBug Pinmemberironfelix2-Sep-03 20:56 
GeneralBackground color and transparency PinmemberAppstmd18-Jul-03 0:02 
Questionwhy this class can not use create? Pinsusspoor31-May-03 22:01 
AnswerRe: why this class can not use create? PinmemberRenjith R31-May-03 22:35 
GeneralRe: why this class can not use create? Pinmembersaptor20-Jan-05 11:17 
GeneralRe: why this class can not use create? Pinmemberzwzzj16-Mar-07 20:58 
GeneralBig Big Bugs!!! Pinmemberpsusong31-May-03 17:02 
GeneralRe: Big Big Bugs!!! Pinmembertijske22-Mar-06 2:22 
Generalexcellent Pinmemberasrx28-Jan-03 1:11 
GeneralRe: excellent PinmemberRenjith R28-Jan-03 4:06 
GeneralStandard hand cursor PinmemberTak29-Dec-02 3:57 
GeneralRe: Standard hand cursor PinmemberRenjith R29-Dec-02 6:30 
GeneralRe: Standard hand cursor PinmemberTak29-Dec-02 6:37 
GeneralRe: Standard hand cursor PinmemberRenjith R29-Dec-02 13:59 
GeneralRe: Standard hand cursor PinmemberTak29-Dec-02 14:07 
GeneralRe: Standard hand cursor PinmemberHockey3-May-04 0:23 
GeneralRe: Standard hand cursor PinsussAnonymous15-Aug-05 22:59 
GeneralExcellent.... PinsussAnonymous27-Dec-02 13:15 
GeneralRe: Excellent.... PinmemberRenjith R31-May-03 18:17 
Questionbetter hyperlink? PinsussAnonymous27-Dec-02 7:50 
AnswerRe: better hyperlink? PinsussAnonymous27-Dec-02 13:13 
GeneralRe: better hyperlink? PinmemberEmcee Lam29-Dec-02 13:55 

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
Web04 | 2.8.140916.1 | Last Updated 27 Dec 2002
Article Copyright 2002 by Renjith Ramachandran
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid