简体   繁体   English

CComboBox-如何使用下拉列表样式更改静态颜色

[英]CComboBox - How to change the color of the static with Drop List Style

I want to know if it's possible to change the color of the text (and of the small arrow) and the background of the static in a CCombobox with the Drop List style. 我想知道是否可以使用“ Drop List样式在CCombobox更改文本(和小箭头)的颜色和静态背景。

My class is derived from CComboBox and I've tried with the function CtlColor and OnCtlColor , but nothing seems to change the color of the ComboBox. 我的课是从CComboBox派生的,我已经尝试过使用CtlColorOnCtlColor函数,但是似乎没有什么可以改变ComboBox的颜色。

Here's a picture of the control with the Drop List style : 这是“删除列表”样式的控件的图片:

ComboBoxDropList

I would like the text and the arrow to change to RGB(0, 255, 255) and the background to RGB(255,255,0) . 我想文字和改变箭头RGB(0, 255, 255)和背景,以RGB(255,255,0)

Here's my function CtlColor() : 这是我的函数CtlColor()

HBRUSH CColoredComboBox::CtlColor(CDC *pDC, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_EDIT) 
    {
        pDC->SetBkColor(RGB(255,255,0));
        pDC->SetTextColor(RGB(0, 255, 255));
    }
    return m_brBkgnd;
}

It works for the style Dropdown, but not for the Drop List. 它适用于样式Dropdown,但不适用于Drop List。

Thank you. 谢谢。

Don't know arrow color can be changed or not but, color of combobox can be changed. 不知道是否可以更改箭头颜色,但是可以更改组合框的颜色。 With help of OnChildNotify() function, you can retrieve child HDC and then particular child HDC can be changed. 借助OnChildNotify()函数,您可以检索子HDC ,然后可以更改特定的子HDC

/////////////////////////////////////////////////////////////////////////////
// CMyComboBox message handlers

BOOL CMyComboBox::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult) 
{
    // TODO: Add your specialized code here and/or call the base class

    if(WM_CTLCOLOREDIT != message)
        return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);

    HDC hdcChild = (HDC)wParam;
    if(NULL != hdcChild)
    {
        SetBkMode(hdcChild, TRANSPARENT);
        SetTextColor(hdcChild, RGB(255, 255, 0));
        SetBkColor(hdcChild, RGB(255, 0, 0));
        *pLResult = (LRESULT)(m_Brush.GetSafeHandle());
    }

    return TRUE;
//  return CComboBox::OnChildNotify(message, wParam, lParam, pLResult);
}

Result: 结果:

结果

There are two ways - easy and hard. 有两种方法-简单和困难。 The hard way is to complete DrawItem with ownerdraw to handle all the cases. 困难的方法是用ownerdraw完成DrawItem来处理所有情况。 The easy way is to put two combos on top of each other in your dialog resource and hide the one you're not going to use. 一种简单的方法是在对话框资源中将两个组合放在另一个顶部,并隐藏不使用的组合。 Maybe this can give you some ideas. 也许可以给您一些想法。

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

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