Header file:
# If! Defined (afx_colorpickercb_h1_c74333b7_a13a_11d1_adb6_c04d0bc1_1_encoded _)
# Define afx_colorpickercb_h1_c74333b7_a13a_11d1_adb6_c04d0bc1_1_encoded _
# If _ msc_ver> = 1000
# Pragma once
# Endif // _ msc_ver >=1000
//
// Constants...
//
# Define ccb_max_colors 17 // colors in list
# Define ccb_max_color_name 17 // Max chars for color name-1
//
// Internal structure for color/name storage...
//
Struct scolorandname
{
/**/Scolorandname ()
{
Zeromemory (this, sizeof (scolorandname); // init Structure
};
/**/Scolorandname (colorref crcolor, pcstr cpcolor)
{
Zeromemory (this, sizeof (scolorandname); // init Structure
M_crcolor = crcolor; // set color RGB Value
Strncpy (m_ccolor, cpcolor, ccb_max_color_name); // set color name
};
Colorref m_crcolor; // actual color RGB Value
Char m_ccolor [ccb_max_color_name]; // actual name for color
};
Class ccolorpickercb: Public ccombobox
{
// Construction
Public:
Ccolorpickercb ();
// Attributes
PRIVATE:
Bool m_binitialized; // control initialized?
Cstring m_scolorname; // name of selected color
Static
Scolorandname ms_pcolors [ccb_max_colors]; // array of colors and names
PRIVATE:
Void initialize (void); // initialize control/colors
Public:
Colorref getselectedcolorvalue (void); // get selected color value
Cstring getselectedcolorname (void); // get selected color name
Void setselectedcolorvalue (colorref crclr); // set selected color value
Void setselectedcolorname (pcstr cpcolor); // set selected color name
Bool removecolor (pcstr cpcolor); // remove color from List
Bool removecolor (colorref crclr); // remove color from List
Int addcolor (pcstr cpnam, colorref crclr); // Insert a new color
// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (ccolorpickercb)
Protected:
Virtual void presubclasswindow ();
//} Afx_virtual
Virtual void drawitem (lpdrawitemstruct );
// Implementation
Public:
Virtual ~ Ccolorpickercb ();
// Generated message map Functions
Protected:
// {Afx_msg (ccolorpickercb)
Afx_msg int oncreate (maid );
//} Afx_msg
Declare_message_map ()
};
//////////////////////////////////////// /////////////////////////////////////
// {Afx_insert_location }}
// Microsoft developer studio will insert additional declarations immediately before the previous line.
# Endif //! Defined (afx_colorpickercb_h1_c74333b7_a13a_11d1_adb6_c04d0bc1_1_encoded _)
Source File
// Colorpickercb. cpp: implementation file
//
# Include "stdafx. H"
# Include "colorpickercb. H"
# Include "imageclass. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
//
// Load Standard colors...
// (If you change this, be sure to load ccb_max_colors colors ...)
//
Scolorandname ccolorpickercb: ms_pcolors [ccb_max_colors] =
{
Scolorandname (RGB (0x00, 0x00, 0x00), "black "),
Scolorandname (RGB (0x80, 0x00, 0x00), "maroon "),
Scolorandname (RGB (0x00, 0x80, 0x00), "green "),
Scolorandname (RGB (0x80, 0x80, 0x00), "Olive "),
Scolorandname (RGB (0x00, 0x00, 0x80), "Navy "),
Scolorandname (RGB (0x80, 0x00, 0x80), "Purple "),
Scolorandname (RGB (0x00, 0x80, 0x80), "teal "),
Scolorandname (RGB (0x80, 0x80, 0x80), "grey "),
Scolorandname (RGB (0xc0, 0xc0, 0xc0), "Silver "),
Scolorandname (RGB (0xff, 0x00, 0x00), "Red "),
Scolorandname (RGB (0x00, 0xff, 0x00), "Lime "),
Scolorandname (RGB (0xff, 0xff, 0x00), "yellow "),
Scolorandname (RGB (0x00, 0x00, 0xff), "blue "),
Scolorandname (RGB (0xff, 0x00, 0xff), "fushcia "),
Scolorandname (RGB (0x00, 0xff, 0xff), "Aqua "),
Scolorandname (RGB (0xff, 0xff, 0xff), "White "),
Scolorandname (RGB (192,192,192), "gray "),
};
Ccolorpickercb: ccolorpickercb ()
{
M_binitialized = false;
}
Ccolorpickercb ::~ Ccolorpickercb ()
{
}
Begin_message_map (ccolorpickercb, ccombobox)
// {Afx_msg_map (ccolorpickercb)
On_wm_create ()
//} Afx_msg_map
End_message_map ()
//////////////////////////////////////// /////////////////////////////////////
// Ccolorpickercb message handlers
Int ccolorpickercb: oncreate (lpcreatestruct pcstruct)
{
If (ccombobox: oncreate (pcstruct) =-1) // If create failed
Return (-1); // return failure
Initialize (); // initialize Contents
Setcursel (0); // select first item by default
Return (0); // done!
}
Void ccolorpickercb: presubclasswindow ()
{
Initialize (); // initialize Contents
Ccombobox: presubclasswindow (); // subclass Control
Setcursel (0); // select first item by default
Return; // done!
}
Void ccolorpickercb: Initialize (void)
{
Int iaddeditem =-1;
If (m_binitialized) // if already initialized
Return; // return
For (INT icolor = 0; icolor <ccb_max_colors; icolor ++) // For all colors
{
Iaddeditem = addstring (ms_pcolors [
Icolor]. m_ccolor); // set color name/Text
If (iaddeditem = cb_errspace) // if not added
Break; // stop
Else // If added successfully
Setitemdata (iaddeditem, ms_pcolors [
Icolor]. m_crcolor); // set color RGB Value
}
M_binitialized = true; // set initialized flag
}
Void ccolorpickercb: drawitem (lpdrawitemstruct pdistruct)
{
Static cstring scolor; // No need to reallocate each time
CDC dccontext;
Crect ritemrect (pdistruct-> rcitem );
Crect rblockrect (ritemrect );
Crect rtextrect (rblockrect );
Cbrush brframebrush;
Int ifourthwidth = 0;
Int iItem = pdistruct-> Itemid;
Int iaction = pdistruct-> itemaction;
Int istate = pdistruct-> itemstate;
Colorref crcolor = 0;
Colorref crnormal = getsyscolor (color_window );
Colorref crselected = getsyscolor (color_highlight );
Colorref crtext = getsyscolor (color_windowtext );
If (! Dccontext. Attach (pdistruct-> HDC) // attach CDC object
Return; // stop if attach failed
Ifourthwidth = (rblockrect. Width ()/4); // get 1/4 of item area
Brframebrush. createstockobject (black_brush); // create black brush
If (istate & ods_selected) // If selected
{// Set selected attributes
Dccontext. settextcolor (
(0x00ffffff &~ (Crtext); // set inverted text color (with mask)
Dccontext. setbkcolor (crselected); // set BG to highlight color
Dccontext. fillsolidrect (& rblockrect, crselected); // erase item
}
Else // if not selected
{// Set standard attributes
Dccontext. settextcolor (crtext); // set text color
Dccontext. setbkcolor (crnormal); // set BG color
Dccontext. fillsolidrect (& rblockrect, crnormal); // erase item
}
If (istate & ods_focus) // If item has the focus
Dccontext. drawfocusrect (& ritemrect); // draw focus rect
//
// Calculate text area
//
Rtextrect. Left + = (ifourthwidth + 2); // set start of Text
Rtextrect. Top + = 2; // offset a bit
//
// Calculate Color Block Area
//
Rblockrect. deflaterect (csize (2, 2); // reduce Color Block Size
Rblockrect. Right = ifourthwidth; // Set width of Color Block
//
// Draw color text and block
//
If (Iitem! =-1) // if not an empty item
{
Getlbtext (Iitem, scolor); // get color text
If (istate & ods_disabled) // if disabled
{
Crcolor = getsyscolor (color_inactivecaptiontext );
Dccontext. settextcolor (crcolor); // set text color
}
Else // If normal
Crcolor = getitemdata (Iitem); // get color value
Dccontext. setbkmode (transparent); // transparent background
Dccontext. textout (rtextrect. Left, rtextrect. Top,
Scolor); // draw color name
Dccontext. fillsolidrect (& rblockrect, crcolor); // draw color
Dccontext. framerect (& rblockrect, & brframebrush); // draw frame
}
Dccontext. Detach (); // detach DC from object
}
Colorref ccolorpickercb: getselectedcolorvalue (void)
{
Int iselecteditem = getcursel (); // get selected item
If (iselecteditem = cb_err) // if nothing selected
Return (RGB (0, 0, 0); // return black
Return (getitemdata (iselecteditem); // return selected color
}
Cstring ccolorpickercb: getselectedcolorname (void)
{
Int iselecteditem = getcursel (); // get selected item
If (iselecteditem = cb_err) // if nothing selected
Return (m_scolorname = afxemptystring); // return nothing (not "black! ")
Getlbtext (iselecteditem, m_scolorname); // store name of color
Return (m_scolorname); // return selected color name
}
Void ccolorpickercb: setselectedcolorvalue (colorref crclr)
{
Int iItems = getcount ();
For (INT iItem = 0; iItem <iItems; iItem ++)
{
If (crclr = getitemdata (Iitem) // If match found
Setcursel (Iitem); // select it
}
Return; // done!
}
Void ccolorpickercb: setselectedcolorname (pcstr cpcolor)
{
Int iItems = getcount ();
Cstring scolorname;
For (INT iItem = 0; iItem <iItems; iItem ++)
{
Getlbtext (Iitem, scolorname); // get color name
If (! Scolorname. comparenocase (cpcolor) // If match found
Setcursel (Iitem); // select it
}
Return; // done!
}
Bool ccolorpickercb: removecolor (pcstr cpcolor)
{
Int iItems = getcount ();
Cstring scolor;
Bool bremoved = false;
For (INT iItem = 0; iItem <iItems; iItem ++)
{
Getlbtext (Iitem, scolor); // get color name
If (! Scolor. comparenocase (cpcolor) // If match found
{
Deletestring (Iitem); // remove it
Bremoved = true; // set flag
Break; // stop checking
}
}
Return (bremoved); // done!
}
Bool ccolorpickercb: removecolor (colorref crclr)
{
Int iItems = getcount ();
Bool bremoved = false;
For (INT iItem = 0; iItem <iItems; iItem ++)
{
If (crclr = getitemdata (Iitem) // if desired color found
{
Deletestring (Iitem); // remove it
Bremoved = true; // set flag
Break; // stop checking
}
}
Return (bremoved); // done!
}
Int ccolorpickercb: addcolor (pcstr cpname, colorref crcolor)
{
Int iItem =-1;
IItem = insertstring (-1, cpname); // insert string
If (Iitem! = Lb_err) // If insert good
Setitemdata (Iitem, crcolor); // set color value
Return (Iitem); // done! Return location
}
Subsequent processing:
When added to the dialog box, you should also change it.
Ccolorpickercb display style:
In the propoties dialog box, select styles, select drop list on the left, set ownerdraw to fixed, and select has strings.
This code is complete.