VC中控件字体与文本颜色的设置

来源:互联网 发布:3d游戏编程入门 编辑:程序博客网 时间:2024/06/09 16:42
标签: VC中控件字体与文本颜色的设置 

设置字体
 函数原型:
  BOOL CreateFont( int nHeight, int nWidth, int nEscapement, int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline, BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision, BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily, LPCTSTR lpszFacename );

  参数说明:
  nHeight         :字体高度.>0:字体的高度值;=0:字体采用缺省直.<0:此值的绝对值为高度.
  nWidth          :字体宽度.
  nEscapement     :文本行的倾斜度.
  nOrientation    :字符基线的倾斜度.
  nWeight         :字体的粗细.如下:
    .FW_DONTCARE
    .FW_THIN
    .FW_EXTRALIGHT
     .....
  bItalic         :字体是否为斜体
  bUnderline      :字体是否带下划线
  cStrikeOut      :字体是否带删除线
  nCharSet        :字体的字符集
    .ANSI_CHARSET
    .DEFAULT_CHARSET
    .SYMBOL_CHARSET.....
  nOutPrecision   :字符的输出精度
  nClipPrecision  :字符裁剪的精度
  nQuality        :字符的输出质量
  nPitchAndFamily :字符间距和字体族(低位说明间距,高位说明字符族)
  lpszFacename    :字体名称
  [程序实现]
  假设你已有了名为My的对话框工程.并有一个ID=IDC_EDIT1的Edit控件.
  class CMyDlg : public CDialog
  { 
  public:
         CFont m_Font;
  ........
  };
  BOOL CTMyDlg::OnInitDialog()
  {
     CDialog::OnInitDialog();

     // TODO: Add extra initialization here
     //CFont m_Font;
     m_Font.CreateFont(-11,0,0,0,100,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"Arial");
    CEdit *m_Edit=(CEdit *)GetDlgItem(IDC_EDIT1);
     m_Edit->SetFont(&m_Font,FALSE);
     return TRUE;  // return TRUE  unless you set the focus to a control
  }
  小小说明:在OnInitDialog()中的//CFont m_Font;前的"//"号去掉,将类声明中的CFont m_Font;去掉会是什么结果?请自己试试.
改变Edit字体颜色! [所有相关帖子]
HBRUSH CButtonDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
   
    // TODO: Change any attributes of the DC here
    if(nCtlColor == CTLCOLOR_EDIT)
    {
    if(pWnd->GetDlgCtrlID()== IDC_EDIT1)
        {
            pDC->SetTextColor(RGB(255,255,0));
            pDC->SetBkColor(RGB(251, 247, 200));
            pDC->SetBkMode(TRANSPARENT);
            return (HBRUSH) m_brush.GetSafeHandle();
        }
    }

    // TODO: Return a different brush if the default is not desired
    return hbr;
}

----------------

CBrush   m_brushedit;  
  m_brushedit.CreateSolidBrush   (RGB   (   255,   255,   0   )   );  
   
  在Dlg::OnCtlColor函数中加入:  
  if(nCtlColor   ==   CTLCOLOR_EDIT){  
  pDC->SetTextColor(RGB(255,0,0));       //文字颜色  
  pDC->SetBkColor(RGB(255,255,200));   //文字背景颜色  
  return   (HBRUSH)m_brushedit.GetSafeHandle()   ;     //edit框的颜色  
  }  

 

原创粉丝点击