Thursday, September 25, 2008

Getting the hyper link to work in CRichEditCtrl

Trying to get a mouse click to work in RichEdit and not succeeding? I faced a similar issue and ended up with this flow

Set the SetEventMask for your m_MyRichEdit (this is my control variable for the CRichEditCtrl class) to understand links
m_MyRichEdit.SetEventMask( \
m_MyRichEdit.GetEventMask() | ENM_MOUSEEVENTS)
Now, add a override function for OnNotify in your class. In the .h file, add
afx_msg BOOL OnNotify (WPARAM wParam, \
LPARAM lParam, LRESULT *pResult);
Add the corresponding function similar to the one below
BOOL CParentDlg:: OnNotify (WPARAM wParam, LPARAM lParam, LRESULT * pResult) 
{

if (LOWORD (wParam) == IDC_RICHEDIT)
{
MSGFILTER * mf = (MSGFILTER *) lParam;
switch (mf-> msg)
{
case WM_LBUTTONUP:
ENLINK *p_EnLink;
p_EnLink = (ENLINK *)lParam;
m_MyRichEdit.SetSel (p_EnLink->chrg);
CString szLinkString = m_RichEdit.GetSelText ();
break;
)
)

return CDialog:: OnNotify (wParam, lParam, pResult);
)
Now, your szLinkString will have the data you need to manipulate. You can use this with the ShellExecute function to popup whatever you want.

On, by the way, you need to add RTF content if you intend to make that identifiable. It goes something like
CString szT = "{\\rtf1 \\par http " + szAppendString + " \\par}";
My above code will popup a http link using which I Open it in ShellExecute

No comments: