Search code examples
c++winapivisual-c++richedit

RichEdit's EM_AUTOURLDETECT message recognizes link, but I can't click it


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;
}

Solution

  • 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.