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

Hyperlink Text View Control

By , 20 Jun 2002
 

Sample Image - hv.gif

Introduction

This control works like a static control, but have some enhancements.
The main is that it recognizes and shows hyperlinks in text.

Feautures

  • Automatically recognizes hyperlinks
  • Word wrapped text view
  • Highlights hyperlink when hover
  • Auto show scrollbars
  • Manually added hyperlinks that can do some operations
  • Tool tips for hyperlinks

Requirements

Next files required to be included before "HyperTextCtr.h":
atlstr.h
atltypes.h
atlctrls.h
atlscrl.h

Also this control uses two STL classes: std::vector and std::list

How to use it in your own applications

  1. Create WTL project and add following header files to your stdafx.h file:
    // WTL includes
    #include <atlstr.h>
    #include <atltypes.h>
    #include <atlctrls.h>
    #include <atlscrl.h>
    
    // Hyper Text Control
    #include "HyperTextCtrl.h"
    
  2. To support mouse wheel define the following in stdafx.h before all includes:
    #define _WIN32_WINNT 0x0400
  3. Add HyperTextControl.h to solution

  4. Add variable declaration inside view class:
    CHyperTextCtrl m_hc;
  5. Create control and set control's properties in WM_CREATE message handler:
    m_hc.Create(m_hWnd, rcDefault, NULL, 
                WS_VISIBLE | WS_CHILD | HTC_WORDWRAP |HTC_AUTO_SCROLL_BARS |
                HTC_UNDERLINE_HOVER | HTC_ENABLE_TOOLTIPS, 0, 0);
    CFont fnt;
    fnt.CreatePointFont(90, "Tahoma");
    m_hc.SetFont(fnt.Detach()); // default font is Times New Roman
    
    m_hc.SetLinkColor(RGB(0,130,0)); // default is blue
    m_hc.SetHoverColor(RGB(0,30,0)); // default is red
    
    Supported additional control styles:
    • HTC_WORDWRAP Word wrap text
    • HTC_AUTO_SCROLL_BARS Auto show/hide scroll bars
    • HTC_UNDERLINE_LINKS Underline links
    • HTC_UNDERLINE_HOVER Underline hover links
    • HTC_ENABLE_TOOLTIPS Enable hyperlink tool tips

  6. Add text and/or manually add hyperlinks:
    m_hc.SetWindowText("The http://www.codeproject.com is a very nice place for the sources.");
    
    m_hc.AppendHyperLink("Launch notepad\n", "Click here to open notepad", "notepad.exe", "");
    m_hc.AppendText("Send mail to ");
    m_hc.AppendHyperLink("author\n\n", "Click here to send e-mail", "mailto:maq@...","");
    m_hc.AppendHyperLink("Close this window\n", "Click here to close window", 
                         m_hWnd, WM_CLOSE, 0, 0);
  7. Add the following code to WM_SIZE message handler of view to fit control into
    the view when resizing:
    CRect rc;
    GetClientRect(rc);
    m_hc.MoveWindow(rc);
    

Environment

This control was created with VC7, WTL7.
It has been tested on Windows XP Pro English.

History

11.06.2002
+ CHyperTextCtrlFSB - uses flat scroll bars
+ SetTextColor, SetBkColor
- bugs fixed

08.06.2002
+ Now CHyperTextCtrl inherits from CHyperTextCtrlT<>

07.06.2002
+ .cpp file removed. Now all code is in HyperTextCtrl.h
- bugs fixed
* Tested on WinXP, VC++7, WTL7

01.06.2001
+ Control was created.
* Tested on Win2K, VC++6 SP3, WTL3.1

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Magomed Abdurakhmanov
Web Developer
Russian Federation Russian Federation
Member
No Biography provided

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   
Generalerror with vs2005 wtl8membergaoyh23 Sep '08 - 22:48 
at line 1539-1540 ,
"Offset = jt->Begin();
Len = jt->Len();"
 
File:c:\program files\microsoft visual studio 8\vc\include\vector
Line:99
Expression:vector iterator not dereferencable.
 
i don't konw why?
GeneralHelp with headersmemberxrigau15 Sep '03 - 20:21 
Anybody knows where to get the latest ATL 3.0 so I can use WTL 7 with VC++ 6.0?

GeneralI added HTC_CENTERmemberPhlip30 Jan '03 - 10:46 
Here's the bio-patch for anyone curious:
 
#define HTC_ENABLE_TOOLTIPS   16 // enable hyperlink tool tips
#define HTC_CENTER                  (1 << 5)   //   like HTML <center>
 
...
 
      ::GetTextExtentExPoint(dc, s + Offset, Len, 0, NULL, NULL, &sz);
 
                              int exx (0);
 
                              if (check_bits(dwStyle, HTC_CENTER))
                                    {
                                    RECT rect;
                                    GetWindowRect(&rect);
                                    int width = rect.right - rect.left;
                                    exx = (width - sz.cx) / 2;
                                    }
 
      rcBounds.left = XPos + exx;
      XPos+=sz.cx;
      rcBounds.right = XPos + exx;
      rcBounds.top = ypos;
 
Just search for the first line of each of those blocks, then apply the edits.
 
Have a good one!

 
   Phlip
                     http://www.c2.com/cgi/wiki?PhlIp
   --   Now collecting votes for a new group:
         news:alt.flame.moderated   --

GeneralGreat control! But, at the risk of pedantry...memberPhlip20 Nov '02 - 20:00 
class CLineInfo : public std::vector
{
public:
 
That line attracts evil spirits. It's bad luck to inherit from an STL template.
 
Here it's a convenience that adds no value but a tiny maintenance hazard (in the unlikely event anyone would ever need to maintain so fine a control).
 
Just an Effective C++ thing...
 
Phlip
http://www.greencheese.org/DontPlanDesigns
-- Got in trouble at StarBucks. I tried to order
"A double latte mocha and a body piercing." --
GeneralRe: Great control! But, at the risk of pedantry...memberMagomed G. Abdurakhmanov20 Nov '02 - 21:22 
Smile | :)
I don't agree with you.
Inheriting from STL template will make code easier and
comprehensible.
For example compare these lines:
 
1. std::vector::const_iterator iter = m_Vector.begin();
2. CLineInfo::const_iterator iter = m_Vector.begin();
 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Great control! But, at the risk of pedantry...memberMagomed G. Abdurakhmanov20 Nov '02 - 21:24 
hmm > < missed:
 
1. std::vector<CLinePartInfo>const_iterator iter = m_Vector.begin();
2. CLineInfo::const_iterator iter = m_Vector.begin();

 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Great control! But, at the risk of pedantry...memberMagomed G. Abdurakhmanov20 Nov '02 - 21:24 
And the last try Smile | :)
 
1. std::vector<CLinePartInfo>::const_iterator iter = m_Vector.begin();
2. CLineInfo::const_iterator iter = m_Vector.begin();

 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Great control! But, at the risk of pedantry...memberPhlip21 Nov '02 - 8:14 
Magomed G. Abdurakhmanov wrote:
1. std::vector::const_iterator iter = m_Vector.begin();
2. CLineInfo::const_iterator iter = m_Vector.begin();

 
Now that I have your attention (Wink | ;-)
 
May I ask how to get all the URLs to launch in a Web browser into a _blank window? I live for keeping multiple Web browsers open at all times, and the "eat the most recently viewed browser" feature licks. Dead | X|
 
Phlip
http://www.greencheese.org/LucidScheming
-- "The Epiphany of the Epiphenomenon of the Epiphysis"
- some jerk-off maliciously pretending to be Henry Miller --

GeneralGreat control!!sussKronuz18 Oct '02 - 7:47 
I think this control is great, but it has only one missing thing.
Any idea how could I change it so I can select and copy the text on it?
GeneralRe: Great control!!memberMagomed G. Abdurakhmanov19 Oct '02 - 23:31 
The easiest is to provide popup menu item - Copy. To allow user to select a range of text using mouse is much harder.
The bad news is that i stopped my work on this control and replaced it with with QHTM http://www.gipsysoft.com/qhtm/
 
Cheers,
Magomed G. Abdurakhmanov

GeneralNice but....memberloket21 Jun '02 - 12:02 
..why not just use the webbrowser control?

GeneralRe: Nice but....memberMagomed G. Abdurakhmanov22 Jun '02 - 7:22 
1. WebBrowser control doesn't automatically displays hyperlinks from plain text.
 
2. Window that contains webbrowser controls opens very slowly, (on my PIII700 it takes 1-2 seconds) for example this component i've used in a hint window
 
3. WEB Browser control takes more than 10MB memory
 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Nice but....memberMagomed G. Abdurakhmanov23 Jun '02 - 2:05 
great... but sources doesn't available Cry | :((
 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Nice but....memberMagomed G. Abdurakhmanov23 Jun '02 - 4:26 
Good. Just to see the code...
 
Cheers,
Magomed G. Abdurakhmanov

GeneralRe: Nice but....memberAnna-Jayne Metcalfe12 Jun '03 - 22:28 
While the Webbrowser control "does what it says on the tin", it's not ideal for use as a static control replacement. It's just too heavyweight.
 
I use this one:
 
http://www.codeproject.com/staticctrl/xhtmlstatic.asp[^]
 
Anna Rose | [Rose]
 
Homepage | My life in tears
 
"Be yourself - not what others think you should be"
- Marcia Graesch
 
"Anna's just a sexy-looking lesbian tart"
- A friend, trying to wind me up. It didn't work.
 
Trouble with resource IDs? Try the Resource ID Organiser Visual C++ Add-In

Generalatlres.h is missing from the RES directorymemberMarat Bedretdinov30 Jun '01 - 11:34 
The atlres.h file is missing from the .\res directory of the demo project.
It would be nice if you're to update your demo source code. Smile | :)
 
Nice work however,
Thanks Cool | :cool:
GeneralRe: atlres.h is missing from the RES directorymemberMagomed G. Abdurakhmanov2 Jul '01 - 6:52 
atlres.h is a part from WTL library which can be downloaded from microsoft.com
 
Cheers,
Magomed G. Abdurakhmanov

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 21 Jun 2002
Article Copyright 2001 by Magomed Abdurakhmanov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid