1.添加消息定义
afx_msg void OnRichEditExLink(NMHDR* in_pNotifyHeader, LRESULT* out_pResult); //.h
ON_NOTIFY(EN_LINK, IDC_RICHEDIT1, OnRichEditExLink ) //.cpp
2.修改richedit控件属性
//这段放在OninitDlg函数里做
unsigned int mask = ::SendMessage(m_rich.m_hWnd, EM_GETEVENTMASK, 0, 0);
::SendMessage(m_rich.m_hWnd, EM_SETEVENTMASK, 0, mask | ENM_LINK | ENM_MOUSEEVENTS | ENM_SCROLLEVENTS | ENM_KEYEVENTS);
::SendMessage(m_rich.m_hWnd, EM_AUTOURLDETECT, true, 0);
3.消息处理函数
void CTESTEDITHPYDlg::OnRichEditExLink( NMHDR* in_pNotifyHeader, LRESULT* out_pResult )
{
ENLINK* l_pENLink = ( ENLINK* )in_pNotifyHeader ;
*out_pResult = 0 ;
switch( l_pENLink->msg )
{
case WM_LBUTTONDOWN:
{
CString l_URL ;
CHARRANGE l_CharRange ;
CRichEditCtrl *m_TempEdit;
m_TempEdit = (CRichEditCtrl*)CRichEditCtrl::FromHandle(l_pENLink->nmhdr.hwndFrom);
m_TempEdit->GetSel( l_CharRange ) ;
m_TempEdit->SetSel( l_pENLink->chrg ) ;
l_URL = m_TempEdit->GetSelText() ;
m_TempEdit->SetSel( l_CharRange ) ;
CWaitCursor l_WaitCursor ;
ShellExecute( this->GetSafeHwnd(), _T( "open" ), l_URL, NULL, NULL, SW_SHOWNORMAL ) ;
*out_pResult = 1 ;
}
break ;
}
}
//vs2008
消息处理函数可以通过添加 afx_msg void OnEnLinkRichedit21(NMHDR *pNMHDR, LRESULT *pResult);来做.
vs2008 处理打开网站方法
void CReminderDlg::OnEnLinkRichedit21(NMHDR *pNMHDR, LRESULT *pResult)
{
ENLINK *pEnLink = reinterpret_cast<ENLINK *>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
switch (pEnLink->msg)
{
case WM_LBUTTONDOWN:
{
CString l_URL ;
CHARRANGE l_CharRange ;
CRichEditCtrl *m_TempEdit;
m_TempEdit = (CRichEditCtrl*)CRichEditCtrl::FromHandle(pEnLink->nmhdr.hwndFrom);
m_TempEdit->GetSel(l_CharRange ) ;
m_TempEdit->SetSel(pEnLink->chrg ) ;
l_URL = m_TempEdit->GetSelText() ;
m_TempEdit->SetSel( l_CharRange ) ;
CWaitCursor l_WaitCursor ;
ShellExecute( this->GetSafeHwnd(), _T( "open" ), l_URL, NULL, NULL, SW_SHOWNORMAL );
*pResult=1;
}
break;
}
}