Alex Jenter
Alex Jenter

Reputation: 4432

How to autodetect urls in RichEdit 2.0?

When we have a RichEdit control and send it an EM_AUTOURLDETECT message with WPARAM set to TRUE, it nicely hightlights the detected URLs and sends the EN_LINK notifications. But it does this only for text that is entered into the control. I haven't found the way to do it for text that's loaded into the control with SetWindowText or EM_STREAMIN. Please help! Thanks

Upd: I've created a test application from scratch and it works fine there. I think the problem might be that I have superclassed the control, that is, created a new window class and just use the window procedure of the original class. I'm gonna try subclassing the control instead..

Upvotes: 0

Views: 2240

Answers (4)

Thomas Rupert
Thomas Rupert

Reputation: 27

I was able to get this to work with a clue from (https://forums.codeguru.com/showthread.php?81261-How-do-I-enable-my-richeditview-receive-EN_LINK-massages).

In header

BOOL b_button = FALSE;

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnEnLink(NMHDR* pNMHDR, LRESULT* pResult);
DECLARE_MESSAGE_MAP()

In scource

void CXXXView::OnInitialUpdate()
{
    CRichEditView::OnInitialUpdate();
    CRichEditCtrl& wndRichEdit = GetRichEditCtrl();
    wndRichEdit.SetAutoURLDetect(TRUE);
    wndRichEdit.SetEventMask(ENM_LINK);
}

void CXXXView::OnEnLink(NMHDR* pNMHDR, LRESULT* pResult)
{
    if (b_button == TRUE)
    {
        b_button = FALSE;
        ENLINK* pEnProtected = reinterpret_cast<ENLINK*>(pNMHDR);
        CString cs;
        GetWindowText(cs);
        LONG cpMin = pEnProtected->chrg.cpMin;
        LONG cpMax = pEnProtected->chrg.cpMax;
        cs = cs.Mid(cpMin, cpMax - cpMin);
        ShellExecute(0, "open", cs, 0, 0, SW_SHOW);
    }
}

void CXXXView::OnLButtonDown(UINT nFlags, CPoint point)
{
    b_button = TRUE;
    CRichEditView::OnLButtonDown(nFlags, point);
}

void CXXXView::OnLButtonUp(UINT nFlags, CPoint point)
{
    b_button = FALSE;
    CRichEditView::OnLButtonUp(nFlags, point);
}

Upvotes: 0

Rob
Rob

Reputation: 78688

I just knocked up a basic WTL dialog based app containing a riched20 control and the following works fine:

CRichEditCtrl richedit = GetDlgItem(IDC_RICHEDIT);
richedit.SetAutoURLDetect(TRUE);
richedit.SetWindowText(_T("http://www.stackoverflow.com"));

I have some old MFC code that does something similar, albeit with ES_STREAM, and it works OK too.

FWIW the WTL CRichEditCtrl wrapper is pretty thin. SetAutoURLDetect simply calls SendMessage passing it EM_AUTOURLDETECT.

I am compiling with _RICHEDIT_VER set to 0x0200 FWIW.

Upvotes: 1

veefu
veefu

Reputation: 2890

You might just have to rewrite the text to the control to get it to re-parse.

Upvotes: 0

Kevin Loney
Kevin Loney

Reputation: 7553

Without knowing the format of the text you are trying to add to the control with SetWindowText and EM_STREAMIN I'm going to take a guess and say this might have something to do with the control's text mode. After setting the contents of the control try sending it a EM_GETTEXTMODE message and see if the TM_PLAINTEXT bit is set. If this is the case then try sending a EM_SETTEXTMODE message followed by EM_AUTOURLDETECT. Your code should look something like this:

UINT textmode = (UINT)::SendMessage(handle_to_control, EM_GETTEXTMODE, 0, 0);
if(textmode & TM_PLAINTEXT) {
    textmode &= ~TM_PLAINTEXT;    // Clear the TM_PLAINTEXT bit
    textmode |= TM_RICHTEXT;      // Set the TM_RICHTEXT bit
    if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) {
        // Failed to set the text mode
    }
}
::SendMessage(handle_to_control, EM_AUTOURLDETECT, TRUE, 0);

Upvotes: 0

Related Questions