Click here to Skip to main content
11,576,063 members (54,669 online)
Click here to Skip to main content

Hyperlink control

, 22 Mar 2000 CPOL 249.3K 4.9K 97
Rate this:
Please Sign up or sign in to vote.
A simple drop-in hyperlink control
  • Download demo project - 21 Kb
  • Download source - 7 Kb
  • CHyperLink example image <!-- Article Starts -->

    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)

    Share

    About the Author

    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.

    You may also be interested in...

    Comments and Discussions

     
    GeneralMy vote of 5 Pin
    IMRAN SHAFQA14-May-11 1:08
    memberIMRAN SHAFQA14-May-11 1:08 
    GeneralRerouting the click message. Pin
    Kyudos25-Nov-08 15:26
    memberKyudos25-Nov-08 15:26 
    GeneralRe: Rerouting the click message. [modified] Pin
    Member 12862401-Apr-09 5:49
    memberMember 12862401-Apr-09 5:49 
    GeneralRe: Rerouting the click message. Pin
    Kyudos7-Oct-09 15:37
    memberKyudos7-Oct-09 15:37 
    GeneralRegarding Example Pin
    Hemang Raval17-May-08 1:11
    memberHemang Raval17-May-08 1:11 
    GeneralGreetings Pin
    proximuss16-Mar-07 7:02
    memberproximuss16-Mar-07 7:02 
    GeneralDear Chris, I used yours convenient Hyperlink control which is proposed to be commercial but Pin
    vitsavinov26-Sep-06 2:22
    membervitsavinov26-Sep-06 2:22 
    GeneralCHyperLink to open in Firefox Pin
    mail4johnv7-Jun-06 20:46
    membermail4johnv7-Jun-06 20:46 
    QuestionDynamic create a Hyperlink control Pin
    unionize22-Apr-06 20:04
    memberunionize22-Apr-06 20:04 
    Generalfew fixes Pin
    Andrew K22-Apr-05 9:12
    memberAndrew K22-Apr-05 9:12 
    I'm not sure if these fixes are 100% correct, but I had to make these changes in order for things to work...

    1) In SetURL

    m_strURL = strURL;
    SetWindowText(m_strURL); <-- added this

    The url wouldn't change using this method, so I added the SetWindowText call. A check to see if it's an empty string might be useful too.

    2) In PositionWindow

    My center aligned url was being cut off on the left side.

    I changed...
    Extent.cx += WndRect.Width() - ClientRect.Width();
    to
    Extent.cx += WndRect.Width() - ClientRect.Width() + 1;

    Now the text is showing correctly.
    GeneralRe: few fixes Pin
    lano11068-Nov-05 15:25
    memberlano11068-Nov-05 15:25 
    GeneralCtlColor not firing Pin
    Sanatforu9-Feb-05 0:29
    memberSanatforu9-Feb-05 0:29 
    GeneralCompile error: Cannot open include file: 'atlconv.h' Pin
    dxchen24-Mar-04 3:18
    memberdxchen24-Mar-04 3:18 
    Generalwithout using MFC Pin
    maharsoft6-Jan-04 2:03
    membermaharsoft6-Jan-04 2:03 
    GeneralRe: without using MFC Pin
    lano110617-Nov-05 12:00
    memberlano110617-Nov-05 12:00 
    GeneralPerfect Pin
    William Brendel7-Jun-03 16:47
    memberWilliam Brendel7-Jun-03 16:47 
    GeneralRe: Perfect Pin
    Ed Deighton24-Aug-03 18:39
    memberEd Deighton24-Aug-03 18:39 
    GeneralDefault Hand cursor Pin
    Zorglab16-Apr-03 22:35
    memberZorglab16-Apr-03 22:35 
    GeneralRedraw Pin
    Rage31-Mar-03 20:19
    memberRage31-Mar-03 20:19 
    Questionhow to do it w/out class-wizard/mfc? Pin
    morgler21-Nov-02 1:24
    membermorgler21-Nov-02 1:24 
    GeneralCHyperLink is already part of WTL 7.0 Pin
    Anonymous12-Jul-02 1:31
    sussAnonymous12-Jul-02 1:31 
    QuestionHyperlink in a textbox? Pin
    Anonymous25-Jun-02 21:17
    memberAnonymous25-Jun-02 21:17 
    QuestionHyperlink control in a dialog bar or in a Toolbar!? Pin
    Khlauster2-Jun-02 9:25
    memberKhlauster2-Jun-02 9:25 
    Questionhow to do this? Pin
    kalpana20-Mar-02 22:02
    memberkalpana20-Mar-02 22:02 
    AnswerRe: how to do this? Pin
    chandubai1-Aug-02 0:07
    susschandubai1-Aug-02 0:07 
    GeneralUse SetCapture iso Timer Pin
    CK Lam11-Mar-02 12:52
    memberCK Lam11-Mar-02 12:52 
    GeneralRe: Use SetCapture iso Timer Pin
    Emcee Lam31-Dec-02 8:14
    memberEmcee Lam31-Dec-02 8:14 
    GeneralRe: Use SetCapture iso Timer Pin
    lano11069-Nov-05 0:50
    memberlano11069-Nov-05 0:50 
    GeneralRe: Use SetCapture iso Timer Pin
    lano110610-Nov-05 0:06
    memberlano110610-Nov-05 0:06 
    QuestionMemory Leaking? Pin
    anonymos5-Feb-02 19:31
    memberanonymos5-Feb-02 19:31 
    GeneralBackground colour Pin
    Javier Cerezal11-Jan-02 10:52
    memberJavier Cerezal11-Jan-02 10:52 
    GeneralRe: Background colour Pin
    Emcee Lam29-Dec-02 16:31
    memberEmcee Lam29-Dec-02 16:31 
    QuestionHow to support text changing? Pin
    Andreas Eckhardt24-Oct-01 23:53
    memberAndreas Eckhardt24-Oct-01 23:53 
    GeneralResource Leak Pin
    Geert Delmeiren6-Sep-01 0:23
    memberGeert Delmeiren6-Sep-01 0:23 
    GeneralI can't create it dynamically Pin
    Allen Sun21-Aug-01 10:42
    memberAllen Sun21-Aug-01 10:42 
    GeneralRe: I can't create it dynamically Pin
    yartiss25-Sep-03 4:04
    memberyartiss25-Sep-03 4:04 
    GeneralRe: I can't create it dynamically Pin
    Allen Sun25-Sep-03 11:50
    memberAllen Sun25-Sep-03 11:50 
    GeneralRe: I can't create it dynamically Pin
    Jesper Knudsen25-Sep-05 21:12
    memberJesper Knudsen25-Sep-05 21:12 
    GeneralRe: I can't create it dynamically Pin
    Jesper Knudsen14-Oct-05 2:18
    memberJesper Knudsen14-Oct-05 2:18 
    GeneralSomething I was searching for Pin
    Jitendra16-Jul-01 7:10
    memberJitendra16-Jul-01 7:10 
    Question"open" with new IE window? Pin
    Anonymous13-May-01 13:52
    memberAnonymous13-May-01 13:52 
    AnswerRe: "open" with new IE window? Pin
    tqc16-Sep-01 9:37
    membertqc16-Sep-01 9:37 
    GeneralRe: "open" with new IE window? Pin
    AnthonyJ9-Nov-01 10:37
    memberAnthonyJ9-Nov-01 10:37 
    GeneralRe: "open" with new IE window? Pin
    Tim Smith9-Nov-01 10:47
    memberTim Smith9-Nov-01 10:47 
    GeneralRe: "open" with new IE window? Pin
    AnthonyJ6-May-02 4:33
    memberAnthonyJ6-May-02 4:33 
    QuestionSTN_CLICKED ? Pin
    Patrick Corcoran6-May-01 19:37
    memberPatrick Corcoran6-May-01 19:37 
    QuestionCan anyone help a newbie, please? Pin
    Rick Gungadoo5-Oct-00 7:01
    sussRick Gungadoo5-Oct-00 7:01 
    GeneralIllegal Operation using Release build Pin
    C.G3-Oct-00 22:52
    sussC.G3-Oct-00 22:52 
    GeneralIllegal Operation using Release build Pin
    C.G3-Oct-00 22:52
    sussC.G3-Oct-00 22:52 
    QuestionProblems with picture? Pin
    George1-Apr-00 10:29
    sussGeorge1-Apr-00 10:29 

    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 | Terms of Use | Mobile
    Web04 | 2.8.150603.1 | Last Updated 23 Mar 2000
    Article Copyright 1999 by Chris Maunder
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid