实现代码就一点,就是用这个类来Load Richedit20
1_AFX_RICHEDITEX_STATE::_AFX_RICHEDITEX_STATE() 2{ 3 m_hInstRichEdit20 = NULL ; 4} 5 6 7 8_AFX_RICHEDITEX_STATE::~_AFX_RICHEDITEX_STATE() 9{ 10 if( m_hInstRichEdit20 != NULL ) 11 { 12 ::FreeLibrary( m_hInstRichEdit20 ) ; 13 } 14} 15 16 17 18_AFX_RICHEDITEX_STATE _afxRichEditStateEx ; 19 20 21 22BOOL PASCAL AfxInitRichEditEx() 23{ 24 if( ! ::AfxInitRichEdit() ) 25 { 26 return FALSE ; 27 } 28 _AFX_RICHEDITEX_STATE* l_pState = &_afxRichEditStateEx ; 29 if( l_pState->m_hInstRichEdit20 == NULL ) 30 { 31 l_pState->m_hInstRichEdit20 = LoadLibraryA("RICHED20.DLL") ; 32 } 33 return l_pState->m_hInstRichEdit20 != NULL ; 34} 然后在我们创建RichEdit的时候,就使用 RichEdit20A 作为ClassName;
1CreateEx(dwExStyle, _T( "RichEdit20A" ), NULL, dwStyle, rect, pParentWnd, nID, NULL ); 这样一来,你就使用先进的RichEdit20A了, 可以简单的实现你的要的功能了。先给你的RichEditCtrl设置EN_LINK Style, 代码量也是少得可怜:
1unsigned mask = ::SendMessage(m_hWnd, EM_GETEVENTMASK, 0, 0); 2::SendMessage(m_hWnd, EM_SETEVENTMASK, 0, mask | ENM_LINK | ENM_MOUSEEVENTS | ENM_SCROLLEVENTS | ENM_KEYEVENTS); 3::SendMessage(m_hWnd, EM_AUTOURLDETECT, true, 0); 如果你觉得使用CRichEditCtrl的Member Function更方便的话,当然可以使用GetEventMak和SetEventMask,同样方便。为了扩展的需要,RichEdit20并没有定义为,当你点击links的时候用浏览器打开这个页面,而是让你处理这个点击,你就可以任意的扩展!
ON_NOTIFY(EN_LINK, IDC_SENDMSG, OnRichEditExLink ) 知道在哪加吧。这个IDC_SENGMSG就是RichEdit的Resource ID, OnRichEditExLink就是处理这个消息的函数咯,然后看最后的代码。
1void CMsgerDlg::OnRichEditExLink( NMHDR* in_pNotifyHeader, LRESULT* out_pResult ) 2{ 3 ENLINK* l_pENLink = ( ENLINK* )in_pNotifyHeader ; 4 *out_pResult = 0 ; 5 switch( l_pENLink->msg ) 6 { 7 default:{} 8 break ; 9 10 case WM_LBUTTONDOWN: 11 { 12 CString l_URL ; 13 CHARRANGE l_CharRange ; 14 CExtRichEdit *m_TempEdit; 15 m_TempEdit = (CExtRichEdit*)CExtRichEdit::FromHandle(l_pENLink->nmhdr.hwndFrom); 16 m_TempEdit->GetSel( l_CharRange ) ; 17 m_TempEdit->SetSel( l_pENLink->chrg ) ; 18 l_URL = m_TempEdit->GetSelText() ; 19 m_TempEdit->SetSel( l_CharRange ) ; 20 CWaitCursor l_WaitCursor ; 21 ShellExecute( this->GetSafeHwnd(), _T( "open" ), l_URL, NULL, NULL, SW_SHOWNORMAL ) ; 22 *out_pResult = 1 ; 23 } 24 break ; 25 26 case WM_LBUTTONUP: 27 { 28 *out_pResult = 1 ; 29 } 30 break ; 31 } 超链接就说到这里,下面是背景跟字体。
|