0

I'm trying to insert clickable hyperlinks into a CRichEditCtrl.

After searching the Internet for many hours, I am really confused because people are suggesting many different solutions, but none of them seem to work in the end.

The RTF syntax for hyperlinks is like the following, if I'm right:

{\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}

I've tried to insert this into my Rich Edit, but it doesn't work.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
C. Lee
  • 13
  • 4
  • 2
    Possible duplicate of [Can my RichEdit Control contain clickable links?](https://stackoverflow.com/questions/45366016/can-my-richedit-control-contain-clickable-links) – zett42 Aug 28 '17 at 11:25
  • @zett42 Sorry for my bad description, but what I acctually want, is to display RTF formatted Text (like the hyperlink above) in the CRichEditCtrl. I cannot achieve what I want, using the method described in the Question you posted! – C. Lee Aug 29 '17 at 00:10
  • Why not? Easiest way would be to use the newer richedit control. AFAIK RichEdit 4.1 is supported since Windows XP SP3 through msftedit.dll (MSFTEDIT_CLASS). – zett42 Aug 29 '17 at 06:19
  • I need to switch between large texts in my CRichEditCtrl and adding each line (most lines contain a link) one by one and formatting the text afterwards takes way to long. When formatting the text with RTF and adding all the text at once it takes way less time. I managed to display RTF text in bold and color, but still having trouble with the links. How can I use RichEdit 4.1? Thanks a lot! – C. Lee Aug 30 '17 at 07:11
  • Replace `AfxInitRichEdit2()` with `AfxInitRichEdit5()`. Open the .rc file and change the window class of the richedit control from `RichEdit20W` to `RichEdit50W`. – zett42 Aug 30 '17 at 09:21
  • Thanks a lot for your help, I changed AfxInitRichEdit2() with AfxInitRichEdit5() but I can't find where to change the window class. It's not in the properties of the Control. I'm using VS 2008 – C. Lee Aug 30 '17 at 13:41
  • You have to manually edit the .rc file in a text editor to change the window class. – zett42 Aug 30 '17 at 14:12
  • I only found RichEdit20A. Do I need to change it to RichEdit50A or W? Also I get the error AfxInitRichEdit5() identifier not found – C. Lee Aug 30 '17 at 15:09
  • Use `RichEdit50W` which is the Unicode version. `AfxInitRichEdit5()` propably came in a later version of Visual Studio. It just calls `AtlLoadSystemLibraryUsingFullPath(L"MSFTEDIT.DLL");`. Code for `AtlLoadSystemLibraryUsingFullPath` can be found [here](https://github.com/zao/foobar2000_sdk/blob/master/atl/include/atlcore.h). For quick testing you could just do `LoadLibraryW(L"MSFTEDIT.DLL");` but the ATL code is more secure (it tries to prevent DLL hijacking). – zett42 Aug 30 '17 at 15:22
  • The Rich Edit Control doesn't translate the RTF code above. Do I need to change it in order to work with the unicode control? – C. Lee Aug 30 '17 at 15:39
  • Comment under [this answer](https://stackoverflow.com/a/4133689/7571258) suggests slightly different syntax: `{\field{\*\fldinst { HYPERLINK "http://www.google.com" }}{\fldrslt {http://www.google.com}}}`. Also make sure to use double backslash in C string literal. – zett42 Aug 30 '17 at 15:48
  • I did double backslash, but it's still not working. – C. Lee Aug 30 '17 at 15:55
  • Check with Spy++ if the control actually uses `RichEdit50W` class. Sometimes resource editor changes it back to default. – zett42 Aug 30 '17 at 16:02
  • Yeah it does use RichEdit50W – C. Lee Aug 30 '17 at 16:11
  • How do you insert the RTF into the richedit control? – zett42 Aug 30 '17 at 17:46
  • Using SetWindowText(). (I'm german,too, by the way!) – C. Lee Aug 30 '17 at 17:57
  • For RTF you need to use [`CRichEditCtrl::StreamIn(SF_RTF,es)`](https://msdn.microsoft.com/en-us/library/h2hkhzhe(v=vs.120).aspx). – zett42 Aug 30 '17 at 18:20
  • It kinda works now. Thanks so much for you help, time and effort.The only thing that bothers me are two warnings when casting from CMemFile to DWORD and vice versa, like CMemFile* pFile = (CMemFile*)dwCookie; Is there a cleaner way to stream the data or to cast without a warning? – C. Lee Aug 30 '17 at 20:50
  • I've added some code and instructions how to implement friendly name hyperlinks to [this answer](https://stackoverflow.com/a/45367265/7571258). I've always wanted to try out this feature to get rid of my old workaround code for Rich Edit 2.0 but never found the time and motivation to do so ;-). It seems to work nicely, have to do some more testing though. – zett42 Aug 30 '17 at 21:24
  • Great! Thanks a ton again! – C. Lee Aug 30 '17 at 22:57
  • One more question, sorry! Currently my StreamIn function replaces the entire text in the Rich Edit. How can I append it? – C. Lee Aug 30 '17 at 23:58
  • Move selection to end, then use flag to stream into selection. `int len = richedit.GetTextLength(); richedit.SetSel(len,len); richedit.StreamIn(SF_RTF|SFF_SELECTION,...);`. – zett42 Aug 31 '17 at 07:31

0 Answers0