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

HyperEdit Control

, 9 Oct 2008
Rate this:
Please Sign up or sign in to vote.
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

  • CHyperEdit();

    Standard empty constructor.

  • 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.

  • HE_OPTION_LINK GetLinkOption() const;

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

  • void SetUnderlineOption(HE_OPTION_UNDERLINE optUnderline);

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

  • HE_OPTION_UNDERLINE GetUnderlineOption() const;

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

  • void SetVisited(BOOL bVisited = TRUE);

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

  • BOOL GetVisited() const;

    Returns whether the link has been visited.

  • 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.

  • HCURSOR GetLinkCursor() const;

    Returns the current link cursor.

  • 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.

  • 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.

  • 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.

  • 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).

  • BOOL GetDblClkToJump() const;

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

  • 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.

  • BOOL GetCtrlClkToJump() const;

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

  • void SetURL(LPCTSTR lpszURL = NULL);

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

  • CString GetURL() const;

    Returns the currently set URL.

  • 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.

  • 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.

  • BOOL GetURLIsText() const;

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

  • 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.

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

    Returns the current accelerator used for editing the hyperlink.

  • 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

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)

About the Author

Paul Vickery
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 PinmemberPetitPapaNoël14-Dec-06 22:39 
GeneralRe: Problem with ReadOnly PinmemberMember 54067796-Aug-08 5:23 
GeneralRe: Problem with ReadOnly PinmemberPaul S. Vickery12-Oct-08 22:45 
Generalthanks PinmemberXiaoJiuBin20-Aug-06 17:26 
GeneralCool PinmemberHockey29-Apr-04 16:57 
GeneralRe: Cool PinmemberPaul S. Vickery29-Apr-04 23:35 
GeneralRe: Cool PinmemberHockey30-Apr-04 21:19 
GeneralGet Job.... but PinmemberDaniel Kamisnki16-Apr-03 10:26 
GeneralRe: Get Job.... but PinmemberPaul S. Vickery29-Apr-04 23:40 
GeneralRe: Get Job.... but PinmemberHockey1-Sep-04 8:52 
GeneralA suggestion PinmemberHolger Persch15-Oct-02 20:50 
GeneralNice work Paul V :-) PineditorNishant S23-Aug-02 17:57 

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 | Mobile
Web04 | 2.8.140709.1 | Last Updated 9 Oct 2008
Article Copyright 2002 by Paul Vickery
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid