Click here to Skip to main content
Click here to Skip to main content

XHyperLink - yet another hyperlink control

By , 16 Oct 2003
 

Introduction

For many years I have used Chris Maunder's excellent Hyperlink control. Recently I needed a hyperlink that would send a notification to the parent dialog. So I started by adding this feature, and then I found that I had added other features too. Here is a list of the new features:

  • Parent notification - the parent window is optionally sent a notification message.
  • New browser window - you can optionally tell the browser to open a new window (same as if you had held down SHIFT key).
  • Background color - you can override default background color and set your own.
  • Disable tooltip - you can optionally disable tooltips for the hyperlink. This makes sense where the hyperlink only sends a notification to the parent window, but does not navigate.
  • Disable URL - optionally disable the URL. Again, this makes sense where the hyperlink only sends a notification to the parent window.
  • Dynamic resizing - you can set new window text at any time and now the control will be resized.
  • Use of IDC_HAND - the MFC hand cursor is used when it is available, before defaulting to the cursor from Winhelp.
  • GoToURL() is now static - now you can say CXHyperLink::GoToURL("http://www.somedomain.com").
  • XHYPERLINK_REPORT_ERROR define - by default this symbol is not defined, which will remove all the strings and message box from ReportError().
  • Resource leak fixed - added DestroyCursor(m_hLinkCursor) that was suggested by Geert Delmeiren.

How to use

To integrate XHyperLink() into your app, you first need to add following files to your project:

  • XHyperLink.cpp
  • XHyperLink.h

Then use the resource editor to add a static control to your dialog, and use Class Wizard to attach a member variable to that control. Note that when adding the static control, you must name it something other than IDC_STATIC.

Next, include the header file XHyperLink.h in the dialog's header file. Then replace the CStatic definition with CXHyperLink. Now you are ready to start using XHyperLink.

Demo app

The XHyperLinkTest.exe demo shows how to use CXHyperLink. The first hyperlink sends an email:

screenshot

The next hyperlink sends a notification to the parent dialog:

screenshot

and the dialog displays a message box:

screenshot

The last hyperlink shows how the text and URL may be dynamically changed:

screenshot

Acknowledgments

Revision history

  • Version 1.0 - 2003 October 17
    • Initial public release.

Usage

This software is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please to consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.

License

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

About the Author

Hans Dietrich
Software Developer (Senior) Hans Dietrich Software
United States United States
Member
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.






Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUsing in open source app ok?membergalets119 Jul '08 - 9:08 
I wonder if this source code can be used in GNU3 project? Header has a "can not be sold" stipulation, which technically contradicts with GNU3. If any of the authors could come forward and clarify if I can use this control in GNU3-licensed project (or any other open source project for the same matter), I would appreciate it. I tried to email authors, but none of the emails I could find is active. I want to use it in VVCap (www.vvcap.net) project
 
--galets

GeneralThanksmemberAnu_Bala10 Mar '08 - 20:53 
Hi, I used this.Nice.Thanks
 
Anu

GeneralSome more bugsmemberconch2 Mar '06 - 14:09 
When this control is used on a property page that is added and removed several times, some errors occur because the font and the tooltip objects are not destroyed.
 
Here's the fix for the tooltip:
 
// Create the tooltip
if (m_bToolTip)
{
CRect rect;
GetClientRect(rect);
if (m_ToolTip)
{
m_ToolTip.DestroyWindow();
m_ToolTip.Detach();
}
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
}

 

For the font objects you need to do:
 
if (m_Font.m_hObject)
{
m_Font.DeleteObject();
m_Font.Detach();
}

 
All these fixes need to be done in the PreSubclassWindow procedure.

 
Best regards,
 
Matthias
GeneralExcatly What I NeedmemberThatsAlok20 Feb '05 - 18:30 
Parent Notification Static HyperLink control
 
Thanks for This Great Control.
 
One Question: Could i modify it for my use, is I am permitted to do that.
 
thanks once again
 

"I Think this Will Help"
[Vote One Here,.....]
 visit me at http://www.thisisalok.tk

GeneralFix for SetUnderline()memberdrake2815 Dec '04 - 15:55 
In the downloaded code the call to SetUnderline() has no effect because the
underline state has been hard coded in the function declaration. Here are my
changes to give you full control over your hyperlink appearance.
 
1) in the XHyperLink.h file comment out the [= ulHover] in the function
declaration.
 
2) in the XHyperLink.cpp file set the m_nUnderline variable to your desired
default state in the constructor.
 
3) in the PreClassSubwindow override function pass the m_nUnderline variable
in the SetUnderline() function call -- eg. SetUnderline(m_nUnderline);
 
4) in the SetUnderline() function implementation change the first if
statement to the following:
 
if (m_nUnderline == nUnderline && nUnderline != 1)
		return;
 
This forces an "Always Underline" call to set the underline font. Otherwise,
an "Always" call will not underline if it follows a previous "hover" call.
 
After making these changes now you can call m_mycontrol.SetUnderline(state)
and set unique text states for each hyperlink. Of course, if you omit these
calls, then the default state you set in the constructor will determine the
state for all controls.
 
That's all. Hope this helps.
 

GeneralModification: Customized Tooltipsmemberdrake2812 Dec '04 - 7:29 
This is a fabulous class. I would only like to suggest one improvement.

There may be times where you want to show a tooltip, but hide the URL -- for
example the URL is very long.
 
I modified this class to enable customized tooltips. I simply modified the
class declaration for the SetURL function in the XHyperLink.h file as follows:
 
     void SetURL(CString strURL, CString strTip);
Then in the "Protected Attributes" section I added a new member variable:
 
     CString  m_strTip;  // ToolTip text
Then in the XHyperLink.cpp file I made two modifications. First in the
PreSubclassWindow() function, at the end of the function I modified the
following statement:
 
	// Create the tooltip
	if (m_bToolTip)
	{
		CRect rect; 
		GetClientRect(rect);
		m_ToolTip.Create(this);
		m_ToolTip.AddTool(this, m_strTip, rect, TOOLTIP_ID);
	}
and finally, I modified the SetURL() function as follows:
 
     void CXHyperLink::SetURL(CString strURL, CString strTip)
     {
	m_strURL = strURL;
	m_strTip = strTip;
 
	if (::IsWindow(GetSafeHwnd())) 
	{
		PositionWindow();
		m_ToolTip.UpdateTipText(strTip, this, TOOLTIP_ID);
	}
     }
Now from the OnInitDialog() function of your dialog class you simply call
SetURL passing two variables, the first is the actual URL and the second your
customized Tooltip string -- for example:
 
CString szURL = "http://wwwp.musicmatch.com/skins/search.cgi?
NOHEADER=1&REQUEST=THEMES&Font=Arial,9,FW_BOLD&TextColor=1e323c&ForeColor=0000
00&TextBackColor=b2c8d2&HighLightTextBackColor=91b4c3&HighLightTextColor=00000
0&MMJB_KEY=J425L-WA299-L24KK-H27UK&KEY_STATE=1&CERT_STATE=1&MMUID=C1554A68-
4D2D-4A8D-9683-8DF167402ECC&Grant=0&VERSION=7.50.0089MMD&ThemeVersion=2.1
&OEM=MMD&OOEM=MMD&LANG=ENU";
 
CString szTip = "Get MMJB Skins Here";
 
m_Download.SetURL(szURL, szTip);

 
_____________________________________________________________________________
 
That's All Folks!!
 

GeneralRe: Modification: Customized TooltipsmemberJaime Olivares15 May '05 - 5:14 
I agree, but I think it will be more useful a function like this:
 
void SetURL(CString szURL, LPCTSTR szTip=NULL);
 
So, if invoked with just 1 argument, it must use szURL for both URL and tooltip.
 
void CXHyperLink::SetURL(CString szURL, LPCTSTR szTip /*=NULL*/)
{
   m_strURL = szURL;
   m_strTip = szTip ? szTip : szURL;
   if (::IsWindow(GetSafeHwnd())) {
     PositionWindow();
     m_ToolTip.UpdateTipText(m_strTip, this, TOOLTIP_ID);
   }
}

 
Best regards,
Jaime.
GeneralUsing in a CView derived...memberPuiu M.14 Jul '04 - 10:43 
Hi,
how can I use this control in a SDI application? I need to have this XHyperLink in the midle of a CView...
Thanke you in advance!
QuestionDoes the length of the url to be set has a cap?memberdxchen26 Mar '04 - 8:53 
Hi, Hans.
 
It occurs to me that if the length of the url to be set is large than a certain number not only the hyperlink will cease to function but also the dialog in which the hyperlink control is embedded will disappear.
 
Any idea?
 
Thanks,
 

 
Don Chen
AnswerNot unless you say so....memberdrake2812 Dec '04 - 7:45 
The URL is passed in a CString variable, which as far as the class is
concerned could contain all the data of a text file or HTML page if you so
needed. The limit would only be imposed if you set a limit at the time you
created the variable (in ClassWizard for example).
 
As far as the static control is concerned, the idea is to NOT try to place
the URL in the static control, but rather a meaningful abbreviated version --
such as www.microsoft.com
 
Use SetWindowText to set the text in your control, and SetURL to set the
actual URL (they can--and probably should--be two separate strings).
 
Also see my posting in this forum on customized tooltips for setting and
displaying custom tooltips rather than the URL (this modification was made
due to long URLS).
 

 
_____________________________________________________________________________
 
There is nothing left to say!
 

GeneralWithout using MFCmembermaharsoft6 Jan '04 - 2:11 
Hi Sir,
I like yr hyperlink control
but i want to create without using mfc
how can i create a hyperlink using win32(api)
 
plz help me, if u free!!
 
thank a lot

GeneralBug fixesmemberAnonymous26 Aug '05 - 9:07 
Background colors do not take effect:
 
  // transparent text.
  pDC->SetBkMode(TRANSPARENT);
 
  if (m_Brush.GetSafeHandle())
  {
    pDC->SetBkColor(m_crBackground);
    return (HBRUSH) m_Brush;
  }
  else
  {
    return (HBRUSH)GetStockObject(NULL_BRUSH);
  }
 
Corrected code:
 
  if (m_Brush.GetSafeHandle())
  {
    pDC->SetBkColor(m_crBackground);
    return (HBRUSH) m_Brush;
  }
  else
  {
    // transparent text.
    pDC->SetBkMode(TRANSPARENT);
 
    return (HBRUSH)GetStockObject(NULL_BRUSH);
  }
 

Not so much a bug as a performance issue, but SetTimer and OnTimer are unnecessary:
 
void CXHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
  if (!m_bOverControl)		// Cursor has just moved over control
  {
    m_bOverControl = TRUE;
    if (m_nUnderline == ulHover)  SetFont(&m_UnderlineFont);
    Invalidate();
 
    SetTimer(m_nTimerID, 100, NULL);
  }
  CStatic::OnMouseMove(nFlags, point);
}
 
Modified code:
 
void CXHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
  if (m_bOverControl != FALSE)
  {
    CRect rect;
    GetClientRect(&rect);
 
    if (rect.PtInRect(point) == FALSE)
    {
      m_bOverControl = FALSE;
      if (m_nUnderline == ulHover)  SetFont(&m_StdFont);
      Invalidate();
      ReleaseCapture();
    }
  }
  else
  {
    m_bOverControl = TRUE;
    if (m_nUnderline == ulHover)  SetFont(&m_UnderlineFont);
    Invalidate();
    SetCapture();
  }
 
  CStatic::OnMouseMove(nFlags, point);
}
 
Don't forget to delete the OnTimer() code to save a little time compiling and help reduce final EXE size (in debug mode anyway).

GeneralRe: Bug fixesmemberconch2 Mar '06 - 14:05 
The timer "bugfix" is wrong because it leaves the hyperlink in active state after clicking. The original code (with timer) doesn't have this problem.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 17 Oct 2003
Article Copyright 2003 by Hans Dietrich
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid