I want to display a series of strings to an Edit Control or Rich Edit 2.0 Control. After that, I want some of the text displayed to be underlined and in blue. These underlined texts can then be clicked to open another dialog or some sort.
Is there a way to do this?
Rich Edit 2.0 only supports Automatic RichEdit Hyperlinks while Rich Edit 4.1 and newer (msftedit.dll) supports Friendly Name Hyperlinks.
You can emulate friendly name hyperlinks in Rich Edit 2.0 by using a combination of the
CFE_LINK
andCFE_HIDDEN
character formatting flags. Mark the text withCFE_LINK
and hide the URL by applyingCFE_HIDDEN
. Handle theEN_LINK
notification to react on clicks. At this point you would have to do some parsing to extract the hidden URL from the rich text.Alternatively just use
CFE_LINK
for the text and use astd::map
to map text to URLs. This will work as long as there is a 1:1 mapping of text to URL.Edit: I just noted that you just want "to open another dialog" when a link is clicked, so just applying
CFE_LINK
should be good enough in your case.Edit 2: If you don't need to display formatted text and you also don't need scrolling, I suggest to use the SysLink control. Links displayed by the SysLink control have better accessibility than links in the RichEdit control. The former supports the TAB key to navigate through the individual links whereas the latter does not.
Implementing Friendly Name Hyperlinks (Rich Edit 4.1+)
Disclaimer: The following code has been tested under Win 10 with creators update. I haven't found time yet to test it under older OS versions.
InitInstance()
method of yourCWinApp
-derived class, callAfxInitRichEdit5()
if your version of Visual Studio supports it. Otherwise callLoadLibraryW(L"msftedit.dll")
.RichEdit20A
orRichEdit20W
byRichEdit50W
. TheW
stands for the Unicode version of the control.Call
CRichEditCtrl::StreamIn()
to insert the RTF containing the hyperlink(s). In the following I provide a helper functionStreamInRtf()
that simplifies the task of streaming a string into the control:Example usage (using a raw string literal here to make the RTF more readable):
To handle clicks you need to enable
EN_LINK
notifications for the richedit control, e. g.:Add a handler for
EN_LINK
to your message map:Define the event handler method to handle mouse clicks and the return key:
Beginning with Windows 8, the control can show a tooltip that displays the URL of the link under the mouse cursor. This feature can be enabled by sending the
EM_SETEDITSTYLE
message to the control:In case you are missing the defines, here they are: