在一个ComboBox中选择颜色也将填充到其他ComboBox中.我该如何解决? [英] Choosing Color in One ComboBox is also filled up to other ComboBoxes. How can I solve it?

查看:101
本文介绍了在一个ComboBox中选择颜色也将填充到其他ComboBox中.我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用MFC,我在CDialog上创建了3个组合框.一个是颜色组合框,另外两个是数字组合框.创建颜色组合框时,我在CDialog类上添加了ON_WM_CTLCOLOR() 并在其中编写了颜色更改功能.现在,彩色comboxbox运行良好.但是,从颜色组合框中选择颜色时,其他组合框的颜色也会更改.我不想更改其他组合框的颜色.

假设颜色组合框控件为IDC_COLOR_COMBO.我将ON_WM_CTLCOLOR() 签入为:

By using MFC, I create 3 combo boxes on CDialog. One is color combobox and the other two are just number comboboxes. While creating color combobox, I added ON_WM_CTLCOLOR() on CDialog Class and wrote color changed function there. Now, color comboxbox is working well. But when choosing color from color from color combobox, other comboboxes colors are also changed. I don''t want to change the color of other comboboxes.

Suppose, color combobox control is IDC_COLOR_COMBO. And I check in ON_WM_CTLCOLOR() as:

if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX && pWnd->GetDlgCtrlID() == IDC_COLOR_COMBO) { //change color; }



即使我如上所述编写,它仍然会更改其他组合框的颜色.我不知道该在哪里以及如何控制它.
您能为我的问题提供解决方案吗?
在高级中表示感谢.



Even though, I write as above, it still changes the color of other combo boxes. I don''t know where and how can I control it.
Could you please provide me solution for my problem?
Thanks in advanced.

推荐答案

如果此"if"语句来自您的真实代码,那么您的麻烦就在那里.
让我们首先使用PRE标记并查看您的"if"语句:
If this "if" statement is from your real code, then your troubles are there.
Lets first use PRE tags and have a look at your "if" statement:
if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX && pWnd->GetDlgCtrlID() == IDC_COLOR_COMBO) 
{ 
    //change color; 
}



现在,检查"if"语句中的条件块,并记住 AND运算符(&&)将在 OR运算符(||)之前进行求值,因为它具有更高的优先级 [ OR运算符(||)进行求值):



Now examine the condition block in your "if" statement and keep in mind that the AND operator (&&) will be evaluated before the OR operator (||), because of it''s higher priority[^].

I''m pretty sure that the correct way of writing this "if" statement is this (with additional brackets to guarantee that the OR operator (||) will be evaluated first):

if ((nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX) && pWnd->GetDlgCtrlID() == IDC_COLOR_COMBO) 
{ 
   //change color; 
}



还可以考虑从CComboBox派生一个类,并使用WM_CTLCOLOR的反射,以使自己拥有一个彩色的.
您甚至可以继承组合框的列表控件和编辑控件部分.在
此处 [ ^ ].

我希望这有帮助. :)


[更新]

似乎WM_CTLCOLOR与combobox有一个有趣的行为.请查看 [



Also consider deriving a class from CComboBox and use the reflection of WM_CTLCOLOR in order to have your own colored one.
You can subclass even the list control and the edit control parts of the combobox. Check it out here[^].

I hope this helps. :)


[Update]

It seems that there is an interesting behavior of WM_CTLCOLOR with combobox. Have e look at this[^] discussion.

IMHO the best option is deriving your own class from CComboBox. It could be really very simple.

Have a look at this demo class that I''ve created as an example for you:

- Relevant part from ColorCombo.h:

class CColorCombo : public CComboBox
{
	DECLARE_DYNAMIC(CColorCombo)
public:
	CColorCombo();
	virtual ~CColorCombo();

protected:
	DECLARE_MESSAGE_MAP()

public:
        // We will trap WM_CTLCOLOR directly...
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};



- ColorCombo.cpp 中的相关部分:



- Relevant part from the ColorCombo.cpp:

IMPLEMENT_DYNAMIC(CColorCombo, CComboBox)

CColorCombo::CColorCombo()
{
}

CColorCombo::~CColorCombo()
{
}

BEGIN_MESSAGE_MAP(CColorCombo, CComboBox)
	ON_WM_CTLCOLOR()  // WM_CTLCOLOR message map entry
END_MESSAGE_MAP()

// And here is an example code for OnCtlColor
HBRUSH CColorCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);

	// Just a demo...
	if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX) 
	{
                // For example we can set text and etc.
		pDC->SetTextColor(RGB(255, 0, 0));
		pDC->SetBkMode(TRANSPARENT);

                // If you need to color the background you can return a brush 
                // different from ''hbr''. Because this is just a demonstration 
                // I''ll create the brush here but keep in mind that creating brush 
                // once and keeping it for example as a class member is better approach.
		return (HBRUSH) ::CreateSolidBrush(RGB(255, 255, 0));
	}

	return hbr;
}



现在,在对话框类中,您可以为组合框添加成员,包括 ColorCombo.h 并将添加的成员类型从CComboBox更改为CColorCombo-就是这样! :)

另一种方法是将组合框分类.请参见
此处 [



Now in your dialog class you can add member for your combo box, include ColorCombo.h and change the added member type from CComboBox to CColorCombo - that''s it! :)

An alternative approach is subclassing the combo box. See here[^] for details, but I still recommend deriving your own class from CComboBox as I''ve already demonstrated this method. IMHO this is the clearest approach. :)

[/Update]


如果我正确地理解了您的问题,简单的答案是您不能,因为您更改了应用程序一般主题的颜色.这就像在计算机上更改显示设置时一样,它们将应用于所有窗口.
if i understood your question correctly the simple answer is that you cannot since your changing the color of the general theme of your application. This is like when you change the display settings on your computer they will apply to all the windows.


这篇关于在一个ComboBox中选择颜色也将填充到其他ComboBox中.我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆