简体   繁体   English

在 MFC 中更改静态文本颜色

[英]Change Static Text Color in MFC

I am trying to change the color of static text (and also checkbox items) in a dialog window in my MFC application.我正在尝试在 MFC 应用程序的对话框窗口中更改静态文本(以及复选框项)的颜色。

Following this ( MFC - change text color of a cstatic text control ) and similar suggestions, I did the following on ON_WM_CTLCOLOR() message:在此( MFC - 更改 cstatic 文本控件的文本颜色)和类似建议之后,我对ON_WM_CTLCOLOR()消息执行了以下操作:

HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
      pDC->SetTextColor(RGB(255, 0, 0));
      return (HBRUSH)GetStockObject(NULL_BRUSH);
}

The problem is that this only affects edit text boxes and not static text or checkboxes.问题是这仅影响编辑文本框,而不影响静态文本或复选框。 those still have black texts.那些仍然有黑色文本。

I also tried to look for sth similar to winapi's WM_CTLCOLORSTATIC message as that worked well in win32 applications but did not find any equivalent in MFC.我还试图寻找类似于 winapi 的WM_CTLCOLORSTATIC消息的东西,因为它在 win32 应用程序中运行良好,但在 MFC 中没有找到任何等效项。 Any idea how to change the color of the static texts and checkbox text?知道如何更改静态文本和复选框文本的颜色吗?

This works for me:这对我有用:

Put this in the message map:把它放在消息映射中:

ON_WM_CTLCOLOR()

And implement something like this:并实现这样的事情:

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

  if (pWnd->GetDlgCtrlID() == IDC_SOMESTATIC)
  {
    // display the static control IDC_SOMESTATIC in red
    pDC->SetTextColor(RGB(255, 0, 0));
  }

  return hbr;
}

When you add a Static Text control from the Toolbox, it will get the ID IDC_STATIC.当您从工具箱添加静态文本控件时,它将获得 ID IDC_STATIC。 You need to rename this ID to something else, and then use OnCtlColor.您需要将此 ID 重命名为其他名称,然后使用 OnCtlColor。 Assuming you name it IDC_STATIC_1 , In OnCtlColor , use:假设您将其命名为IDC_STATIC_1 ,在OnCtlColor 中,使用:

case IDC_STATIC_1:
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(100,110,120);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM