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

HyperEdit Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
9 Oct 2008CPOL4 min read 120.9K   2.9K   52   12
CEdit-derived Hyperlink control, so user can edit hyperlinks
Sample Image - HyperEdit_demo.jpg

Introduction

This control is similar to Chris Maunder's CHyperLink control, except that it is an edit control. The control allows the user to edit the text and the URL independently of each other, or to set them as always the same. A dialog is included to assist the user in editing the URL, similar to those found in some HTML editors.

The control can be set such that it is only a link when it has a URL explicitly set, or if the text looks like a URL. Alternatively, it can set to be always, or never, a link.

Sample Image - HyperEdit_edit_demo.jpg

A context menu item allows the user to invoke the dialog for editing the URL. The control also responds to the Ctrl+K accelerator, which may be changed, in code, to any key combination.

To use the control, just create an edit control, and attach it to a member variable, of type CHyperEdit.

Documentation

The control is fairly simple, and insight into the workings can easily be gained by looking at the source code.

The public functions are listed below:

Functions

  • C++
    CHyperEdit();

    Standard empty constructor.

  • C++
    void SetLinkOption(HE_OPTION_LINK optLink);

    Sets when to display the text as a link. For details of the options, see the enumeration HE_OPTION_LINK.

  • C++
    HE_OPTION_LINK GetLinkOption() const;

    Returns the current link option as set in the above function.

  • C++
    void SetUnderlineOption(HE_OPTION_UNDERLINE optUnderline);

    Sets when to display the text underlined. For details of the options, see the enumeration HE_OPTION_UNDERLINE.

  • C++
    HE_OPTION_UNDERLINE GetUnderlineOption() const;

    Returns the current underline option as set in the above function.

  • C++
    void SetVisited(BOOL bVisited = TRUE);

    Sets whether the link should be displayed as a visited link.

  • C++
    BOOL GetVisited() const;

    Returns whether the link has been visited.

  • C++
    void SetLinkCursor(HCURSOR hCursor = NULL);

    Sets the cursor to be displayed when moving the mouse over a link. Specifying NULL will cause the control to display its default 'hand' cursor.

  • C++
    HCURSOR GetLinkCursor() const;

    Returns the current link cursor.

  • C++
    void SetColours(COLORREF crLink, COLORREF crVisited, COLORREF crHover = -1);

    Sets the link colours. crLink specifies the colour for a link, crVisited specifies the colour of a visited link, and crHover specifies the link colour when the users move their mouse over it. If -1 is specified for crHover then the link does not change colour when the users move their mouse.

  • C++
    void GetColours(COLORREF* pcrLink, COLORREF* pcrVisited = NULL, 
    	COLORREF* pcrHover = NULL) const;

    This retrieves the current link colours. You may specify NULL for any of the parameters if you do not wish to retrieve its value.

  • C++
    void SetIEColours();

    Sets the link colours to the colours chosen in the user's Internet Explorer. If the user does not use Internet Explorer, or the colours have not been set, then they default to blue for the link, purple for a visited link, and no change on hover. Calling GetColours after this function will return the actual values set.

  • C++
    void SetDblClkToJump(BOOL bDblClkToJump = TRUE);

    Sets whether to jump to the URL only on double-clicking the link. If bDblClkToJump is FALSE then a single-click will jump to the URL (the default case).

  • C++
    BOOL GetDblClkToJump() const;

    Returns whether a double-click is needed to jump to the URL.

  • C++
    void SetCtrlClkToJump(BOOL bCtrlClkToJump = TRUE);

    Sets whether to jump to the URL only on control-clicking the link. If bCtrlClkToJump is FALSE then a single-click will jump to the URL (the default case), unless SetDblClkToJump has been set.

  • C++
    BOOL GetCtrlClkToJump() const;

    Returns whether a control-click will jump to the URL.

  • C++
    void SetURL(LPCTSTR lpszURL = NULL);

    Sets the current URL to the string passed in. If lpszURL is NULL, the URL is cleared.

  • C++
    CString GetURL() const;

    Returns the currently set URL.

  • C++
    void EditURL();

    Invokes a dialog to allow the user to edit the URL. Help is given with the URLs prefix, similar to the way in which some HTML editors work.

  • C++
    void SetURLIsText(BOOL bURLIsText = TRUE);

    If bURLIsText is TRUE, then the URL and the displayed text will always be the same. Editing the URL will change the window text, and vice versa. When first calling this with bURLIsText set to TRUE, when the URL and text are different, they will both be set to whichever next changes.

  • C++
    BOOL GetURLIsText() const;

    Returns whether the URL and the text are always the same.

  • C++
    void SetEditURLAccelerator(BYTE fVirt = 0, WORD key = 0);

    Allows an accelerator to be set for editing the hyperlink. The default key combination is Ctrl+K. The fVirt and key parameters are as in the members of the ACCEL structure. If fVirt is 0 then no accelerator is set.

  • C++
    void GetEditURLAccelerator(BYTE& fVirt, WORD& key);

    Returns the current accelerator used for editing the hyperlink.

  • C++
    virtual BOOL IsTextHyperText();

    Determines whether the current edit text is to be treated as a Hyper-link. Returns TRUE if text is a hyper-link, else FALSE. Only called if the link option is set to HEOL_AUTO or HEOL_HASURL. Override this function to use different rules on what makes a valid URL for your application.

Enumerations

C++
typedef enum {
    HEOL_NEVER, // the text will never behave as a link
    HEOL_ALWAYS,// the text will always behave as a link
    HEOL_AUTO,  // the text will behave as a link if it 
                // begins with one of the following:
                // "www", "http:", "file:", "mailto:", "ftp:", 
                // "https:", "gopher:", "nntp:", "prospero:", 
                // "telnet:", "news:", or "wais:".
    HEOL_HASURL // the text will behave as a link if a URL 
                // has been set.
} HE_OPTION_LINK;

typedef enum {
    HEOU_NEVER, // the text is never underlined
    HEOU_ALWAYS,// the text is always underlined.
    HEOU_HOVER  // the text is underlined when 
                // the user hovers the mouse over it.
} HE_OPTION_UNDERLINE;

Acknowledgements

Some of the ideas for this control were taken from Chris Maunder's CHyperLink control.

See Also

If you need a multi-line version of this control, see Hockey's Multiline Hyper Edit Control.

History

Version 1.3 - 09 Oct 2008

  • Updated to build and work correctly in Visual Studio 2003+ and on Unicode
  • Added facility to require Ctrl+click to jump to a link
  • Fixed use of ES_READONLY style as per posts on The Code Project made by PetitPapaNoël and fretje
  • Changed demo application to use Visual Studio 2005

Version 1.2 - 04 Apr 2005

  • Added recognition of file paths (suggested by Holger Persch)
  • Added IsTextHyperText virtual function to allow overriding (suggested by Hockey in his Multiline Hyper Edit Control)
  • Fix to SetLinkOption to re-evaluate the link
  • Fix to context-menu handler to work correctly on multi-monitor systems

Version 1.1 - 09 Jul 2003

  • Updated to support Unicode

Version 1.0 - 12 Aug 2002

  • First version

License

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


Written By
Software Developer (Senior)
United Kingdom United Kingdom
Originally from an electronics background, I moved into software in 1996, partly as a result of being made redundant, and partly because I was very much enjoying the small amount of coding (in-at-the-deep-end-C) that I had been doing!

I swiftly moved from C to C++, and learned MFC, and then went on to real-time C on Unix. After this I moved to the company for which I currently work, which specialises in Configuration Management software, and currently program mainly in C/C++, for Windows. I have been gradually moving their legacy C code over to use C++ (with STL, MFC, ATL, and WTL). I have pulled in other technologies (Java, C#, VB, COM, SOAP) where appropriate, especially when integrating with third-party products.

In addition to that, I have overseen the technical side of the company website (ASP, VBScript, JavaScript, HTML, CSS), and have also worked closely with colleagues working on other products (Web-based, C#, ASP.NET, SQL, etc).

For developing, I mainly use Visual Studio 2010, along with an in-house-designed editor based on Andrei Stcherbatchenko's syntax parsing classes, and various (mostly freeware) tools. For website design, I use Dreaweaver CS3.

When not developing software, I enjoy listening to and playing music, playing electric and acoustic guitars and mandolin.

Comments and Discussions

 
GeneralProblem with ReadOnly Pin
PetitPapaNoël14-Dec-06 22:39
PetitPapaNoël14-Dec-06 22:39 
GeneralRe: Problem with ReadOnly Pin
Member 54067796-Aug-08 5:23
Member 54067796-Aug-08 5:23 
GeneralRe: Problem with ReadOnly Pin
Paul Vickery12-Oct-08 22:45
professionalPaul Vickery12-Oct-08 22:45 
Generalthanks Pin
XiaoJiuBin20-Aug-06 17:26
XiaoJiuBin20-Aug-06 17:26 
GeneralCool Pin
alex.barylski29-Apr-04 16:57
alex.barylski29-Apr-04 16:57 
GeneralRe: Cool Pin
Paul Vickery29-Apr-04 23:35
professionalPaul Vickery29-Apr-04 23:35 
The simplest way to do that is to use a richedit control, as they can support hyperlinks within their text. I'm not sure that there would be any advantage in creating a new control to do that. If you can think of one let me know!



"The way of a fool seems right to him, but a wise man listens to advice" - Proverbs 12:15 (NIV)
GeneralRe: Cool Pin
alex.barylski30-Apr-04 21:19
alex.barylski30-Apr-04 21:19 
GeneralGet Job.... but Pin
Daniel Kamisnki16-Apr-03 10:26
Daniel Kamisnki16-Apr-03 10:26 
GeneralRe: Get Job.... but Pin
Paul Vickery29-Apr-04 23:40
professionalPaul Vickery29-Apr-04 23:40 
GeneralRe: Get Job.... but Pin
alex.barylski1-Sep-04 8:52
alex.barylski1-Sep-04 8:52 
GeneralA suggestion Pin
Holger Persch15-Oct-02 20:50
Holger Persch15-Oct-02 20:50 
GeneralNice work Paul V :-) Pin
Nish Nishant23-Aug-02 17:57
sitebuilderNish Nishant23-Aug-02 17:57 

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.