注:m_richedit代表ID為IDC_RICHEDIT1的CRichEditCtrl控制項的control類型的變數
1. 如何使用richedit添加AfxInitRichEdit();
CxxxApp::InitInstance()
{
AfxInitRichEdit();
.............
}
AfxInitRichEdit()功能:裝載 RichEdit 1.0 Control (RICHED32.DLL).
2. 改變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_richedit.SetSel(1, 5); //設定處理地區
m_richedit.SetSelectionCharFormat(cf);
3. 改變richedit指定地區的文字背景顏色
CHARFORMAT2 cf; //聲明為CHARFORMAT2結構,詳細見MSDN
ZeroMemory(&cf, sizeof(CHARFORMAT2));
cf.cbSize = sizeof(CHARFORMAT2);
cf.dwMask = CFM_BACKCOLOR; cf.crBackColor=RGB(0, 255, 0); //背景顏色為綠色
m_richedit.SetSel(0, 2); //設定處理地區
m_richedit.SendMessage(EM_SETCHARFORMAT,SCF_SELECTION,(LPARAM)&cf);
4.只修改指定地區文字的背景顏色
先指定選擇地區
m_richedit1.SetSel(0, 19); //設定處理地區
m_richedit1.HideSelection(FALSE, TRUE);//顯示選擇的地區背景 很重要
Changes the visibility of the selection.
| |
void HideSelection( BOOL bHide, BOOL bPerm ); |
Parameters
-
bHide
-
Indicates if the selection should be shown or hidden, TRUE to hide the selection.
-
bPerm
-
Indicates if this change in visibility for the selection should be permanent.
Remarks
When bPerm is TRUE, it changes the
ECO_NOHIDESEL option for this CRichEditCtrl object. For a brief description of this option, see SetOptions. You can use this function to set all the options for this
CRichEditCtrl object.