OnCtrlColor 不起作用?

发布于 2024-08-28 00:36:17 字数 441 浏览 14 评论 0原文

我使用以下重载方法在基于 Visual C++ MFC 对话框的应用程序中将列表框中的文本颜色更改为红色。当我在调试模式下构建程序时,它运行得很好。但是当我使用 RELEASE 模式时,文本颜色不会改变。这是为什么?我该如何克服这个问题?

谢谢!!

HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  if(nCtlColor == CTLCOLOR_LISTBOX)
  {
     if(bChangeTextColor)
     {
       pDC->SetTextColor(RGB(255, 0, 0));
       return m_hRedBrush;
     }
  } 
  return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

I used the following overloaded method to change the text color to red in a listbox, in a Visual C++ MFC dialog based application. When I build the program in DEBUG mode, it works perfectly. But when I use the RELEASE mode the text color doesn't change. Why is this and how can I overcome this problem??

Thanks!!

HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  if(nCtlColor == CTLCOLOR_LISTBOX)
  {
     if(bChangeTextColor)
     {
       pDC->SetTextColor(RGB(255, 0, 0));
       return m_hRedBrush;
     }
  } 
  return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜妞爱困 2024-09-04 00:36:17

您必须在 MESSAGE_MAP 中声明

,例如:

BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog)
    ON_WM_SYSCOMMAND()    
    ON_WM_PAINT()    
    ON_WM_QUERYDRAGICON()    
    //}}AFX_MSG_MAP

    ON_WM_CTLCOLOR()  // your handle    
END_MESSAGE_MAP()

You must declare in MESSAGE_MAP

ex:

BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog)
    ON_WM_SYSCOMMAND()    
    ON_WM_PAINT()    
    ON_WM_QUERYDRAGICON()    
    //}}AFX_MSG_MAP

    ON_WM_CTLCOLOR()  // your handle    
END_MESSAGE_MAP()
叹梦 2024-09-04 00:36:17

您可以尝试在自定义代码之前调用基本实现CDialog::OnCtlColor,如下所示:

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

    if(nCtlColor == CTLCOLOR_LISTBOX)
    {
        if(bChangeTextColor)
        {
            pDC->SetTextColor(RGB(255, 0, 0));
            hBrush=m_hRedBrush;
        }
    } 
    return hBrush;
}

CDialog::OnCtlColor 在内部执行一些由您的跳过的操作return 在你的函数中。这只是一个非常模糊的想法,但我一直以这种方式使用 OnCtlColor 并且从未遇到过问题。

Can you try to call the base implementation CDialog::OnCtlColor before your custom code, like this:

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

    if(nCtlColor == CTLCOLOR_LISTBOX)
    {
        if(bChangeTextColor)
        {
            pDC->SetTextColor(RGB(255, 0, 0));
            hBrush=m_hRedBrush;
        }
    } 
    return hBrush;
}

CDialog::OnCtlColor does some stuff internally which is skipped by your return inside of your function. It's only a very vague idea but I have always used OnCtlColor this way and never had a problem.

比忠 2024-09-04 00:36:17

我必须使用之前的两个答案才能使其发挥作用。

  1. 我在 MESSAGE_MAP 中声明了句柄

    BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog)
    
        ON_WM_CTLCOLOR() // 你的句柄
    
    END_MESSAGE_MAP()
    
  2. 我在自定义代码之前使用了CDialog::OnCtlColor

    HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
         HBRUSH hBrush=CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
         if(nCtlColor == CTLCOLOR_LISTBOX)
         {
              if(b更改文本颜色)
              {
                   pDC->SetTextColor(RGB(255, 0, 0));
                   hBrush = m_hRedBrush;
              }
        } 
        返回hBrush;
    }
    

I have to use both previous answers to make it work.

  1. I declared handle in MESSAGE_MAP

    BEGIN_MESSAGE_MAP(CTrainMFCDlg, CDialog)
    
        ON_WM_CTLCOLOR()  // your handle
    
    END_MESSAGE_MAP()
    
  2. I used CDialog::OnCtlColor before my custom code:

    HBRUSH MyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
         HBRUSH hBrush=CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
         if(nCtlColor == CTLCOLOR_LISTBOX)
         {
              if(bChangeTextColor)
              {
                   pDC->SetTextColor(RGB(255, 0, 0));
                   hBrush=m_hRedBrush;
              }
        } 
        return hBrush;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文