Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Hyperlink control

Rate me:
Please Sign up or sign in to vote.
4.96/5 (34 votes)
22 Mar 2000CPOL2 min read 323.1K   6K   102   58
A simple drop-in hyperlink control
  • 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)


    Written By
    Founder CodeProject
    Canada Canada
    Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

    In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

    In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

    Comments and Discussions

     
    GeneralRe: Background colour Pin
    Emcee Lam29-Dec-02 16:31
    Emcee Lam29-Dec-02 16:31 
    QuestionHow to support text changing? Pin
    24-Oct-01 23:53
    suss24-Oct-01 23:53 
    GeneralResource Leak Pin
    6-Sep-01 0:23
    suss6-Sep-01 0:23 
    GeneralI can't create it dynamically Pin
    Allen Sun21-Aug-01 10:42
    Allen Sun21-Aug-01 10:42 
    GeneralRe: I can't create it dynamically Pin
    yartiss25-Sep-03 4:04
    yartiss25-Sep-03 4:04 
    GeneralRe: I can't create it dynamically Pin
    Allen Sun25-Sep-03 11:50
    Allen Sun25-Sep-03 11:50 
    GeneralRe: I can't create it dynamically Pin
    Jesper Knudsen25-Sep-05 21:12
    Jesper Knudsen25-Sep-05 21:12 
    GeneralRe: I can't create it dynamically Pin
    Jesper Knudsen14-Oct-05 2:18
    Jesper Knudsen14-Oct-05 2:18 
    Another year passed by, still no answer to creating this dynamically? The crash seems to have something to do with the creation of the tooltip control...
    GeneralSomething I was searching for Pin
    16-Jul-01 7:10
    suss16-Jul-01 7:10 
    Question"open" with new IE window? Pin
    13-May-01 13:52
    suss13-May-01 13:52 
    AnswerRe: "open" with new IE window? Pin
    Javier Cerezal16-Sep-01 9:37
    Javier Cerezal16-Sep-01 9:37 
    GeneralRe: "open" with new IE window? Pin
    9-Nov-01 10:37
    suss9-Nov-01 10:37 
    GeneralRe: "open" with new IE window? Pin
    Tim Smith9-Nov-01 10:47
    Tim Smith9-Nov-01 10:47 
    GeneralRe: "open" with new IE window? Pin
    AnthonyJ6-May-02 4:33
    AnthonyJ6-May-02 4:33 
    QuestionSTN_CLICKED ? Pin
    Patrick Corcoran6-May-01 19:37
    Patrick 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
    minimice3-Oct-00 22:52
    minimice3-Oct-00 22:52 
    GeneralIllegal Operation using Release build Pin
    minimice3-Oct-00 22:52
    minimice3-Oct-00 22:52 
    QuestionProblems with picture? Pin
    George1-Apr-00 10:29
    George1-Apr-00 10:29 
    AnswerRe: Problems with picture? Pin
    George1-Apr-00 11:05
    George1-Apr-00 11:05 
    GeneralGotoURL still uses strstr Pin
    zzzzzzzzz27-Mar-00 0:48
    zzzzzzzzz27-Mar-00 0:48 
    GeneralPJ has a rclick menu! Pin
    George22-Mar-00 17:17
    George22-Mar-00 17:17 
    Generaljust what i need Pin
    Raghav Gupta16-Feb-00 4:56
    Raghav Gupta16-Feb-00 4:56 

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.