设置不同的字体以编辑框 [英] Setting different font to edit boxes

查看:110
本文介绍了设置不同的字体以编辑框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有8个编辑框,我需要为每个框设置不同的字体.但是我的代码是将所有框设置为单个字体<下面的代码=">. { UpdateData(TRUE); CFontDialog dlg; //获取当前对话框的字体,仅用于填充LOGFONT LOGFONT lf; CFont * pFont = GetFont(); pFont-> GetLogFont(& lf); 如果(dlg.DoModal()== IDOK) { dlg.GetCurrentFont(& lf); m_font.DeleteObject(); m_font.CreateFontIndirect(& lf); GetDlgItem(IDC_EDIT1)-> SetFont(& m_font); GetDlgItem(IDC_EDIT2)-> SetFont(& m_font); GetDlgItem(IDC_EDIT3)-> SetFont(& m_font); GetDlgItem(IDC_EDIT4)-> SetFont(& m_font); GetDlgItem(IDC_EDIT5)-> SetFont(& m_font); GetDlgItem(IDC_EDIT6)-> SetFont(& m_font); GetDlgItem(IDC_EDIT7)-> SetFont(& m_font); GetDlgItem(IDC_EDIT8)-> SetFont(& m_font); } //TODO:在此处添加您的控件通知处理程序代码 }

解决方案

技术上可行:):

 {
  枚举 {ctlCount =  8 ; }

  静态 常量 UINT suiID [ctlCount] = {
    IDC_EDIT1,IDC_EDIT2,IDC_EDIT3,IDC_EDIT4,
    IDC_EDIT5,IDC_EDIT6,IDC_EDIT7,IDC_EDIT8
  };

  静态 CFont scFont [ctlCount];

   for ( int  i =  0 ; i < ctlCount; i ++){
    CFontDialg dlg;
    如果(IDOK == dlg.DoModal()){
      LOGFONT lf = { 0 };
      dlg.GetCurrentFont(& lf);
      scFont [i] .DeleteObject();
      scFont [i] .CreateFontIndirect(& lf);
      CWnd * pcEdit(GetDlgItem(suiID [i]));
      如果(pcEdit-> GetSafeHwnd()){
        pcEdit-> SetFont(& scFont [i]);
      }
    }
  }
} 


避免使用CTrail3Dlg :: OnEnChangeEdit1()
{
e =(CEdit *)GetDlgItem(IDC_EDIT1);
//TODO:在此处添加您的控件通知处理程序代码
} 


e是CEDIT类型的指针.

void CTrail3Dlg :: OnBnClickedButton1()
{
UpdateData(TRUE);
CFontDialog dlg;
//获取当前对话框的字体,仅用于填充LOGFONT
LOGFONT lf;
CFont * pFont = GetFont();
pFont-& gt; GetLogFont(& amp; lf);

如果(dlg.DoModal()== IDOK)
{
dlg.GetCurrentFont(& amp; lf);
m_font.DeleteObject();
m_font.CreateFontIndirect(& amp; lf);
e-& gt; SetFont(& amp; m_font);
}
}</pre>


In my project I''ve 8 edit boxes and i need to set each box with different font. But my code is setting all the boxes single font<below code="">.. So how can i modify it??

void CTrail3Dlg::OnBnClickedButton1()
{
	UpdateData(TRUE);
	CFontDialog dlg;
	// get font for current dialog, just to fill in LOGFONT
	LOGFONT lf;
	CFont *pFont = GetFont();
	pFont->GetLogFont(&lf);

	if (dlg.DoModal()== IDOK)
	{
		dlg.GetCurrentFont(&lf);
		m_font.DeleteObject();
		m_font.CreateFontIndirect(&lf);
		GetDlgItem(IDC_EDIT1)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT2)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT3)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT4)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT5)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT6)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT7)->SetFont(&m_font);
		GetDlgItem(IDC_EDIT8)->SetFont(&m_font);
	}
	// TODO: Add your control notification handler code here
}

解决方案

Technically possible :) :

{
  enum { ctlCount = 8; }

  static const UINT suiID[ctlCount] = {
    IDC_EDIT1, IDC_EDIT2, IDC_EDIT3, IDC_EDIT4,
    IDC_EDIT5, IDC_EDIT6, IDC_EDIT7, IDC_EDIT8
  };

  static CFont scFont[ctlCount];

  for (int i = 0; i < ctlCount; i++) {
    CFontDialg dlg;
    if (IDOK == dlg.DoModal()) {
      LOGFONT lf = {0};
      dlg.GetCurrentFont(&lf);
      scFont[i].DeleteObject();
      scFont[i].CreateFontIndirect(&lf);
      CWnd* pcEdit(GetDlgItem(suiID[i]));
      if (pcEdit->GetSafeHwnd()) {
        pcEdit->SetFont(&scFont[i]);
      }
    }
  }
}


void CTrail3Dlg::OnEnChangeEdit1()
{
	e=(CEdit*)GetDlgItem(IDC_EDIT1);
	// TODO:  Add your control notification handler code here
}


e is a pointer of type CEDIT.

void CTrail3Dlg::OnBnClickedButton1()
{
UpdateData(TRUE);
CFontDialog dlg;
// get font for current dialog, just to fill in LOGFONT
LOGFONT lf;
CFont *pFont = GetFont();
pFont-&amp;gt;GetLogFont(&amp;amp;lf);

if (dlg.DoModal()== IDOK)
{
dlg.GetCurrentFont(&amp;amp;lf);
m_font.DeleteObject();
m_font.CreateFontIndirect(&amp;amp;lf);
e-&amp;gt;SetFont(&amp;amp;m_font);
}
}</pre>


这篇关于设置不同的字体以编辑框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆