我想给CComboBox的编辑框上颜色。但我也想为CBS_DROPDOWN style和CBS_DROPDOWNLIST这样做。我覆盖
HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(CTLCOLOR_EDIT == nCtlColor)
{
pDC->SetTextColor(m_crAlertText);
pDC->SetBkColor(m_crAlertBkg);
hbr = m_hBrushAlert;
pDC->SetBkMode(TRANSPARENT);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}但如果CComboBox有CBS_DROPDOWNLIST风格的话,这是行不通的.为什么?
稍后编辑:
是的,我已经试过了
HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// if(CTLCOLOR_STATIC == nCtlColor)
if(CTLCOLOR_EDIT == nCtlColor || CTLCOLOR_STATIC == nCtlColor)
{
pDC->SetTextColor(m_crAlertText);
pDC->SetBkColor(m_crAlertBkg);
hbr = m_hBrushAlert;
pDC->SetBkMode(TRANSPARENT);
}
// TODO: Return a different brush if the default is not desired
return hbr;
}好像不管用..。我不明白为什么。
发布于 2014-11-24 10:37:56
引用MSDN
CBS_DROPDOWNLIST类似于CBS_DROPDOWN,只不过编辑控件被在列表框中显示当前选择的静态文本项替换。
因此,不能更改编辑控件的颜色的原因是在使用此样式时没有编辑控件。因此,条件if(CTLCOLOR_EDIT == nCtlColor)永远不会为真。您可能希望尝试测试CTLCOLOR_STATIC。
https://stackoverflow.com/questions/27100148
复制相似问题