小技巧-给CEdit设置字体

关键词CEdit    字体    LOGFONT    CFont                                          

 

给CEDIT设置字体的代码:

在类的.h内

 CMyEdit * m_pEdit;
 CFont font;

在类的cpp文件中:

 LOGFONT lf;
  memset(&lf,0,sizeof(LOGFONT));
  lf.lfHeight =   height;                // request a 12-pixel-height font
  //lf.lfWidth = 0;
  lf.lfWeight = FW_BOLD;
  strcpy(lf.lfFaceName, "Arial");        // request a face name "Arial"
  VERIFY(font.CreateFontIndirect(&lf));  // create the font
  m_pEdit = new CMyEdit;
   m_pEdit->Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP ,CRect(1, 1, width+1, height+1), this, 100); 
  m_pEdit->SetFont(&font);

需要注意的是:

设置CEdit控件的字体需要使用 CEdit::SetFont() 函数,然而输入参数却不能是局部变量,需要在CEdit控件的整个生存期间都有效,因此需要把CFont变量设置为类内参数或全局,总之要在CEdit存在的时候CFont都存在

相关文章:

  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2021-05-22
  • 2022-12-23
  • 2021-12-13
  • 2021-11-20
  • 2021-09-27
猜你喜欢
  • 2022-12-23
  • 2021-12-28
  • 2021-09-11
  • 2021-12-02
  • 2022-01-01
  • 2022-02-05
相关资源
相似解决方案