Hyperlink control

CHyperLink example image Download source files (5.5K) or Download sample project (30K)

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.

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(BOOL bUnderline = TRUE);
      BOOL 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.

Latest additions

  • Link auto-size works correctly, as does changing underline status
  • Default hand cursor.
  • Link now changes colour when the cursor is over it.

Updated: March 8 '98



Comments

  • Excellent. Works great. Thanks.

    Posted by Legacy on 12/29/2003 12:00am

    Originally posted by: Dimitar Darazhanski

    I've been looking for quite a while for something like that. The whole hiper link code is provided in a class, which makes it very fast and easy to use. The included works space is helpful as well. Thanks again:)

    Reply
  • Cursor Flickering

    Posted by Legacy on 10/09/2002 12:00am

    Originally posted by: Larissa

    When LoadCursor is used, the cursor will flicker a little when switching from one icon to another. Is there any way to fix this problem when loading the hand cursor?

    Reply
  • Thanks!

    Posted by Legacy on 06/05/2002 12:00am

    Originally posted by: Elena

    Excelent!

    Reply
  • I use it now :)

    Posted by Legacy on 01/26/2002 12:00am

    Originally posted by: dEdE

    So, i see this page yesterday,
    now i use this class, it's so usefull and simply to use !
    Great thanks !!! nice job.

    Reply
  • Really Useful....

    Posted by Legacy on 01/18/2002 12:00am

    Originally posted by: gandhams

    I really could not understand why Developer Studio doesn't have some thing like this inbuilt. Good Job!
    Saved my time and resources!

    Reply
  • ANIMATION(urgent)

    Posted by Legacy on 12/07/2001 12:00am

    Originally posted by: amit

    Hi all!
    can anyone tell me that how can i use animations in my vc++ dialogbased mfc application.
    how can i use active x animation control.
    pls help
    regards
    amit

    Reply
  • Thanks for an easy to use control

    Posted by Legacy on 07/27/2001 12:00am

    Originally posted by: David Boyd

    It worked quite well, the first time!

    Reply
  • Brilliant

    Posted by Legacy on 04/19/2000 12:00am

    Originally posted by: Barry

    Very useful and easy to implement.

    Just one question, is it possible to use the Classwizard as a means of adding a member variable of type "CHyperLink"?

    I found I had to add a member variable as another type then manually change the code.

    Thanks again.

    Reply
  • Linking control to CHyperLink?

    Posted by Legacy on 03/09/2000 12:00am

    Originally posted by: Mike

    In the description of the control you say...

    To use the hyperlink control, just create a static control (eg IDC_HYPERLINK) and attach it to a member variable of type CHyperLink.

    How do you link this w/o using the class wizard?

    Reply
  • Combo Box Hyperlinks

    Posted by Legacy on 02/03/2000 12:00am

    Originally posted by: Logan

    I would like to implement the CHyperLink class on a pull-down menu. Is this possible, easy or straightforward? I am just wondering how much tweaking would really be involved to make it happen. I am trying to launch a URL "on selection" (or, more specifically, "ON_CBN_CLOSEUP").

    thanks so much,
    Logan

    www.Q2Palm.com
    or search on "Q2Palm" at www.PalmGear.com

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds