我最近在写程序的时候,经常碰到需要选择颜色的控件,这种控件在许多地方是比较常用的,就象qq聊天室中可以选择自己所说的话的颜色的那种控件,这其实就是在一个基本对话框上添加一个或者几个组合框,然后可以通过组合框选择你想要的颜色而已。
下面就是程序代码,你需要做的很简单,你只要把下面的2个文件添加到你工程中,然后你还要通过vc的类向导创建一个该类型的成员变量给组合框,这样就行了。如果你想有更多的颜色选择的话,你可以通过修改.cpp文件中的有关与颜色定义的数组就行了,或者说你有更好的编程水平的话,你完全可以把它扩展成一个很实用的类。我没有把代码全部都看过,我想读者如果有兴趣的话,最好还是把代码全部看一遍,这样会对你更有好处的。以下是全部代码:
//头文件
#if !defined(afx_combocolorpicker_h__b2348841_5541_11d1_8756_00a0c9181e86__included_)
#define afx_combocolorpicker_h__b2348841_5541_11d1_8756_00a0c9181e86__included_
#if _msc_ver >= 1000
#pragma once
#endif // _msc_ver >= 1000
// combocolorpicker.h : header file
// ? 1997 baldvin hansson
/////////////////////////////////////////////////////////////////////////////
// ccombocolorpicker window
class ccombocolorpicker : public ccombobox
{
// construction
public:
ccombocolorpicker();
// attributes
private:
bool m_binitialized;
static colorref m_rgstandardcolors[];
public:
// operations
private:
void initializedata();
public:
colorref getselectedcolor();
// overrides
// classwizard generated virtual function overrides
//{{afx_virtual(ccombocolorpicker)
protected:
virtual void presubclasswindow();
//}}afx_virtual
virtual void drawitem(lpdrawitemstruct lpdrawitemstruct);
// implementation
public:
virtual ~ccombocolorpicker();
// generated message map functions
protected:
//{{afx_msg(ccombocolorpicker)
afx_msg int oncreate(lpcreatestruct lpcreatestruct);
//}}afx_msg
declare_message_map()
};
/////////////////////////////////////////////////////////////////////////////
//{{afx_insert_location}}
// microsoft developer studio will insert additional declarations immediately before the previous line.
#endif // !defined(afx_combocolorpicker_h__b2348841_5541_11d1_8756_00a0c9181e86__included_)
//以下是对于的cpp文件
// combocolorpicker.cpp : implementation file
// ? 1997 baldvin hansson
#include "stdafx.h"
#include "combocolorpicker.h"
#ifdef _debug
#define new debug_new
#undef this_file
static char this_file[] = __file__;
#endif
/////////////////////////////////////////////////////////////////////////////
// ccombocolorpicker
colorref ccombocolorpicker::m_rgstandardcolors[] = {
rgb(0, 0, 0), // black
rgb(255, 255, 255), // white
rgb(128, 0, 0), // dark red
rgb(0, 128, 0), // dark green
rgb(128, 128, 0), // dark yellow
rgb(0, 0, 128), // dark blue
rgb(128, 0, 128), // dark magenta
rgb(0, 128, 128), // dark cyan