CRichEditCntr类提供了带格式编辑控件的功能。这个Windows通用控件(也就是CRichEditCtrl类)只对于运行在Window95和Windows NT 3.51及更新版本下的程序是可用的。
在mfc中使用工具栏里的RichEdit 控件时,应该在程序初始化时加入AfxInitRichEdit(),或者AfxInitRichEdit2(),否则的话程序会起不来.也没有任何错误信息。
test.cpp HMODULE hMod; //增加一全局变量 BOOL CTestApp::InitInstance() { …… AfxInitRichEdit(); //装载 RichEdit 1.0 Control (RICHED32.DLL) hMod = LoadLibrary(_T("RICHED20.DLL")); //加载 Riched20.dll(Riched32.dll) // RichEdit2.0A自动为宽字符(WideChar),所以它可以解决中文乱码以及一些汉字问题 // RichEdit2.0A可设置行间距 …… } // 加载dll也可以在InitDialog中进行 BOOL CTestApp::::ExitInstance() { …… FreeLibrary(hMod); …… }
在对话框上放一个IDC_RICHEDIT1,文本方式打开.rc文件修改该richedit控件的类名"RICHEDIT" to "RichEdit20a".
在对话框头文件添加 CRichEditCtrl m_richedit;
在OnInitDialog中添加 m_richedit.SubclassDlgItem(IDC_RICHEDIT1, this);
控件类别 | category | variable type | remark |
Cedit | value | CString\int等 | 此类型的变量类似于基本类型变量,可通过全局函数UpdateData()与控件交换数据 |
CRichEidtCtrl | value | CString\int等 | |
Cedit | control | Cedit | 相当于实例化的对象,可通过其提供的方法与控件交换数据 |
CRichEidtCtrl | control | CRichEidtCtrl |
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中选中了一段内容。
<void CMy56Dlg::OnButton1() { CString str; m_edit.GetWindowText(str); //第1步:获得编辑框内容(所有的内容) int nfirst,nend; m_edit.GetSel(nfirst,nend); //第2步:获得你在编辑框所选择的内容(包括初始位置与结束位置) CString str1=str.Mid(nfirst,nend-nfirst); MessageBox("当前选择的内容是:"+str1); //第3步:根据第2步的位置获得内容 //备注:当然你也可以设置if(nfirst!=nend),屏蔽掉为空的情况(即没选择的情况) }
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); } }
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
转自
http://blog.csdn.net/lixiaosan/archive/2006/04/06/652795.aspx
Environment: VC6 SP4, 2000.
Follow these 10 easy steps to build the OutLookRichEdit control:
CRichEditCtrl m_ctrlText1;
to
COutLookRichEdit m_ctrlText1;
m_ctrlText1.SetRawHyperText(_T("Click <%$here$#100#%>to see the about box."));
At this level, if you build the project and run it, you can see the rich edit control with linked text, but nothing would happen if you clicked on the link.
To Show a dialog while the link is clicked, you have to add some more code in the dialog class. Before that, have a closer look at the preceding code and hypertext syntax. The link text is enclosed between the "$" symbols and the corresponding dialog's resource value 100 (About Box), enclosed in "#" symbols.
You can find the #define values of dialogs in the resource.h file.
BOOL CDEMODlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) { NMHDR* pNmHdr = (NMHDR*) lParam; if(IDC_RICHEDIT1 == pNmHdr->idFrom) { switch(pNmHdr->code) { case IDD_ABOUTBOX: CAboutDlg oDlg; oDlg.DoModal (); break; } } return CDialog::OnNotify(wParam, lParam, pResult); }
COleDataSource src;
STGMEDIUM sm;
sm.tymed=TYMED_GDI;
sm.hBitmap=hbmp;
sm.pUnkForRelease=NULL;
src.CacheData(CF_BITMAP, &sm);
LPDATAOBJECT lpDataObject =
(LPDATAOBJECT)src.GetInterface(&IID_IDataObject);
pRichEditOle->ImportDataObject(lpDataObject, 0, NULL);
lpDataObject->Release();
字体设置代码
最后添加字体变换函数:
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.DoModal() == IDOK)
{
dlg.GetCharFormat(cf);//获得所选字体的属性
if (bSelect)
SetSelectionCharFormat(cf); //为选定的内容设定所选字体
else
SetWordCharFormat(cf); //为将要输入的内容设定字体
}
在RichEdit中实现超链接
在RichEdit中实现超链接首先在Form上放置一个RichEdit。
在窗体的构造函数中添加以下代码:
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
unsigned mask = SendMessage(RichEdit1->Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(RichEdit1->Handle, EM_SETEVENTMASK, 0, mask | ENM_LINK);
SendMessage(RichEdit1->Handle, EM_AUTOURLDETECT, true, 0); //自动检测URL
RichEdit1->Text = "欢迎访问C++ Builder\n"
"网址: http://www.ccrun.com\n"
"偶的信箱:\n"
"mailto::info@ccrun.com \n"
"嘿嘿\n";
}
重载窗体的WndProc
1 在.h中添加:
protected:
virtual void __fastcall WndProc(Messages::TMessage &Message);
2 在.cpp中添加:
/* --------------------------------------------------------------------------- */ void __fastcall TMainForm::WndProc( Messages::TMessage & Message ) { if ( Message.Msg == WM_NOTIFY ) { if ( ( (LPNMHDR) Message.LParam) - > code == EN_LINK ) { ENLINK* p = (ENLINK *) Message.LParam; if ( p - > msg == WM_LBUTTONDOWN ) { SendMessage( RichEdit1 - > Handle, EM_EXSETSEL, 0, (LPARAM) & (p - > chrg) ); ShellExecute( Handle, " open " , RichEdit1 - > SelText.c_str(), 0, 0, SW_SHOWNORMAL ); } } } TForm::WndProc( Message ); }
vc++快速使用richedit控件
CXXXApp::CXXXApp() //找到应用类
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
AfxInitRichEdit(); //此句必须加
LoadLibrary(_T("RICHED20.DLL")); // 或LoadLibrary(_T("RICHED32.DLL"));
}
2)使用wizard加入RichEdit的变量cstring类型m_richtext,以及控件control类型m_richctrl
就可以方便使用整个RichEdit了。
3)设置want return就可以直接回车换行,否则要ctrl+enter才能换行。
4)可以用m_richtext.find("\r\n")来找到换行符,找到的次数合计就是行数了,当然也有更高级方法sendmessage XXX
5)例如要获取某一行文字的傻瓜办法是用strtok函数来处理m_richtext。
6)如果要响应Rich Edit控件的OnEnChange事件,
//CRichEditCtrl().SetEventMask(ENM_CHANGE);
GetDlgItem(RichEditID)->SetEventMask(ENM_CHANGE);
或者
m_CtrlRichEdit.SetEventMask(ENM_CHANGE);
其中m_CtrlRichEdit是Rich Edit关联的控件变量,非CString或其他变量。
在Edit控件OnEnChange的事件中不要随便用UpdateData(FALSE),因为当输入字符超过256个时,输入焦点会自动跳转到首行第一格
//富编辑框ID:IDC_EDIT2
//富编辑框变量类型:CRichEditCtrl,名称:m_text
//富编辑框清空;
//必须是CRichEditCtrl数据类型,如果是CString类型,不能用下面的方面清空;
void CVCRichEditDlg::OnButton1() { //UpdateData(1); //设置选中编辑框中的所有内容 // IDC_EDIT2.SetSel(0,-1); //替换编辑框中的内容为空 //IDC_EDIT2.ReplaceSel(""); CEdit* pedt2 = (CEdit*)GetDlgItem(IDC_EDIT2); pedt2->SetWindowText(""); //SetDlgItemText(IDC_EDIT2,NULL); UpdateData(0); }
//将富编辑框的内容给到CString普通编辑框;
void CVCRichEditDlg::OnButton2() { CString m_text; UpdateData(1); CEdit *edit1=(CEdit*)GetDlgItem(IDC_EDIT2); edit1->GetWindowText(m_text); if(m_text == "") { m_text4 = "不能为空" ; } else { m_text4 = m_text ; } UpdateData(0); }
//将富编辑框的内容给到CEdit编辑框;
void CVCRichEditDlg::OnButton3() { CString m_text; UpdateData(1); CEdit *edit1=(CEdit*)GetDlgItem(IDC_EDIT2); edit1->GetWindowText(m_text); if(m_text == "") { m_text2.SetSel(0, -1); m_text2.ReplaceSel("不能为空!" ); } else { CString m_text12; CEdit *edit1=(CEdit*)GetDlgItem(IDC_EDIT2); //CEdit *edit2=(CEdit*)GetDlgItem(IDC_RICHEDIT2); edit1->GetWindowText(m_text12);//获取文本1的内容 赋值到CString变量里面; //edit2->GetWindowText(m_text12);//把CString类型的m_strr内容显示在文本框2 m_text2.SetSel(0, -1); m_text2.ReplaceSel(m_text12); } UpdateData(0); }
//将富编辑框的内容给到富编辑框;
void CVCRichEditDlg::OnButton4() { CString m_text; UpdateData(1); CEdit *edit1=(CEdit*)GetDlgItem(IDC_EDIT2); edit1->GetWindowText(m_text); if(m_text == "") { m_textrich.SetSel(0, -1); m_textrich.ReplaceSel("不能为空!" ); } else { CString m_text12; CEdit *edit1=(CEdit*)GetDlgItem(IDC_EDIT2); //CEdit *edit2=(CEdit*)GetDlgItem(IDC_RICHEDIT2); edit1->GetWindowText(m_text12);//获取文本1的内容 赋值到CString变量里面; //edit2->GetWindowText(m_text12);//把CString类型的m_strr内容显示在文本框2 m_textrich.SetSel(0, -1); m_textrich.ReplaceSel(m_text12); } UpdateData(0); }
CRichEditCtrl 构造一个CRichEditCtrl对象
Create创建Windows带格式编辑控件并将它与这个CRichEditCtrl对象相联系
GetLineCount获取这个CRichEditCtrl对象中的行数目
GetLine从这个CRichEditCtrl对象中获取一行文本
GetFirstVisibleLine确定这个CRichEditCtrl对象的最上面的可见行
LineIndex获取此CRichEditCtrl对象中一个给定行的字符索引
LineFromChar确定是哪一行包含了给定字符
LineLength获取此CRichEditCtrl对象中的给定行的长度
LineScroll在此CRichEditCtrl对象中滚动文本
Clear清除当前选择
GetSel获取此CRichEditCtrl对象中的当前选择的开始和结束位置
SetSel设置此CRichEditCtrl对象中的选择
GetSelText获取此CRichEditCtrl对象中的当前选择的文本
GetSelectionType获取此CRichEditCtrl对象中的当前选择中内容的类型
ReplaceSel用指定的文本替换此CRichEditCtrl对象中的当前选择
HideSelection显示或隐藏当前的选择
GetDefaultCharFormat获取此CRichEditCtrl对象中当前缺省的字符格式属性
SetDefaultCharFormat设置此CRichEditCtrl对象中的当前缺省字符格式的属性
GetSelectionCharFormat获取此CRichEditCtrl对象中当前选择的字符格式属性
SetSelectionCharFormat设置此CRichEditCtrl对象中当前选择的字符格式属性
GetParaFormat获取此CRichEditCtrl对象中的当前选择的段落格式属性
SetParaFormat设置此CRichEditCtrl对象中的当前选择的段落格式属性
SetWordCharFormat设置此CRichEditCtrl对象中的当前单词的字符格式属性
Undo取消最后一次编辑操作
CanUndo确定是否可以取消一次编辑操作
EmptyUndoBuffer重置(清除)此CRichEditCtrl对象的取消标志
StreamIn将来自一个输入流的文本插入此CRichEditCtrl对象中
StreamOut将来自此CRichEditCtrl对象的文本保存到输出流中
GetModify确定在最后一次保存后此CRichEditCtrl对象的内容是否已经被改变了
SetModify为这个CRichEditCtrl对象设置或清除修改标志
FindText在这个CRichEditCtrl对象中定位文本
GetRect为此CRichEditCtrl对象获取格式化矩形
SetRect为此CRichEditCtrl对象设置格式化矩形
GetCharPos确定此CRichEditCtrl对象中的一个给定字符的位置
SetOptions为这个CRichEditCtrl对象设置选项
SetReadOnly为这个CRichEditCtrl对象设置只读选项
GetTextLength获取此CRichEditCtrl对象中的文本的长度
GetLimitText获取一个用户可以输入这个CRichEditCtrl对象的文本数量的限制
LimitText限制一个用户可以输入此CRichEditCtrl对象的文本数量
GetEventMask获取此CRichEditCtrl对象的事件掩码
SetEventMask设置此CRichEditCtrl对象的事件掩码
RequestResize强迫此CRichEditCtrl对象发送请求改变大小的通知
SetBackgroundColor设置此CRichEditCtrl对象中的背景颜色
SetTargetDevice设置此CRichEditCtrl对象的目标输出设备
FormatRange为目标输出设备格式化一个文本范围
DisplayBand显示此CRichEditCtrl对象的一部分内容
Copy将当前选项拷贝到剪贴板上
Cut将存取选择剪下到剪贴板上
Paste剪贴板上的内容插入到此带格式编辑控件中
PasteSpecial将剪贴板上的内容按指定的数据格式插入到此带格式编辑控件中
CanPaste确定剪贴板上的内容是否可以粘贴到此带格式编辑控件中
GetIRichEditOle为此带格式编辑控件获取一个指向IrichEdit Ole接口的指针
SetOLECallback为此带格式编辑控件设置IrichEditOleCallback COM对象