Click here to Skip to main content
6,595,444 members and growing! (14,638 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate License: The Code Project Open License (CPOL)

Hyperlink control

By Chris Maunder

A simple drop-in hyperlink control
VC6, MFC, Dev
Posted:20 Nov 1999
Updated:22 Mar 2000
Views:169,295
Bookmarked:80 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
60 votes for this article.
Popularity: 8.29 Rating: 4.66 out of 5

1

2
1 vote, 4.2%
3
2 votes, 8.3%
4
21 votes, 87.5%
5
  • Download demo project - 21 Kb
  • Download source - 7 Kb
  • CHyperLink example image

    This is a simple hyperlink control that can be plugged into any dialog. There are already a few examples of this in circulation (PJ Naughter, and one from Paul DiLascia from MSJ) but none had all the features that I wanted. I have used the GotoURL function by Stuart Patterson in the Aug 97 WDJ. It is more robust than a simple ShellExecute, and I have modified it to be a little more Unicode friendly.

    The hyperlink is initially coloured blue, but changes colour when the cursor is over it, and after the user has clicked on it. The cursor that appears when the mouse pointer is over the link can easily be set using CHyperLink::SetLinkCursor, as can the link colours and underlining. The default cursor is a small pointing hand (cursor #106 from the winhlp32.exe file - Thanks to Paul DiLascia of MSJ for that one). There is also a tooltip for the link that dispalys the underlying URL of the control.

    The link has three underline modes which are set using CHyperLink::SetUnderline:

    • CHyperLink::ulHover - Underline appears when cursor is over link
    • CHyperLink::ulNone - No underline
    • CHyperLinkulAlways - Link is always underlined

    To use the hyperlink control, just create a static control (eg IDC_HYPERLINK) and attach it to a member variable of type CHyperLink. The URL for the link is taken as the caption of the static control unless otherwise specified. If no caption for the control is specified then it is set as the URL. If neither exists, then a debug assertion occurs.

    The control can auto-size itself to fit the size of the caption (to preserve a true hyperlink look and feel). The resizing will honour the SS_CENTERIMAGE, SS_LEFT, SS_RIGHT and SS_CENTER flags.

    The control is very simple. The button click and colour notifications are handled by the control, and appropriate fonts and colours selected according to the state of the link. The only tricky bit is handling the button click. In order to do this the static control must have the SS_NOTIFY bit set and a ON_CONTROL_REFLECT handler for the STN_CLICKED message is installed.

    To actually follow the link, ShellExecute is called to open the URL, but if this fails, then the registry is examined in order to find a likely candidate for .htm files. If one is found then this it is launched with the hope that it can handle the URL string supplied. In any case, an error message is displayed on failure.

    Operations for CHyperLink:

    void SetURL(CString strURL);      // Get and set the target URL for the link
    
    CString GetURL();                 
    
    // Set (and get) the Link (unvisited and unvisited) colours      
    
    void SetColours(COLORREF crLinkColour, COLORREF crVisitedColour
                    COLORREF crHoverColour = -1);                      
    COLORREF GetLinkColour();      
    COLORREF GetVisitedColour();      
    COLORREF GetHoverColour();      
    
    // Set (and get) whether or not the link has been visited      
    
    void SetVisited(BOOL bVisited = TRUE);      
    BOOL GetVisited();      
    
    void SetLinkCursor(HCURSOR hCursor);      // Set the cursor for the link      
    
    
    // Set (and get) whether or not ot underline the link)      
    
    void SetUnderline(int nUnderline = CHyperLink::ulHover);      
    int GetUnderline();
    
    // Set (and get) whether to autosize the control to fit the caption text      
    
    void SetAutoSize(BOOL bAutoSize = TRUE);      
    BOOL GetAutoSize();
    

    Note on SetColour: This allows the unvisited/visited and hover colours to be specifed. If the Hover colour (the colour the link appears when the cursor is over it) is -1, then the system highlight colour is used.

    History

    29 Feb 2000 - P. Shaffer standard font modification

    License

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

    About the Author

    Chris Maunder


    Member
    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.
    Occupation: Founder
    Company: The Code Project
    Location: Canada Canada

    Other popular Miscellaneous articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 53 (Total in Forum: 53) (Refresh)FirstPrevNext
    GeneralRerouting the click message. PinmemberKyudos16:26 25 Nov '08  
    GeneralRe: Rerouting the click message. [modified] PinmemberMember 12862406:49 1 Apr '09  
    GeneralRe: Rerouting the click message. PinmemberKyudos16:37 7 Oct '09  
    GeneralRegarding Example PinmemberHemang Raval2:11 17 May '08  
    GeneralGreetings Pinmemberproximuss8:02 16 Mar '07  
    GeneralDear Chris, I used yours convenient Hyperlink control which is proposed to be commercial but Pinmembervitsavinov3:22 26 Sep '06  
    GeneralCHyperLink to open in Firefox Pinmembermail4johnv21:46 7 Jun '06  
    QuestionDynamic create a Hyperlink control Pinmemberunionize21:04 22 Apr '06  
    Generalfew fixes PinmemberAndrew K10:12 22 Apr '05  
    GeneralRe: few fixes Pinmemberlano110616:25 8 Nov '05  
    GeneralCtlColor not firing PinmemberSanatforu1:29 9 Feb '05  
    GeneralCompile error: Cannot open include file: 'atlconv.h' Pinmemberdxchen4:18 24 Mar '04  
    Generalwithout using MFC Pinmembermaharsoft3:03 6 Jan '04  
    GeneralRe: without using MFC Pinmemberlano110613:00 17 Nov '05  
    GeneralPerfect PinmemberWilliam Brendel17:47 7 Jun '03  
    GeneralRe: Perfect PinmemberEd Deighton19:39 24 Aug '03  
    GeneralDefault Hand cursor PinmemberZorglab23:35 16 Apr '03  
    GeneralRedraw PinmemberRage21:19 31 Mar '03  
    Generalhow to do it w/out class-wizard/mfc? Pinmembermorgler2:24 21 Nov '02  
    GeneralCHyperLink is already part of WTL 7.0 PinsussAnonymous2:31 12 Jul '02  
    GeneralHyperlink in a textbox? PinmemberAnonymous22:17 25 Jun '02  
    GeneralHyperlink control in a dialog bar or in a Toolbar!? PinmemberKhlauster10:25 2 Jun '02  
    Generalhow to do this? Pinmemberkalpana23:02 20 Mar '02  
    GeneralRe: how to do this? Pinsusschandubai1:07 1 Aug '02  
    GeneralUse SetCapture iso Timer PinmemberCK Lam13:52 11 Mar '02  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 22 Mar 2000
    Editor: Chris Maunder
    Copyright 1999 by Chris Maunder
    Everything else Copyright © CodeProject, 1999-2009
    Web19 | Advertise on the Code Project