作者:賈旭濱(譯)
//原著:Baldvin Hansson //譯者:重慶大學光電工程學院 賈旭濱(daixi1703163@163.net) //歡迎大家批評指教,謝謝!
我最近在寫程序的時候,經常碰到需要選擇顏色的控件,這種控件在許多地方是比較常用的,就象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水平還是菜鳥等級的,沒有能力把它給擴展了,我很希望有讀者能把它給擴展了,到時候可別忘記給我發一個。謝謝!
譯于2001年9月9號。
|