MFC CRichEdit改变指定区域的背景色和字体 .

注: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 theECO_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 thisCRichEditCtrl object.

 

 

你可能感兴趣的:(MFC CRichEdit改变指定区域的背景色和字体 .)