本文共 4041 字,大约阅读时间需要 13 分钟。
我最近在写程序的时候,经常碰到需要选择颜色的控件,这种控件在许多地方是比较常用的,就象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 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::ccombocolorpicker() { 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 ccombocolorpicker::oncreate(lpcreatestruct lpcreatestruct) { if (ccombobox::oncreate(lpcreatestruct) == -1) return -1; initializedata(); return 0; } void ccombocolorpicker::presubclasswindow() { initializedata(); ccombobox::presubclasswindow(); } void ccombocolorpicker::initializedata() { int nitem; if (m_binitialized) 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 state 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]; } 以上只是一个很简单的类,功能还很不完善,由于本人的VC水平还是菜鸟等级的,没有能力把它给扩展了,我很希望有读者能把它给扩展了,到时候可别忘记给我发一个。谢谢!转载地址:http://coxqj.baihongyu.com/