字体、颜色对话框

字体对话框 CFontDialog

颜色对话框 CColorialog

富文本框 CRichEdit

一、建立名为FontAndColor的MFC工程,添加menu资源,并添加到对话框。修改id 分别为ID_FONT、ID_COLOR。主窗口添加rich edit 构建,并添加初始化

 

 

 

 二、为menu添加事件处理程序

void CFontAndColorDlg::OnFont()
{
	// TODO: 在此添加命令处理程序代码
	CHARFORMAT cf={0};
	cf.cbSize = sizeof(cf);
	//获取当前文本框文字格式
	m_editcontent.GetSelectionCharFormat(cf);
	CFontDialog dlg(cf);
	if(dlg.DoModal() == IDOK)
	{
		dlg.GetCharFormat(cf);
		m_editcontent.SetSelectionCharFormat(cf);
	}
}


void CFontAndColorDlg::OnColor()
{
	// TODO: 在此添加命令处理程序代码
	CHARFORMAT cf={0};
	cf.cbSize = sizeof(cf);
	//只修改颜色
	cf.dwMask = CFM_COLOR;

	m_editcontent.GetSelectionCharFormat(cf);

	CColorDialog dlg(cf.crTextColor);
	if(dlg.DoModal() == IDOK)
	{
		//获取选中的颜色
		cf.crTextColor = dlg.GetColor();
		cf.dwEffects = 0;
		//设置字体颜色
		m_editcontent.SetSelectionCharFormat(cf);
	}
}

 三、源码下载

链接:https://pan.baidu.com/s/1eldBgaftRy-qw-rUFLDy6g
提取码:06bh

 

posted on 2021-07-03 19:26  Malphite  阅读(132)  评论(0编辑  收藏  举报