HyperEdit Control





5.00/5 (6 votes)
CEdit-derived Hyperlink control, so user can edit hyperlinks

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.

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, andcrHover
specifies the link colour when the users move their mouse over it. If-1
is specified forcrHover
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
isFALSE
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
isFALSE
then a single-click will jump to the URL (the default case), unlessSetDblClkToJump
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
isNULL
, 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
isTRUE
, 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 withbURLIsText
set toTRUE
, 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
andkey
parameters are as in the members of theACCEL
structure. IffVirt
is0
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, elseFALSE
. Only called if the link option is set toHEOL_AUTO
orHEOL_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