使用前的工作
添加RichEdit控件,并在程序起始的地方初始化 AfxInitRichEdit2(),如果没有该步骤,会闪一下退出。
BOOL CMOSHENApp::InitInstance()
{
AfxInitRichEdit2(); //RichEdit 2.0控件,必须在CWinApp函数初始化调用
return TRUE;
}
//对话框类头文件增加:
CRichEditCtrl m_richedit;
给颜色按钮添加下述代码
//.h
afx_msg void OnBnClickedBtncolor();
//RichEidt2.0 字体设置颜色
void CUserManual::OnBnClickedBtncolor()
{
// TODO: 在此添加控件通知处理程序代码
CHARFORMAT cf = { 0 };
cf.cbSize = sizeof(CHARFORMAT);
m_richedit.GetSelectionCharFormat(cf);
CColorDialog colorDialog(cf.crTextColor);
if (colorDialog.DoModal() == IDOK)
{
cf.crTextColor = colorDialog.GetColor();
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0;
m_richedit.SetSelectionCharFormat(cf);
}
}
给字体按钮增加代码
//.h
afx_msg void OnBnClickedBtnfont();
//.cpp
//RichEidt2.0 字体设置字体
void CUserManual::OnBnClickedBtnfont()
{
// TODO: 在此添加控件通知处理程序代码
CHARFORMAT cf = { 0 };
cf.cbSize = sizeof(CHARFORMAT);
m_richedit.GetSelectionCharFormat(cf);
CFontDialog fontDialog(cf);
if (fontDialog.DoModal() == IDOK)
{
fontDialog.GetCharFormat(cf);
m_richedit.SetSelectionCharFormat(cf);
}
}
上面的例子都是在后期进行更改,那么如何在我编码的时候直接在RichEdit控件中改变字体的格式呢?
下述几个例子可以作为参考:
Example:加粗
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.dwEffects = CFE_BOLD; //设置粗体,取消用cf.dwEffects&=~CFE_BOLD;
cf.dwMask = CFM_BOLD;
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:字体大小
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.yHeight = 300; //字体的大小(并非我们常见的字号概念)
cf.dwMask = CFM_SIZE;
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example: 设置斜体
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.dwEffects = CFE_ITALIC;//设置斜体,取消用cf.dwEffects&=~CFE_ITALIC;
cf.dwMask = CFM_ITALIC;
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:下划线
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.dwMask|=CFM_UNDERLINE;
cf.dwEffects|=CFE_UNDERLINE;//设置下划线,取消用cf.dwEffects&=~CFE_UNDERLINE;
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:设置颜色
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.dwMask|=CFM_COLOR;
cf.crTextColor = RGB(255,0,0);//设置颜色
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:设置字体
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);//RichEdit
CHARFORMAT cf; //定义字体格式结构体变量
cf.cbSize = sizeof(cf);
cf.dwMask|=CFM_FACE;
_tcscpy(cf.szFaceName ,_T("隶书"));//设置字体
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:设置行间距
BOOL CUserManual::OnInitDialog()
{
CRichEditCtrl* m_pRichEdit;
m_pRichEdit = (CRichEditCtrl*)GetDlgItem(IDC_RICHEDIT21);
PARAFORMAT2 pf;
memset(&pf, 0, sizeof(pf));
pf.cbSize = sizeof(PARAFORMAT2);
pf.dwMask = PFM_LINESPACING | PFM_SPACEAFTER;
pf.dyLineSpacing = 800;
pf.bLineSpacingRule = 1;
m_pRichEdit->SetParaFormat(pf); //控件变量
m_pRichEdit->SetDefaultCharFormat(cf); //设置控件默认的字体格式
return TRUE;
}
Example:增加按钮,选择某一段重新设置格式
long nStart = 0;
long nEnd = m_pRichEdit->GetTextLength();
m_pRichEdit->SetSel(nStart,nEnd);
m_pRichEdit->SetSelectionCharFormat(cf);
具体作用没有测试,应该是可以实现增加一个按钮,选择一段文字,点击按钮,然后该段文字编程该按钮预设定的格式。