改变编辑框字体和颜色

调用字体对话框,来改变编辑框的字体和颜色
1.添加一个编辑框 ID为 IDC_EDIT1
2.为编辑框添加变量 CEdit  m_edit
3.添加一个按钮触发该事件
  1. void CTeTDlg::OnButton1() //添加一个按钮  
  2.     {  
  3.         // TODO: Add your control notification handler code here  
  4.         //获得控件的当前字体  
  5.         LOGFONT lf;  
  6.         GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);//IDC_EDIT1为编辑框ID  
  7.         //使用按钮的当前字体初始化字体对话框  
  8.         CFontDialog dlgFontDlg(&lf);  
  9.         //显示字体选择对话框  
  10.   
  11.         if (dlgFontDlg.DoModal() == IDOK)  
  12.         {  
  13.             //如果用户在字体选择对话框中单击了“确定”按钮  
  14.             //则将按钮ID_BUTTON_DEMODE的标题文本字体设置为所选定的字体  
  15.             static CFont font;     
  16.              color = dlgFontDlg.GetColor();   //获得选择的颜色  color为COLORREF类型 在.h文件中声明  (设置颜色)  
  17.               
  18.              m_edit.SetFocus();               //m_edit 为编辑框的控件变量  
  19.   
  20.             dlgFontDlg.GetCurrentFont(&lf);   //获取当前的字体  
  21.             font.DeleteObject();              //删除字体  
  22.             font.CreateFontIndirect(&lf);      //新建  
  23.             GetDlgItem(IDC_EDIT1)->SetFont(&font);//设置为新的字体        
  24.         }         
  25.     }  
void CTeTDlg::OnButton1() //添加一个按钮
	{
		// TODO: Add your control notification handler code here
		//获得控件的当前字体
		LOGFONT lf;
		GetDlgItem(IDC_EDIT1)->GetFont()->GetLogFont(&lf);//IDC_EDIT1为编辑框ID
		//使用按钮的当前字体初始化字体对话框
		CFontDialog dlgFontDlg(&lf);
		//显示字体选择对话框

		if (dlgFontDlg.DoModal() == IDOK)
		{
			//如果用户在字体选择对话框中单击了“确定”按钮
			//则将按钮ID_BUTTON_DEMODE的标题文本字体设置为所选定的字体
			static CFont font;	 
			 color = dlgFontDlg.GetColor();   //获得选择的颜色  color为COLORREF类型 在.h文件中声明  (设置颜色)
			
             m_edit.SetFocus();               //m_edit 为编辑框的控件变量

			dlgFontDlg.GetCurrentFont(&lf);   //获取当前的字体
			font.DeleteObject();              //删除字体
			font.CreateFontIndirect(&lf);      //新建
			GetDlgItem(IDC_EDIT1)->SetFont(&font);//设置为新的字体		
		}		
	}
4.添加color消息
  1. HBRUSH CTeTDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  //添加color消息  
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.       
  5.     // TODO: Change any attributes of the DC here  
  6.       
  7.         if(nCtlColor == CTLCOLOR_EDIT)  
  8.         pDC->SetTextColor(color);              //初始化颜色  
  9.     // TODO: Return a different brush if the default is not desired  
  10.     return hbr;  
  11. }  

http://blog.csdn.net/cddchina/article/details/8149166

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值