I have recently written the program, I often encounter a control that needs to choose color. This control is more common in many places, just like the color of the color you can say in the QQ chat room, this is actually Add one or several combo boxes on a basic dialog, then you can select the color you want through the combo box.
The following is the program code, you need to do very simple, you just add the following to your project, then you have to create a member variable to the combo box through the VC class wizard, so that you will do it. If you want to have more color selection, you can modify the .cpp file, you can line it with color definitions in the .cpp file, or you have a better programming level, you can extend it into a very practical the type. I didn't have all the code all, I would like to read more than interested, it is best to look at all the code, which will be more beneficial to you. The following is all code:
//head File
#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 overce
#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 Immediate Line.
#ENDIF /! Defined (AFX_COMBOCOLORPICKER_H__B2348841_5541_11D1_8756_00A0C9181E86__included_)
/ / The following is the CPP file
// ComboColorPicker.cpp: importation 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 Yellow
RGB (0, 0, 128), // Dark Blue
RGB (128, 0, 128), // DARK MAGENTA
RGB (0, 128, 128), // Dark Cyan
RGB (192, 192, 192), // light Grey
RGB (128, 128, 128), // Dark Grey
RGB (255, 0, 0), // RED
RGB (0, 255, 0), // Green
RGB (255, 255, 0), // Yellow
RGB (0, 0, 255), // blue
RGB (255, 0, 255), // magenta
RGB (0, 255, 255), // cyan
}
CCOMBOCOLORPICKER :: CCOCOLORPICKER ()
{
m_binitialized = false;
}
CCOMBOCOLORPICKER :: ~ ccomboColorPicker ()
{
}
Begin_MESSAGE_MAP (CCOMBOCOLORPICKER, CCOMBOBOX)
// {{AFX_MSG_MAP (CCOMBOCOLORPICKER)
ON_WM_CREATE ()
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
/
// ccombocolorpicker message handlers
INT CCOCOLORPICKER :: OnCreate (lpcreatestruct lpcreatestruct)
{
IF (CCOMBOBOX :: OnCreate (lpcreateStruct) == -1)
Return -1;
Initializedata ();
Return 0;
}
Void CCOCOLORPICKER :: PRESUBCLASSWINDOW ()
{
Initializedata ();
CCOMBOBOX :: PRESUBCLASSWINDOW ();
}
Void CCOCOLORPICKER :: Initializedata ()
{
Int Nitem;
IF (m_binitialize)
Return;
For (int ncolor = 0; ncolor hdc))
Return;
ColorRef rgbtextcolor = dc.getTextColor ();
ColorRef rgbbkcolor = dc.getbkcolor (); if (lpdrawItemstruct-> itemaction & oda_focus)
{
Dc.drawfocusRect (& lpdrawItemstruct-> rcitem);
}
Else IF (LPDrawItemstruct-> Itemaction & ODA_DRAWENTIRE)
{
IF (LPDrawItemstruct-> itemState & ODS_FOCUS)
Dc.drawfocusRect (& lpdrawItemstruct-> rcitem);
Else
Dc.extTextOut (0, 0, ETO_OPAQUE, & LPDRAWITEMSTRUCT-> RCITEM, _T (""), 0, null;
}
IF (0 <= (int) LPDRAWITEMSTRUCT-> ITEMID) // any item selected?
{
:: Inflaterect (& lpdrawItemstruct-> rcitem, -2, -2);
Dc.FillSolidRect (& lpdrawItemstruct-> rcitem, (colorref) lpdrawitemstruct-> itemdata);
Dc.framerect (& lpdrawItemstruct-> rcitem, & brushblack);
}
// restore the dc stat
Dc.SetTextColor (RGBTextColor);
Dc.SetBkcolor (RGBBKCOLOR);
Dc.detach ();
}
ColorRef CCOMBOCOLORPICKER :: getSelectedColor ()
{
INT NITEM = GetCursel ();
IF (CB_ERR == Nitem)
Return RGB (0, 0, 0); // Default to Black if Nothing IS SELECTED
Return M_RGStandardColors [NITEM];
}
The above is just a very simple class, the function is still very imperfect, because my VC level is still a rookie level, there is no ability to expand it, I really hope that the reader can expand it, but if you don't forget it. I sent one. Thank you!