【问题标题】:ComboBox - OnCtlColor : How to color the whole list rectComboBox - OnCtlColor:如何为整个列表着色
【发布时间】:2019-07-10 21:38:22
【问题描述】:

我正在尝试为派生的 ComboBox 类列表的rect 着色。在OnCtlColor 中,我正在用FillSolidRect 绘制一个矩形,但是当我将鼠标放在列表上时,它会丢失所有文本(只有选定的文本会保留)。这是我的代码:

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

    if (nCtlColor == CTLCOLOR_LISTBOX || nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_MSGBOX)
    {
        CRect rect;
        pDC->GetClipBox(&rect);
        // fill the rectangular area with the color
        pDC->FillSolidRect(&rect, RGB(255, 0, 255));
        hbr = m_brBkgnd;
        pDC->SetTextColor(RGB(0, 255, 0));
        pDC->SetBkColor(RGB(255, 0, 255));
    }

    return hbr;
}

我想我需要回DrawText的文字,有人知道怎么做吗?

我看到有些人在继承 CEditCListCtrl,我应该试试吗?

谢谢:)

【问题讨论】:

  • OnCtlColor() 不适用于绘图。您只能修改设备上下文状态,例如。 G。 CDC::SetBkColor, CDC::SetTextColor 并返回一个画笔。

标签: c++ mfc


【解决方案1】:

我在 SetBkColor 之后的 OnCtlColor 函数中添加了这个

        int numItems = GetCount();
        char text[1024];
        int height = GetItemHeight(0);

        for (int i = 0; i < numItems; i++) 
        {
            GetLBText(i, text);
            CRect pos(rect.left + 2, rect.top + (height * i), rect.right, rect.bottom);
            DrawText(pDC->GetSafeHdc(), text, -1, &pos, DT_SINGLELINE);
        }

这就是我解决问题的方法! :)

【讨论】:

  • new CRect 在这里毫无意义,您正在泄漏内存。而且我不清楚您发布的代码 sn-p 应该在哪里。
  • 这个答案没有意义,即使在编辑之后也是如此。如何获得rect?如何找到突出显示的项目?
  • @BarmakShemirani rect 由您的 pDC CRect rect; pDC-&gt;GetClipBox(&amp;rect); 获得,对于所选项目,您可以检查:stackoverflow.com/questions/32867099/…
  • GetClipBox 返回错误的矩形。看起来您正在尝试重新绘制组合框的列表框部分以响应 WM_CTLCOLOR_REFLECT 消息。我不明白这是怎么回事。
猜你喜欢
  • 2021-07-16
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多