CRichEditCtrl相关应用技巧收集

MFC 同时被 2 个专栏收录
10 篇文章 0 订阅
47 篇文章 1 订阅

在一些交互性很强的程序中,经常需要使用CRichEditCtrl控件,但是如何进行编辑文本,如设置文本字体,颜色,链接,插入图片等等,如何进行操 作,这儿就转载一篇文章,收集一些常见的问题和解决方法,说不定哪一天就用到了。我以前开发过一个聊天程序,调用了一个人开发的CMyRichEdit扩 展,实现了字体,颜色,添加链接,插入图片(QQ控件)等等。

richedit怎样加载JPG文件(显示出来)?

从CRichEditCtrl继承一个类CMyRichEdit,添加函数

void CMyRichEdit::InsertGraphics(CString strPicPath)
{
LPLOCKBYTES lpLockBytes = NULL;
SCODE sc;
HRESULT hr;     
//print to RichEdit' s IClientSite
LPOLECLIENTSITE m_lpClientSite;
//A smart point to IAnimator
ImageOleLib::IGifAnimatorPtr m_lpAnimator;
//ptr 2 storage     
LPSTORAGE m_lpStorage;
//the object 2 b insert 2
LPOLEOBJECT m_lpObject;
//Create lockbytes
    sc = ::CreateILockBytesOnHGlobal(NULL, TRUE, &lpLockBytes);
if (sc != S_OK)
AfxThrowOleException(sc);
ASSERT(lpLockBytes != NULL);
//use lockbytes to create storage
sc = ::StgCreateDocfileOnILockBytes(lpLockBytes,
STGM_SHARE_EXCLUSIVE|STGM_CREATE|STGM_READWRITE, 0, &m_lpStorage);
if (sc != S_OK)
{
VERIFY(lpLockBytes->Release() == 0);
lpLockBytes = NULL;
AfxThrowOleException(sc);
}
ASSERT(m_lpStorage != NULL);
//get the ClientSite of the very RichEditCtrl
GetIRichEditOle()->GetClientSite(&m_lpClientSite);
ASSERT(m_lpClientSite != NULL);
try
    {
//Initlize COM interface

hr = ::CoInitialize(NULL) ;//( NULL, COINIT_APARTMENTTHREADED );
if( FAILED(hr) )
_com_issue_error(hr);

//Get GifAnimator object
//here, I used a smart point, so I do not need to free it
hr = m_lpAnimator.CreateInstance(ImageOleLib::CLSID_GifAnimator);
if( FAILED(hr) )
_com_issue_error(hr);
//COM operation need BSTR, so get a BSTR
BSTR path = strPicPath.AllocSysString();

//Load the gif
hr = m_lpAnimator->LoadFromFile(path);
if( FAILED(hr) )
_com_issue_error(hr);

TRACE0( m_lpAnimator->GetFilePath() );

//get the IOleObject
hr = m_lpAnimator.QueryInterface(IID_IOleObject, (void**)&m_lpObject);
if( FAILED(hr) )
_com_issue_error(hr);

//Set it 2 b inserted
OleSetContainedObject(m_lpObject, TRUE);

//2 insert in 2 richedit, you need a struct of REOBJECT
REOBJECT reobject;
ZeroMemory(&reobject, sizeof(REOBJECT));

reobject.cbStruct = sizeof(REOBJECT);
CLSID clsid;
sc = m_lpObject->GetUserClassID(&clsid);
if (sc != S_OK)
AfxThrowOleException(sc);
//set clsid
reobject.clsid = clsid;
//can be selected
reobject.cp = REO_CP_SELECTION;
//content, but not static
reobject.dvaspect = DVASPECT_CONTENT;
//goes in the same line of text line
reobject.dwFlags = REO_BELOWBASELINE; //REO_RESIZABLE |
reobject.dwUser = 0;
//the very object
reobject.poleobj = m_lpObject;
//client site contain the object
reobject.polesite = m_lpClientSite;
//the storage 
reobject.pstg = m_lpStorage;

SIZEL sizel;
sizel.cx = sizel.cy = 0;
reobject.sizel = sizel;
HWND hWndRT = this->m_hWnd;
//Sel all text
// ::SendMessage(hWndRT, EM_SETSEL, 0, -1);
// DWORD dwStart, dwEnd;
// ::SendMessage(hWndRT, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
// ::SendMessage(hWndRT, EM_SETSEL, dwEnd+1, dwEnd+1);
//Insert after the line of text
GetIRichEditOle()->InsertObject(&reobject);
::SendMessage(hWndRT, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
VARIANT_BOOL ret;
//do frame changing
ret = m_lpAnimator->TriggerFrameChange();
//show it
m_lpObject->DoVerb(OLEIVERB_UIACTIVATE, NULL, m_lpClientSite, 0, m_hWnd, NULL);
m_lpObject->DoVerb(OLEIVERB_SHOW, NULL, m_lpClientSite, 0, m_hWnd, NULL);

//redraw the window to show animation
RedrawWindow();

if (m_lpClientSite)
{
m_lpClientSite->Release();
m_lpClientSite = NULL;
}
if (m_lpObject)
{
m_lpObject->Release();
m_lpObject = NULL;
}
if (m_lpStorage)
{
m_lpStorage->Release();
m_lpStorage = NULL;
}

SysFreeString(path);
}
catch( _com_error e )
{
AfxMessageBox(e.ErrorMessage());
::CoUninitialize();
}
}


调用

m_RichEditCtrl.InsertGraphics("C:\\123.jpg");

PS: 支持多种图像格式,不仅仅JPG.

如果运行失败,请在C***App::InitInstance添加
AfxOleInit();
AfxInitRichEdit();


m_edit1代表ID为IDC_EDIT1的CEdit控件的control类型的变量
        m_richedit1代表ID为IDC_RICHEDIT1的CRichEditCtrl控件的control类型的变量

 
________________________________________
1.设置edit只读属性

    方法一:
                m_edit1.SetReadOnly(TRUE);
    方法二:
                ::SendMessage(m_edit1.m_hWnd, EM_SETREADONLY, TRUE, 0);

 
________________________________________
2.判断edit中光标状态并得到选中内容(richedit同样适用)

        int nStart, nEnd;
        CString strTemp;

        m_edit1.GetSel(nStart, nEnd);
        if(nStart == nEnd)
        {
            strTemp.Format(_T("光标在%d"), nStart);
            AfxMessageBox(strTemp);
        }
        else
        {
            //得到edit选中的内容     
            m_edit1.GetWindowText(strTemp);
            strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
            AfxMessageBox(strTemp); 
        }
    注:GetSel后,如果nStart和nEnd,表明光标处于某个位置(直观来看就是光标在闪动);
             如果nStart和nEnd不相等,表明用户在edit中选中了一段内容。

________________________________________
3.在edit最后添加字符串

        CString str;
        m_edit1.SetSel(-1, -1);
        m_edit1.ReplaceSel(str);

 
________________________________________
4.随输入自动滚动到最后一行(richedit同样适用)

    方法一:(摘自msdn)
        // The pointer to my edit.
        extern CEdit* pmyEdit;
        int nFirstVisible = pmyEdit->GetFirstVisibleLine();

        // Scroll the edit control so that the first visible line
        // is the first line of text.
        if (nFirstVisible > 0)
        {
            pmyEdit->LineScroll(-nFirstVisible, 0);
        }
    方法二:
        m_richedit.PostMessage(WM_VSCROLL, SB_BOTTOM, 0);

________________________________________
5.如何限制edit输入指定字符

   可以从CEdit派生一个类,添加WM_CHAR消息映射。下面一个例子实现了限定输入16进制字符的功能。

   void CMyHexEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)  
   { 
        if ( (nChar >= '0' && nChar <= '9') || 
             (nChar >= 'a' && nChar <= 'f') || 
             (nChar >= 'A' && nChar <= 'F') || 
              nChar == VK_BACK ||  
              nChar == VK_DELETE)    //msdn的virtual key
       { 
            CEdit::OnChar(nChar, nRepCnt, nFlags); 
        }     
   }

 
________________________________________
6.如何使用richedit

    添加AfxInitRichEdit();
       CxxxApp::InitInstance()
        {
             AfxInitRichEdit();
          .............
       }

   AfxInitRichEdit()功能:装载 RichEdit 1.0 Control (RICHED32.DLL).

 
________________________________________
7.如何使用richedit2.0 or richedit3.0

    使用原因:由于RichEdit2.0A自动为宽字符(WideChar),所以它可以解决中文乱码以及一些汉字问题

    方法一:(msdn上的做法,适用于用VC.NET及以后版本创建的工程)
            To update rich edit controls in existing Visual C++ applications to version 2.0,
            open the .RC file as text, change the class name of each rich edit control from   "RICHEDIT" to  "RichEdit20a". 
            Then replace the call to AfxInitRichEdit with AfxInitRichEdit2.
    方法二:以对话框为例:
       (1)    增加一全局变量 HMODULE hMod;
       (2)    在CxxxApp::InitInstance()中添加一句hMod = LoadLibrary(_T("riched20.dll"));
              在CxxxApp::ExitInstance()中添加一句FreeLibrary(hMod);
       (3)      在对话框上放一个richedit,文本方式打开.rc文件修改该richedit控件的类名"RICHEDIT" to  "RichEdit20a".
       (4)      在对话框头文件添加 CRichEditCtrl m_richedit;
              在OnInitDialog中添加 m_richedit.SubclassDlgItem(IDC_RICHEDIT1, this);

________________________________________
8.改变richedit指定区域的颜色及字体

        CHARFORMAT cf;
        ZeroMemory(&cf, sizeof(CHARFORMAT));
        cf.cbSize = sizeof(CHARFORMAT);
        cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
                            CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
        cf.dwEffects = 0;
        cf.yHeight = 12*12;//文字高度
        cf.crTextColor = RGB(200, 100, 255); //文字颜色
        strcpy(cf.szFaceName ,_T("隶书"));//设置字体
     
        m_richedit1.SetSel(1, 5); //设置处理区域
        m_richedit1.SetSelectionCharFormat(cf);

 
________________________________________
9.设置行间距(只适用于richedit2.0)

        PARAFORMAT2 pf;
        pf2.cbSize = sizeof(PARAFORMAT2);
        pf2.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
        pf2.dyLineSpacing = 200;
        pf2.bLineSpacingRule  = 4;
        m_richedit.SetParaFormat(pf2);

 
________________________________________
10.richedit插入位图

Q220844:How to insert a bitmap into an RTF document using the RichEdit control in Visual C++ 6.0
http://support.microsoft.com/default.aspx?scid=kb;en-us;220844 
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/ 
http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/

 
________________________________________
11.richedit插入gif动画

http://www.codeproject.com/richedit/AnimatedEmoticon.asp

________________________________________
12.richedit嵌入ole对象

http://support.microsoft.com/kb/141549/en-us

________________________________________
13.使richedit选中内容只读

http://www.codeguru.com/cpp/controls/richedit/article.php/c2401/

 
________________________________________
14.打印richedit

http://www.protext.com/MFC/RichEdit3.htm

 
________________________________________
15.richeidt用于聊天消息窗口

http://www.vckbase.com/document/viewdoc/?id=1087 
http://www.codeproject.com/richedit/chatrichedit.asp 
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2395/

________________________________________
16.解决richedit的EN_SETFOCUS和EN_KILLFOCUS无响应的问题

http://support.microsoft.com/kb/181664/en-us

 
________________________________________
17.richedit拼写检查

http://www.codeproject.com/com/AutoSpellCheck.asp

 
________________________________________
18.改变edit背景色

Q117778:How to change the background color of an MFC edit control
http://support.microsoft.com/kb/117778/en-us

 
________________________________________
19.当edit控件的父窗口属性是带标题栏WS_CAPTION和子窗口WS_CHILD时,不能设置焦点SetFocus

Q230587:PRB: Can't Set Focus to an Edit Control When its Parent Is an Inactive Captioned Child Window
http://support.microsoft.com/kb/230587/en-us

________________________________________
20. 在Edit中回车时,会退出对话框 

选中Edit的风格Want Return。

MSDN的解释如下:
ES_WANTRETURN   Specifies that a carriage return be inserted when the user presses the ENTER key while entering text into a multiple-line edit control in a dialog box. Without this style, pressing the ENTER key has the same effect as pressing the dialog box's default pushbutton. This style. has no effect on a single-line edit control.

 
________________________________________
21. 动态创建的edit没有边框的问题

    m_edit.Create(....);
    m_edit.ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);


CRichEditCtrl大全


CRichEditCtrl的使用可以编译,不能执行的

在需要在相应的对话框中加上InitInstance(void)函数中添加

AfxInitRichEdit();

b.升级默认的Riched版本(默认的有一些bug),如
可在InitInstance中添加
LoadLibrary("RICHED20.DLL") 
最后注意 FreeLibrary

如果是CRichEditView基类的可用
BOOL CXXXXXXView::PreCreateWindow(CREATESTRUCT& cs)
{
//装入rich edit version 2.0
if (LoadLibraryA("RICHED20.DLL") == NULL)
{
AfxMessageBox(_T("Fail to load \"riched20.dll\"."),MB_OK | MB_ICONERROR);
PostMessage(WM_QUIT,0,0);
return FALSE;
}

m_strClass = RICHEDIT_CLASSA;//for 2.0 class

return CRichEditView::PreCreateWindow(cs);
}

c.最后追加行
richeditctrl.SetSel(-1, -1);
richeditctrl.ReplaceSel( (LPCTSTR)str );

d.字数限制
CRichEditCtrl::LimitText(long nChars)

e.换行切换
CRichEditView的OnInitialUpdate()函数中加入下面两句:
m_nWordWrap = WrapNone;
WrapChanged(); 
WrapChanged实际上也是调用
ctrl.SetTargetDevice(NULL, 1); //m_nWordWrap == WrapNone
ctrl.SetTargetDevice(NULL, 0); //m_nWordWrap == WrapToWindow
还有不常用的 m_nWordWrap == WrapToTargetDevice
ctrl.SetTargetDevice(m_dcTarget, GetPrintWidth());
如果是在Dialog中,可使用SetTargetDevice,注意在属性里面加上want return

f.有时候不希望带格式的数据粘贴,可通过PasteSpecial选择性粘贴
pmyRichEditCtrl->PasteSpecial(CF_TEXT);

g.随着输入随着自动滚动条滚动到最后一行
int nFirstVisible = pmyRichEditCtrl->GetFirstVisibleLine();
if (nFirstVisible > 0)
{
    pmyRichEditCtrl->LineScroll(-nFirstVisible, 0);
}

m_cRichEdit.PostMessage(WM_VSCROLL, SB_BOTTOM,0);


h.设置UNDO的次数(只能用在RICHED20以上,即默认不支持,必须升级)
SendMessage(EM_SETTEXTMODE,TM_MULTILEVELUNDO,0);
TM_MULTILEVELUNDO 支持多取消(默认值).可通过EM_SETUNDOLIMIT设置最大次数 
SendMessage(EM_SETUNDOLIMIT,100,0);

i.响应OnChange
EM_SETEVENTMASK 设置 ENM_CHANGE 
long lMask = GetEventMask();
lMask |= ENM_CHANGE;
lMask &= ~ENM_PROTECTED;
SetEventMask(lMask);

j.设置只读
CRichEditCtrl::SetReadOnly( BOOL bReadOnly = TRUE );
通过设置PROTECTED实现选中的文本只读,参见
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2401/


二.函数应用
a.设置字体(主要是通过SetSelectionCharFormat)
CHARFORMAT cf;
rich.GetSelectionCharFormat(cf);
cf.dwMask|=CFM_BOLD;
cf.dwEffects|=CFE_BOLD;//设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
cf.dwMask|=CFM_ITALIC;
cf.dwEffects|=CFE_ITALIC;//设置斜体,取消用cf.dwEffects&=~CFE_ITALIC;
cf.dwMask|=CFM_UNDERLINE;
cf.dwEffects|=CFE_UNDERLINE;//设置斜体,取消用cf.dwEffects&=~CFE_UNDERLINE;
cf.dwMask|=CFM_COLOR;
cf.crTextColor = RGB(255,0,0);//设置颜色
cf.dwMask|=CFM_SIZE;
cf.yHeight =200;//设置高度
cf.dwMask|=CFM_FACE;
strcpy(cf.szFaceName ,_T("隶书"));//设置字体
rich.SetSelectionCharFormat(cf);

b.设置字体的行间距
要用richedit2.0以上
试试
PARAFORMAT2 pf;
pf.cbSize = sizeof(PARAFORMAT2);
pf.dwMask = PFM_NUMBERING | PFM_OFFSET;
pf.wNumbering = PFN_BULLET;//注意PFM_NUMBERING 
pf.dxOffset = 10;
VERIFY(SetParaFormat(pf)); 
常用的dwMask有
PFM_NUMBERING 成员 wNumbering 才起作用,项目符号,默认用PFN_BULLET
2 使用阿拉伯数字 (1, 2, 3, ...).  
3 使用小写字母 (a, b, c, ...).  
4 使用大写字母 (A, B, C, ...).  
5 使用小写罗马数字 (i, ii, iii, ...).  
6 使用大写罗马数字 (I, II, III, ...).  
7 自定义,字符见成员 wNumberingStart.  
PFM_OFFSET 成员 dxOffset 才起作用,缩进,单位twips
PFM_STARTINDENT 成员 dxStartIndent 才起作用,首行缩进
PFM_SPACEAFTER 成员 dySpaceAfter 才起作用,段间距
PFM_LINESPACING 成员 dyLineSpacing 才起作用,行间距

c.设置CRichEditCtrl(2.0)背景透明 
long style = ::GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE);
style &= WS_EX_TRANSPARENT;
::SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, style);
或 CreateEx,然后把WS_EX_TRANSPARENT样式加上

e.得到内容有三种
1)GetWindowText
2)使用EM_GETTEXTEX
GETTEXTEX gt;
gt.cb = 200;
gt.flags = GT_DEFAULT;
gt.codepage = CP_ACP ;
gt.lpDefaultChar = NULL; 
gt.lpUsedDefChar = NULL;
SendMessage(EM_GETTEXTEX,(WPARAM)&gt,(LPARAM)text);
3)StreamOut(主要用于RTF等格式输出)
static DWORD CALLBACK 
MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
    CFile* pFile = (CFile*) dwCookie;

    pFile->Write(pbBuff, cb);
    *pcb = cb;

    return 0;
}


    CFile cFile(TEXT("myfile.rtf"), CFile::modeCreate|CFile::modeWrite);
    EDITSTREAM es;
    es.dwCookie = (DWORD) &cFile;//设置用例参数,以便回调函数调用
    es.pfnCallback = MyStreamOutCallback; 
    pmyRichEditCtrl->StreamOut(SF_RTF, es);
读入可以此类推,SetWindowText,EM_SETTEXTEX,StreamIn

f.查找字符串
FINDTEXTEX ft;
ft.chrg.cpMin = 0;
ft.chrg.cpMax = -1;
ft.lpstrText = "|";
long lPos = FindText(0, &ft);

如果要继续查找,修改cpMin,如
int nCount = 0;
do
{
long lPos = GetRichEditCtrl().FindText(0, &ft);
if( -1 == lPos) break;
ft.chrg.cpMin = lPos + strlen(ft.lpstrText);
++nCount;
}while(TRUE);

g.以Html格式保存
目前做法可先转为RTF格式,再通过RTF-to-HTML Converter

http://www.codeguru.com/Cpp/controls/richedit/conversions/article.php/c5377/

h.重载OnProtected函数得到对应的消息,如粘贴等
void CMYichEditorView::OnProtected(NMHDR* pNMHDR, LRESULT* pResult)
{
ENPROTECTED* pEP = (ENPROTECTED*)pNMHDR;

switch (pEP->msg) {
case WM_KEYDOWN://按键,判断pEP->wParam
case WM_PASTE://粘贴
case WM_CUT://剪切
case EM_SETCHARFORMAT:
default:
   break;
};

*pResult = FALSE;
}

三.聊天常用  
a.LINK 链接功能
1.   LoadLibrary(_T("Riched20.dll"));
2. 创建RichEdit2.0控件
CreateEx(0, _T("RichEdit20A"), NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP
|ES_READONLY|ES_WANTRETURN|ES_MULTILINE,
           rect.left, rect.top, cx, cy,
   pParentWnd->m_hWnd, (HMENU)nID, NULL);
3. 设定选中的文字为链接显示
CHARFORMAT2 cf2;
ZeroMemory(&cf2, sizeof(CHARFORMAT2));//
cf2.cbSize = sizeof(CHARFORMAT2);
cf2.dwMask = CFM_LINK;
cf2.dwEffects |= CFE_LINK;
m_cRichEdit.SendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf2);
4.支持链接的点击响应
m_cRichEdit.SetEventMask(ENM_LINK);
5.响应链接的点击EN_LINK

BEGIN_MESSAGE_MAP(CMyRichEdit, CRichEditCtrl)
ON_NOTIFY_REFLECT(EN_LINK,OnURL)
END_MESSAGE_MAP()
......

void CMyRichEdit::OnURLClick(NMHDR *pNmhdr, LRESULT *pResult)
{
TCHAR LinkChar[512];
ENLINK *pLink = (ENLINK *)pNmhdr;
if (pLink->msg == WM_LBUTTONUP)
{
SetSel(penLink->chrg);//这是链接的文字范围
long Res = GetSelText((char *)LinkChar);//这是链接文字
                   //后面是你的处理过程
                   ......
          }
}


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

shaderdx

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值