关于CComboBox的字体颜色变化问题

xixuanfong 2007-07-12 03:19:10
我在dialog里添加了下面的消息映射,用来改变ComboBox的字体颜色,可是为什么不行呢。有这样的现象:
我新建了一个窗口工程,在窗口里拉进去一个ComboBox(ID:IDC_COMBO1)后,加入下面的代码,没有效果

可是我再拉进去一个ComboBox(ID:IDC_COMBO2)后,把下面代码里的IDC_COMBO1换成
IDC_COMBO2,这个时候两个控件的字都变成红色了,为什么阿,请教达人!!!

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

if(pWnd->GetDlgCtrlID() == IDC_COMBO1)
{
pDC->SetTextColor(RGB(255,0,0));
}

return hbr;
}
...全文
415 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyg_zy 2007-07-12
  • 打赏
  • 举报
回复
1:从CComboBox派生自己的ComboBox,并重写OnCtlColor
class CMyComboBox : public CComboBox
{
DECLARE_DYNAMIC(CMyComboBox)

public:
CMyComboBox();
virtual ~CMyComboBox();

protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};

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

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

2:把你的ComboBox声明为CMyComboBox的实例
#include "MyComboBox.h"
class CDlgTestDlg : public CDialog,public CTest
{
CMyComboBox m_Combox;
};

不要在对话框里处理,因为ComboBox是下拉的ListBox不是对话框子窗口,它是ComboBox的子窗口。


lyg_zy 2007-07-12
  • 打赏
  • 举报
回复
这是MSDN的解释:
OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create a CComboBox with an override of OnCtlColor that checks for CTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, the SetBkColor member function must be used to set the background color for the text.

xixuanfong 2007-07-12
  • 打赏
  • 举报
回复
在线等待。。。
xixuanfong 2007-07-12
  • 打赏
  • 举报
回复
补充一点啊,ComboBox的属性是可以编辑的情况下发生的这种情况,纳闷中。。。

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧