作者:sonystone
下载本文的源代码
在VC中提供了两种很方便的编辑控件(CEdit 和CRichEditCtrl),一般来说这两种控件已经满足了我们大部分的需要,不过只有CEdit控件能响应我们鼠标右键消息,通过右键我们很容易的操作我们的编辑,而在CRichEditCtrl控件中我们不能得到这样的操作,同时CRichEditCtrl是能够包含各种格式的内容,就好像Word一样能够写入各种不同的字体,不过CRichEditCtrl没有给我们提供这样的要求,今天我的目的也就是给他添加这样的扩展操作。
本代码运行效果图
一. 首先我们要从CRichEditCtrl类派生一个我们自己的类CMyRichEdit,我们的操作也是在其中完成的。
二. 然后我们添加鼠标右键消息,在其中添加代码如下:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/cyuyanjiaocheng/)//设置为焦点SetFocus();//创建一个弹出式菜单CMenu popmenu;popmenu.CreatePopupMenu();//添加菜单项目popmenu.AppendMenu(0, ID_RICH_UNDO, "&Undo");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_CUT, "&Cut");popmenu.AppendMenu(0, ID_RICH_COPY, "C&opy");popmenu.AppendMenu(0, ID_RICH_PASTE, "&Paste");popmenu.AppendMenu(0, ID_RICH_CLEAR, "C&lear");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_SELECTALL, "Select &All");popmenu.AppendMenu(0, MF_SEPARATOR);popmenu.AppendMenu(0, ID_RICH_SETFONT, "Select &Font");//初始化菜单项UINT nUndo=(CanUndo() ? 0 : MF_GRAYED );popmenu.EnableMenuItem(ID_RICH_UNDO, MF_BYCOMMAND|nUndo);UINT nSel=((GetSelectionType()!=SEL_EMPTY) ? 0 : MF_GRAYED) ;popmenu.EnableMenuItem(ID_RICH_CUT, MF_BYCOMMAND|nSel);popmenu.EnableMenuItem(ID_RICH_COPY, MF_BYCOMMAND|nSel);popmenu.EnableMenuItem(ID_RICH_CLEAR, MF_BYCOMMAND|nSel);UINT nPaste=(CanPaste() ? 0 : MF_GRAYED) ;popmenu.EnableMenuItem(ID_RICH_PASTE, MF_BYCOMMAND|nPaste);//显示菜单CPoint pt;GetCursorPos(&pt);popmenu.TrackPopupMenu(TPM_RIGHTBUTTON, pt.x, pt.y, this);popmenu.DestroyMenu();三. 然后在.h文件中加入如下的ID定义:
#define ID_RICH_UNDO 101#define ID_RICH_CUT 102#define ID_RICH_COPY 103#define ID_RICH_PASTE 104#define ID_RICH_CLEAR 105#define ID_RICH_SELECTALL 106#define ID_RICH_SETFONT 107不过这些值还可以通过VC++编译器中的菜单View->Resource Symbols进行添加。 四. 添加消息相应操作,由于这些ID是我们自己定义的,所以我们只能手动添加:
1.在头文件中添加:
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);afx_msg void OnCopy() { Copy(); }afx_msg void OnCut() { Cut(); }afx_msg void OnPaste() { Paste(); }afx_msg void OnSelectall() { SetSel(0, -1); }afx_msg void OnUndo() { Undo(); }afx_msg void OnClear() { Clear(); }afx_msg void OnSelectfont();//改变字体2.在实现文件的消息映射宏中添加如下:
ON_COMMAND(ID_RICH_COPY, OnCopy)ON_COMMAND(ID_RICH_CUT, OnCut)ON_COMMAND(ID_RICH_PASTE, OnPaste)ON_COMMAND(ID_RICH_SELECTALL, OnSelectall)ON_COMMAND(ID_RICH_UNDO, OnUndo)ON_COMMAND(ID_RICH_CLEAR, OnClear)ON_COMMAND(ID_RICH_SETFONT, OnSelectfont)3.最后添加字体变换函数:
CHARFORMAT cf;LOGFONT lf;memset(&cf, 0, sizeof(CHARFORMAT));memset(&lf, 0, sizeof(LOGFONT));//判断是否选择了内容BOOL bSelect = (GetSelectionType() != SEL_EMPTY) ? TRUE : FALSE;if (bSelect){ GetSelectionCharFormat(cf);}else{ GetDefaultCharFormat(cf);}//得到相关字体属性BOOL bIsBold = cf.dwEffects & CFE_BOLD;BOOL bIsItalic = cf.dwEffects & CFE_ITALIC;BOOL bIsUnderline = cf.dwEffects & CFE_UNDERLINE;BOOL bIsStrickout = cf.dwEffects & CFE_STRIKEOUT;//设置属性lf.lfCharSet = cf.bCharSet;lf.lfHeight = cf.yHeight/15;lf.lfPitchAndFamily = cf.bPitchAndFamily;lf.lfItalic = bIsItalic;lf.lfWeight = (bIsBold ? FW_BOLD : FW_NORMAL);lf.lfUnderline = bIsUnderline;lf.lfStrikeOut = bIsStrickout;sprintf(lf.lfFaceName, cf.szFaceName); CFontDialog dlg(&lf);dlg.m_cf.rgbColors = cf.crTextColor;if (dlg.DoModa