刚在做小飞鸽的文件传送,要在richeidt上提示:XXX向你发送了XX文件,是 否接收,是和否都要使用超链接,
贴个代码吧:
int nTotalLen = m_EidtRecod.GetWindowTextLength(); m_EidtRecod.SetSel(nTotalLen, nTotalLen); CString str; str.Format(_T("%s 向你发送文件:%s 是否接受?\r\n "), m_szName, pFile->m_szName); m_EidtRecod.ReplaceSel((LPCTSTR)str); CHARFORMAT cf = {0}; cf.cbSize = sizeof(CHARFORMAT); cf.dwMask = CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_UNDERLINE|CFM_UNDERLINETYPE|CFM_LINK cf.dwEffects = CFE_LINK|CFE_UNDERLINE; cf.crTextColor = RGB(255, 0, 0); // 设置超链接 nTotalLen = m_EidtRecod.GetWindowTextLength(); str.Format(_T("(Yes)是")); m_EidtRecod.ReplaceSel((LPCTSTR)str); int iEndPos = m_EidtRecod.GetWindowTextLength(); m_EidtRecod.SetSel(iEndPos-10, iEndPos); m_EidtRecod.SetSelectionCharFormat(cf); // 设置超链接 m_EidtRecod.SetSel(iEndPos, iEndPos); m_EidtRecod.ReplaceSel(_T(" ")); nTotalLen = m_EidtRecod.GetWindowTextLength(); m_EidtRecod.ReplaceSel(_T("(No)否")); iEndPos = m_EidtRecod.GetWindowTextLength(); m_EidtRecod.SetSel(iEndPos-10, iEndPos); m_EidtRecod.SetSelectionCharFormat(cf); m_EidtRecod.SetSel(iEndPos, iEndPos); m_EidtRecod.ReplaceSel(_T("\r\n")); // 使RichEdit响应ENM_LINK事件 m_EidtRecod.SetEventMask(ENM_LINK); m_EidtRecod.LineScroll(1);
父类加个消息映射:
afx_msg void OnLink(LPNMHDR pnmh, LRESULT *pResult);
ON_NOTIFY(EN_LINK, IDC_RICHEDIT_RECODE, OnLink)
void XXX::OnLink(LPNMHDR pnmh, LRESULT *pResult) { ENLINK *pEnLink = (ENLINK *)pnmh; if(pEnLink->msg == WM_LBUTTONDOWN) { m_EidtRecod.SetSel(pEnLink->chrg); CString strLink = m_EidtRecod.GetSelText(); if (-1 != strLink.Find(_T("是"))) { } else if(-1 != strLink.Find(_T("否"))) { } } pResult = 0; }
也可以子类化richeidt,通过消息反射函数。
再贴个超链接通用的代码吧:
DWORD Mask = m_edit.GetEventMask(); Mask = Mask | ENM_LINK | ENM_MOUSEEVENTS | ENM_SCROLLEVENTS | ENM_KEYEVENTS; m_edit.SetEventMask(Mask); ::SendMessage(m_edit.m_hWnd, EM_AUTOURLDETECT, true, 0); m_edit.SetWindowText(_T("12222 www.baidu.com ooooie\r\nddfdf www.baidu.com kkkdkf"));
这样所有的www.什么的都变蓝色加下划线
再加个响应代码
ON_NOTIFY(EN_LINK, IDC_RICHEDIT21, OnLink)
void XXXDlg::OnLink(LPNMHDR pnmh, LRESULT *pResult) { ENLINK* pLink = ( ENLINK* )pnmh ; *pResult = 0; switch (pLink->msg) { case WM_LBUTTONDOWN://如果鼠标左键被点击 { CString strURL; CHARRANGE crCharRange; CRichEditCtrl * pTempEdit = (CRichEditCtrl*)CRichEditCtrl::FromHandle(pLink->nmhdr.hwndFrom); pTempEdit->GetSel(crCharRange); //得到原来选中的字符 pTempEdit->SetSel(pLink->chrg); //设置RUL被选中 strURL = pTempEdit->GetSelText(); //得到URL pTempEdit->SetSel( crCharRange ); //恢复原来被选中的字符 CWaitCursor l_WaitCursor ; //用当前默认的浏览器打URL ShellExecute(this->GetSafeHwnd(), _T("open"), strURL, NULL, NULL, SW_SHOWNORMAL); *pResult = 1; } break ; case WM_LBUTTONUP: { *pResult = 1; } break ; default: break ; } }
收工.