Introduction
CHyperEdit
was inspired by the following message post.
CHyperEdit
is a drop-in replacement for the standard MFC CEdit
control, and requires no additional initialization or function calls in order to be fully functional. However, it is possible to fine tune what exactly constitutes a hyperlink-able token, and can easily be achieved in a derived CHyperEdit
class by overriding the virtual function IsWordHyper
; click here to view example.
Features
- Customizable hyperlink and hover colors.
- Multiple hyperlinks mix freely with normal text.
- Extensible hyperlink tokenizer.
API
public
COLORREF GetNormalColor()
COLORREF GetHoverColor()
void SetLinkColors(COLORREF clrNormal, COLORREF clrHover)
CString IsHyperlink(CPoint& pt)
protected
virtual BOOL IsWordHyper(const CString& csToken)
Usage
Basic usage
Using CHyperEdit
couldn't be easier, just add a normal EDIT
control to your dialog. Right click on the EDIT
control you just added and select Classwizard. Click on the Member Variables tab. Double click on the control ID of the EDIT
control you just added. Enter a Member Variable name and instead of Value in the Category drop down, select Control. Under the Variable Type drop down, you should now see a CHyperEdit
.
Note: You may have to delete your *.clw file and rebuild it before CHyperEdit
becomes available.
If the CHyperEdit
class isn't available, you can just select CEdit
and replace any instance of CEdit
with CHyperEdit
. You may also have to manually add the required #include
directives before compiling.
For more control over CHyperEdit
, you can choose to override the default functionality of the hyperlink tokenizer and better dictate what exactly constitutes a hyperlink-able URL. For example, the following snippet extends the default behavior by only hyperlinking tokens which include www in the domain name. This would be useful if we wished to ignore IP addresses.
BOOL CMyHyperEdit::IsWordHyper(const CString& csToken) const
{
if(IsWhiteSpace(csToken, 0)) return FALSE;
CString csTemp(csToken);
csTemp.MakeLower();
if(csTemp.Left(7) == "http://www") return TRUE;
return FALSE;
}
Note: You could also use a readily available regex library like regex++ for much more complex syntax checking of URLs or email addresses.
IsHyperlink
is a publicly available function whose purpose may not be immediately apparent; truth be told, I only now realized that it may be useful. :)
This function returns the entire hyperlink token (if any) that the mouse is currently over. If the mouse is not over any hyperlink token when this function is called, it returns the results of a CString::Empty()
.
With a little imagination, some interesting possibilities arise. You could, for example, use a WM_TIMER
in your application and check the return value of IsHyperlink
; if it returns a valid URL for a web site, you could download the HTML page and extract the TITLE
and display it in a tool tip.