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

 我来答
聚发稿网
2015-08-16 · 知道合伙人生活技巧行家
聚发稿网
知道合伙人生活技巧行家
采纳数:5647 获赞数:29662
聚发稿网创始人,作家,笔名志耘誉松著《机械魔敌》《笑泪恋曲》

向TA提问 私信TA
展开全部
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?
伟丽皮骊茹
2020-03-11 · TA获得超过3666个赞
知道大有可为答主
回答量:3157
采纳率:32%
帮助的人:213万
展开全部
改变label控件的字体颜色可使用forecolor属性实现。
forecolor属性,forecolor-返回或设置在对象里显示图片和文本的前景颜色。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式