1

I have a RichEdit control in a dialog box. The RichEdit control displays RTF text. EM_AUTOURLDETECT causes the RichEdit control to properly format and recognize the hyperlink. When the mouse hovers over the link, the pointer changes to a hand, but the browser doesn't launch once the link is clicked.

Am I missing some kind of event handler code?

case WM_INITDIALOG:
{
    // Create Richedit
    HWND hwndRE = CreateWindowA("RichEdit20A", "", WS_CHILD | WS_BORDER | WS_VSCROLL | ES_READONLY | ES_MULTILINE, 10, 10, 480, 220, hDlgWnd, 0, hInst, 0);

    SendMessage(hwndRE ,EM_AUTOURLDETECT,(WPARAM)TRUE,(LPARAM)0);
    //SendMessage(hwndRE ,EM_SETEVENTMASK, 1, ENM_LINK | ENM_CHANGE);

    ShowWindow(hwndRE, SW_SHOWNORMAL);
    SETTEXTEX SetTextEx;
    char* aboutdata = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Verdana;}}\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 www.whateverdomain.com} ");
    SendMessage(hwndRE, EM_SETTEXTEX,(WPARAM)&SetTextEx, (LPARAM)(LPCTSTR)aboutdata);
    return TRUE;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Josh Bond
  • 1,719
  • 4
  • 17
  • 26

2 Answers2

3

You can try something like this:

case WM_NOTIFY:
    switch (((LPNMHDR)lParam)->code)
    {
        case EN_LINK:
            ENLINK * enLinkInfo = (ENLINK *)lParam;

            if (enLinkInfo->msg == WM_LBUTTONUP)
            {
                // code which gets clicked URL using enLinkInfo->chrg and saves it in
                // "urlString"

                ShellExecute(NULL, "open", urlString, NULL, NULL, SW_SHOWNORMAL);
            }
            break;

        .... // More cases on WM_NOTIFY switch.
    }
    break;

Basically, when the WM_NOTIFY code is EN_LINK, you get the clicked URL and launch it using ShellExecute.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cosmin
  • 21,216
  • 5
  • 45
  • 60
  • I set: SendMessage(hwndRE ,EM_SETEVENTMASK, 0, ENM_LINK); That enabled the EN_LINK messages to be sent. But now when I hover the mouse over the link (don't even have to click), the browser opens with the website? And the longer you hover, the more instances of the website that it opens... – Josh Bond Jun 01 '11 at 15:55
  • EM_AUTOURLDETECT automatically modifies the rich edit control so it can send EN_LINK notifications. Did you try debugging the code to see what happens? Does your dialog receive any notification from the rich edit control? – Cosmin Jun 01 '11 at 15:56
  • Sorry if this double posts, I was editing while you were typing. I set: SendMessage(hwndRE ,EM_SETEVENTMASK, 0, ENM_LINK); That enabled the EN_LINK messages to be sent. But now when I hover the mouse over the link (don't even have to click), the browser opens with the website? And the longer you hover, the more instances of the website that it opens... – Josh Bond Jun 01 '11 at 15:57
  • You need to check the "msg" element from ENLINK structure. I edited my answer. – Cosmin Jun 01 '11 at 16:06
  • Yeah, EN_LINK sends various notification codes, one called WM_LBUTTONUP. I need to set that as a condition for the shell execute to launch. Will work on that. --EDIT, I see your answer now, sorry. – Josh Bond Jun 01 '11 at 16:13
0

Have a look at EN_LINK:

http://msdn.microsoft.com/en-us/library/bb787970(VS.85).aspx

PhilMY
  • 2,621
  • 21
  • 29
  • I'm not sure how to implement EN_LINK with my code above and how both interact with WM_NOTIFY. That's why I'm asking here... – Josh Bond Jun 01 '11 at 06:12