韩国水光针面膜官网:【C++】如何改变控件的字体和字体颜色

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 11:49:27

如何改变控件的字体和字体颜色

问题详述

很多时候,对话框上各种子控件需要不同的的字体以达到某种效果,该如何实现呢?

专家解答

由于控件也是窗口,用户可以调用CWnd::SetFont指定新字体。该函数用了一个CFont指针,要保证在控件撤销之前不能撤销该字体对象。

下面介绍一下该函数:

CWnd::SetFont

This method sets the current font of the window to the specified font.

void SetFont(

CFont* pFont,

BOOL bRedraw = TRUE );

Parameters

pFont

Specifies the new font.

bRedraw

If TRUE, redraw the CWnd object.

Requirements

  Windows CE versions: 1.0 and later
  Header file: Declared in Afxwin.h
  Platform: H/PC Pro, Palm-size PC, Pocket PC

与此函数对应的还有CFont::GetFont

CWnd::GetFont

This method retrieves the current font for this window.

CFont* GetFont( ) 
const; 

Return Value

Returns a pointer to a CFont that contains the current font.

The pointer may be temporary and should not be stored for later use.

Requirements

  Windows CE versions: 1.0 and later
  Header file: Declared in Afxwin.h
  Platform: H/PC Pro, Palm-size PC, Pocket PC

 

本问题的对应工程为CtrlFont,相关代码如下:

//在对话框头文件中声明两个字体变量,分别对应两个按钮的字体

Protected:

     CFont m_fntOk;//ok按钮对应的字体

     CFont m_fntCancel;//cancel按钮对应的字体

//设置字体

BOOL CCtrlFontDlg::OnInitDialog()

{

CDialog::OnInitDialog();

//创建字体

m_fntOk.CreateFont(MulDiv(8,GetDC()->GetDeviceCaps(LOGPIXELSY),72),0,0,0,FW_NORMAL,0,0,0,ANSI_CHARSET,OUT_STROKE_PRECIS,CLIP_STROKE_PRECIS,DRAFT_QUALITY,VARIABLE_PITCH|FF_SWISS,_T(“Arial”));

//设置ok按钮的字体

CWnd *pWnd=GetDlgItem(IDOK);

pWnd-.SetFont(&m_fntOk);

//创建字体

m_fntCancel.CreateFont(12,10,10,10,FW_NORMAL,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|SWISS,””);

//设置cancel按钮的字体

pWnd=GetDlgItem(IDCANCEL);

pWnd->SetFont(&m_fntCancel);

 

//IDM_ABOUTBOX must be in the system command range.

ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);

ASSERT(IDM_ABOUTBOX<0xF000);

 

CMenu* pSysMenu=GetSystemMenu(FALSE);

If(pSysMenu!=NULL)

{

CString strAboutMenu;

strAboutMenu.LoadString(IDS_ABOUTBOX);

if(!strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_AEPARATOR);

pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

}

 

//Set the icon for this dialog. The framework does this automatically

//when the application’s main window is not a dialog

SetIcon(m_hIcon,TRUE);

SetIcon(m_hIcon,FALSE);

 

Return TRUE;

}

 

//设置字体颜色

在MFC类库中提供了CWnd::OnCtlColor函数在工作框架的窗口被重画时将调用该成员函数,因此可以重载WM_CTLCOLOR消息的响应函数,此函数的原型:

CWnd::OnCtlColor

This method is called by the framework when a child control is about to be drawn. Most controls send this message to their parent, usually a dialog box, to prepare the pDC for drawing the control using the correct colors.

afx_msg HBRUSH OnCtlColor(

CDC* pDC,

CWnd* pWnd,

UINT nCtlColor );

Parameters

pDC

Contains a pointer to the display context for the child window. May be temporary.

pWnd

Contains a pointer to the control asking for the color. May be temporary.

nCtlColor

Contains one of the following values, specifying the type of control: (指定控件的类型)

·   CTLCOLOR_BTN   Button control (按钮控件)

·   CTLCOLOR_DLG   Dialog box (对话框)

·   CTLCOLOR_EDIT   Edit control (编辑控件)

·   CTLCOLOR_LISTBOX   List-box control (列表控件)

·   CTLCOLOR_MSGBOX   Message box (消息框)

·   CTLCOLOR_SCROLLBAR   Scroll-bar control (滚动条控件)

·   CTLCOLOR_STATIC   Static control (静态控件)

Return Value

OnCtlColor must return a handle to the brush that is to be used for painting the control background.

Remarks

To change the text color, call the SetTextColor method with the desired red, green, and blue (RGB) values.

To change the background color of a single-line edit control, set the brush handle in both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and call the CDC::SetBkColor method in response to the CTLCOLOR_EDIT code.

OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create a CComboBox with an override of OnCtlColor that checks for CTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, the SetBkColor method must be used to set the background color for the text.

This method is called by the framework to allow your application to handle a Windows CE message. The parameters passed to your method reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this method, that implementation will use the parameters originally passed with the message and not the parameters you supply to the method.

Example

// This OnCtlColor handler will change the color of a static control

// with the ID of IDC_MYSTATIC. The code assumes that the CMyDialog

// class has an initialized and created CBrush member named m_brush.

// The control will be painted with red text and a background

// color of m_brush.

 

HBRUSH CZilchDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)

{

  // Call the base class implementation first! Otherwise, it may

  // undo what we are trying to accomplish here.

  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 

  // Are we painting the IDC_MYSTATIC control? We can use

  // CWnd::GetDlgCtrlID() to perform the most efficient test.

  if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)

  {

    // Set the text color to red.

    pDC->SetTextColor(RGB(255, 0, 0));

 

    // Set the background mode for text to transparent

    // so background will show thru.

    pDC->SetBkMode(TRANSPARENT);

 

    // Return handle to our CBrush object.

    hbr = m_brush;

  }

 

  return hbr;

}

 

 

假设你已有了名为My的对话框工程,你有了一个STATIC的控件,IDC_STATIC1

HBRUSH CMyDlg::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_STATIC)

       {

              pDC->SetTextColor(RGB(255,0,0));//字体颜色

              pDC->SetBkColor(RGB(0,0,255));//字体背景颜色

       }

       // TODO: Return a different brush if the default is not desired

       return hbr;

}

如果要指定某个特定控件可以这样写:

ID为IDC_STATIC1

if(pWnd->GetDlgCtlID()==IDC_STATIC1)

{

pDC->SetTextColor(RGB(255,0,0));//

pDC->SetBkMode(TRANSPARENT);//设置字体背景为透明

return (HBRUSH)::GetStockObject(BLACK_BRUSH);//设置背景色

}

Return hbr;