posts - 126,  comments - 73,  trackbacks - 0

richedit 常见使用问题
一.常见问题
a.可以编译,不能执行的


在需要在相应的对话框中加上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);//这是链接文字
                   //后面是你的处理过程
                   ......
          }
}

b.插入位图
http://www.codeguru.com/Cpp/controls/richedit/article.php/c2417/
http://www.codeguru.com/Cpp/controls/richedit/article.php/c5383/

自定义在RichEdit中插入对象的图标
http://www.blogcn.com/user3/jiangsheng/blog/1319738.html
方法基本同Knowledge Base文章Q220844 HOWTO: Insert a Bitmap Into an RTF Document Using the RichEdit Control
只是在最后插入之前调用一下IOleCache::SetData,用一个HGLOBAL作为参数,HGLOBAL里面的数据是一个METAFILEPICT结构,包含自己提供的图片

使用CRichEditView::InsertFileAsObject就可以插入图像。VC++带有一个例子WordPad。
另外可以参考“Insert any HBITMAP (Bitmap) in your RichEdit Control”(http://www.codeguru.com/richedit/richeditrc.html)。

c.显示GIF动画
常用的是通过qq的imageole.dll(也有用Gif89.dll的)
http://www.xiaozhou.net/cooldog/blogview.asp?logID=82
http://www.codeproject.com/richedit/AnimatedEmoticon.asp

在richedit控件中插入动态GIF (Native C++版)
http://blog.joycode.com/jiangsheng/archive/2004/12/15/41209.aspx

d.IRichEditOleCallback的使用
http://61.186.252.131/Expert/topic/905/905844.xml?temp=.8379022

类似 MSN 信息发送框的制作(上)
http://www.vckbase.com/document/viewdoc/?id=1087
内容包含:实现右键菜单,图片插入,读取/写入RTF格式字符串

自定义 CRichEditCtrl 控件
http://www.vckbase.com/document/viewdoc/?id=328
内容包含:鼠标右键消息,消息映射,字体变换

PS.richedit控件升级到2.0后,先把字体设为楷体,输入汉字没有问题,但输入字母时,字母自动跳转为Arial字体,而1.0却没有这个文题,仍然是用楷体显示字母
是一个专门的设计 Dual-font, Smart font apply, 参见 http://61.186.252.131/Expert/topic/913/913807.xml?temp=.3753778

不能显示图片等其他OLE对象

MFC提供的CRichEditCtrl没有提供直接显示图片等OLE对象的属性或方法设置,但是提供了一个接口SetOLECallback( IRichEditOleCallback* pCallback );
要让CRichEditCtrl显示图片,就得在IRichEditOleCallback上下功夫。
IRichEditOleCallback是windows中的接口,它的定义如下:
ContextSensitiveHelp:
    通过该方法通知应用程序它将以上下文关联方式调度帮助。
DeleteObject:
    通过该方法发出通知:一个对象即将从RichEdit控件中删除
GetClipboardData:
    通过该方法允许RichEdit的客户端(调用程序)提供自己的粘贴对象
GetContextMenu:
    通过该方法向应用程序提出通过鼠标右键事件来获取上下文菜单的请求
GetDragDropEffect:
    通过该方法允许RichEdit的客户端(调用程序)设置拖动操作的效果
GetInPlaceContext:
   通过该方法提供了应用程序级和文档级接口,以及必要的支持In-place激活的信息
GetNewStrorage:
    通过该方法存储从粘贴板或超文本流(RTF)中读取的新对象
QueryAcceptData:
    通过该方法决定在粘贴操作或拖放操作中引入的数据是否可以被接受。
QueryInsertObject:
    通过该方法向应用程序询问某个对象是否可以被插入
ShowContainerUI:
    通过该方法告知应用程序是否显示自己的操作界面

大致了解了IRichEditOleCallback接口后,就应该清楚,要显示图片等ole对象,至少应该实现GetNewStorage方法,因为该方法是存储ole对象的接口方法。

以下是接口声明的代码:
    interface IExRichEditOleCallback;   // forward declaration (see below in this header file)

   IExRichEditOleCallback* m_pIRichEditOleCallback;
   BOOL m_bCallbackSet;
  
  
   interface IExRichEditOleCallback : public IRichEditOleCallback
   {
   public:
       IExRichEditOleCallback();
       virtual ~IExRichEditOleCallback();
       int m_iNumStorages;
       IStorage* pStorage;
       DWORD m_dwRef;

       virtual HRESULT STDMETHODCALLTYPE GetNewStorage(LPSTORAGE* lplpstg);
       virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
       virtual ULONG STDMETHODCALLTYPE AddRef();
       virtual ULONG STDMETHODCALLTYPE Release();
       virtual HRESULT STDMETHODCALLTYPE GetInPlaceContext(LPOLEINPLACEFRAME FAR *lplpFrame,
           LPOLEINPLACEUIWINDOW FAR *lplpDoc, LPOLEINPLACEFRAMEINFO lpFrameInfo);
        virtual HRESULT STDMETHODCALLTYPE ShowContainerUI(BOOL fShow);
        virtual HRESULT STDMETHODCALLTYPE QueryInsertObject(LPCLSID lpclsid, LPSTORAGE lpstg, LONG cp);
        virtual HRESULT STDMETHODCALLTYPE DeleteObject(LPOLEOBJECT lpoleobj);
        virtual HRESULT STDMETHODCALLTYPE QueryAcceptData(LPDATAOBJECT lpdataobj, CLIPFORMAT FAR *lpcfFormat,
           DWORD reco, BOOL fReally, HGLOBAL hMetaPict);
        virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL fEnterMode);
        virtual HRESULT STDMETHODCALLTYPE GetClipboardData(CHARRANGE FAR *lpchrg, DWORD reco, LPDATAOBJECT FAR *lplpdataobj);
        virtual HRESULT STDMETHODCALLTYPE GetDragDropEffect(BOOL fDrag, DWORD grfKeyState, LPDWORD pdwEffect);
        virtual HRESULT STDMETHODCALLTYPE GetContextMenu(WORD seltyp, LPOLEOBJECT lpoleobj, CHARRANGE FAR *lpchrg,
           HMENU FAR *lphmenu);
   };

-----------------------------------------------------------------------

注:
        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);

--------------------------------------------------------------------------------
22. 一个能显示RTF,ole(包括gif, wmv,excel ,ppt)的例子

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


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qsycn/archive/2009/11/10/4793461.aspx

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/abcpanpeng/archive/2010/04/21/5509680.aspx

posted on 2010-11-03 15:51 我风 阅读(21638) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2010年4月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

常用链接

留言簿(12)

随笔分类

随笔档案

文章档案

相册

收藏夹

C++

MyFavorite

搜索

  •  

积分与排名

  • 积分 - 323692
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜