更改标签颜色 [英] Change color of label

查看:86
本文介绍了更改标签颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

在我的MFC应用程序中,我需要更改标签颜色.颜色将取决于要在标签中显示的文本.
例如:-对于警告,需要在标签上显示红色文本警告".
为了获得成功,需要显示绿色文本成功".

Hi.

In my MFC application, i need to change the label color. The color will depend on the text to be displayed in the label.
Eg:- for Warning, a red text "Warning" need to be shown as label.
For Success, a green text "Success" need to be shown.

Please anybody help me?

推荐答案

1)您需要为该标签指定与IDC_STATIC不同的ID.
在这里,我们在示例代码中使用了IDC_COLORED_LABEL.

2a)MFC项目

为对话框添加WM_CTLCOLOR处理程序,并按如下所示对其进行修改:

1) You need specify ID different than IDC_STATIC for the label in question.
Here we used IDC_COLORED_LABEL in the sample code.

2a) MFC project

Add WM_CTLCOLOR handler for the dialog and modify it as follows:

HBRUSH CDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd,
UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

// Add the following code
if (pWnd->GetDlgCtrlID() == IDC_COLORED_LABEL)
pDC->SetTextColor(RGB(128, 0, 0)); // dark red
// End

return hbr;
}



请注意,MFC使用OnCtlColor方法来处理所有类型的WM_CTLCOLORXXX
消息.

2b)Windows API

使用
结束WM_CTLCOLORSTATIC对话框消息处理程序的主体
返回(BOOL)GetSysColorBrush(COLOR_3DFACE);

返回有效的画笔柄(不是TRUE或FALSE)非常重要.



HTH



Note that MFC uses OnCtlColor method to handle all types of WM_CTLCOLORXXX
messages.

2b) Windows API

End the body of WM_CTLCOLORSTATIC dialog message handler with

return (BOOL) GetSysColorBrush(COLOR_3DFACE);

It is important that a valid brush handle, not TRUE or FALSE is returned.



HTH


每次需要更改标签文本时,都会调用OnCtlColor()吗?
在标签上添加文字时如何指定所需的颜色?

在我的代码中,
m_lStatusMessage.SetWindowText("Success");
m_lStatusMessage.SetFont(&m_Font); //我也在增加字体..这是可行的.
在这里,我将如何提供RGB信息?或者如何调用OnCtlColor()?我可以在上述两个步骤之后直接调用以更改标签颜色吗?
will the OnCtlColor() get called , each time i need to change the label text?
How will i specify the required colour when i add text to the label?

in my code,
m_lStatusMessage.SetWindowText("Success");
m_lStatusMessage.SetFont(&m_Font); // i am increasing the font too.. this is working.
Here, how will i give RGB information?Or how can i call OnCtlColor()? can i directly call after the above two steps for changing the label color?


每次控件重画时都会发送WM_CTLCOLOR消息,因此您可以添加COLORREF成员到您的对话框类,并以类似于以下代码段的方式修改您的代码:

在您的课程声明中:
The WM_CTLCOLOR message is sent each time a control should be repainted, so you can add a COLORREF member to your dialog class and modify your code in a way similar to the snippet below:

In your class declaration:
class CMyClass: public CDialog
{
   ...
protected:
   COLORREF m_rgbLabel;
   ...
};



在您的课程实施中:



In your class implementation:

HBRUSH CDialogDlg::OnCtlColor(CDC* pDC, CWnd* pWnd,UINT nCtlColor)
{
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

   // Add the following code
   if (pWnd->GetDlgCtrlID() == IDC_COLORED_LABEL)
      pDC->SetTextColor(m_rgbLabel);

   return hbr;
}

...

   // Change the label color before changing its text!
   m_rgbLabel = RGB(0x00F, 0xFF, 0x00); // Light green
   m_lStatusMessage.SetWindowText("Success");
   m_lStatusMessage.SetFont(&m_Font);
 
...


这篇关于更改标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆