Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

XHyperLink - yet another hyperlink control

, 16 Oct 2003
XHyperLink is a simple drop-in hyperlink control with optional parent notification.
xhyperlink_demo.zip
XHyperLinkTest.dsw
XHyperLinkTest.exe
app.manifest
hans.ico
XHyperLinkTest.dsp
// XHyperLinkTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XHyperLinkTest.h"
#include "XHyperLinkTestDlg.h"
#include "About.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXHyperLinkTestDlg dialog

BEGIN_MESSAGE_MAP(CXHyperLinkTestDlg, CDialog)
	//{{AFX_MSG_MAP(CXHyperLinkTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP

	// this message is sent by XHyperLink control
	ON_REGISTERED_MESSAGE(WM_XHYPERLINK_CLICKED, OnXHyperLinkClicked)

END_MESSAGE_MAP()

CXHyperLinkTestDlg::CXHyperLinkTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CXHyperLinkTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CXHyperLinkTestDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXHyperLinkTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CXHyperLinkTestDlg)
	DDX_Control(pDX, IDC_STATIC_VISIT, m_Visit);
	DDX_Control(pDX, IDC_STATIC_NOTIFY, m_Notify);
	DDX_Control(pDX, IDC_STATIC_MAIL, m_Mail);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////
// CXHyperLinkTestDlg message handlers

BOOL CXHyperLinkTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_Mail.SetURL(_T("mailto:hdietrich2@hotmail.com"));

	m_Notify.EnableTooltip(FALSE);	// don't display tooltip
	m_Notify.SetNotifyParent(TRUE);		// do notify parent
	m_Notify.EnableURL(FALSE);			// disable navigation
	//m_Notify.SetBackgroundColor(RGB(255,255,255));

	m_Visit.SetURL(_T("http://www.codeproject.com"));

	SetTimer(1, 5000, NULL);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

LRESULT CXHyperLinkTestDlg::OnXHyperLinkClicked(WPARAM wParam, LPARAM)
{
	CString str = _T("");

	str.Format(_T("XHyperLink control #%d was clicked!"), wParam);
	AfxMessageBox(str);

	return 0;
}

void CXHyperLinkTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CXHyperLinkTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CXHyperLinkTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CXHyperLinkTestDlg::OnTimer(UINT /*nIDEvent*/) 
{
	static int nCount = 0;

	TCHAR *cp = NULL;

	switch (nCount)
	{
		default:
		case 0:  cp = _T("Visit CodeProject NOW!!!"); break;
		case 1:  cp = _T("Visit CodeProject now to read what your buds are saying..."); break;
		case 2:  cp = _T("OK, you don't have to visit CodeProject now"); break;
		case 3:  cp = _T("How about a nice visit to Microsoft?"); break;
		case 4:  cp = _T("Visit CodeProject"); break;
	}

	m_Visit.SetWindowText(cp);
	m_Visit.SetAlwaysOpenNew(FALSE);

	if (nCount == 1)
	{
		m_Visit.SetURL(_T("http://www.codeproject.com/lounge.asp"));
	}
	else if (nCount == 3)
	{
		m_Visit.SetURL(_T("http://www.microsoft.com"));
		m_Visit.SetAlwaysOpenNew(TRUE);
	}
	else
	{
		m_Visit.SetURL(_T("http://www.codeproject.com"));
	}

	nCount = ++nCount % 5;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.
 
Recently, I have moved to Los Angeles where I am doing consulting and development work.
 
For consulting and custom software development, please see www.hdsoft.org.






| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 17 Oct 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid