如何改变控件的字体和字体颜色
2个回答
2015-08-16 · 知道合伙人生活技巧行家
关注
展开全部
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_ABOUTBOXAppendMenu(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 dialogSetIcon(m_hIcon,TRUE);SetIcon(m_hIcon,FALSE); Return TRUE;} //设置字体颜色在MFC类库中提供了CWnd::OnCtlColor函数在工作框架的窗口被重画时将调用该成员函数,因此可以重载WM_CTLCOLOR消息的响应函数,此函数的原型:CWnd::OnCtlColorThis 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);ParameterspDCContains a pointer to the display context for the child window. May be temporary. pWndContains a pointer to the control asking for the color. May be temporary. nCtlColorContains 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 ValueOnCtlColor must return a handle to the brush that is to be used for painting the control background.RemarksTo 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?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询