65.9K
CodeProject 正在变化。 阅读更多。
Home

一个颜色选择按钮

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (18投票s)

2000年9月5日

viewsIcon

186087

downloadIcon

5210

对Chris Maunder的“Office 97风格颜色选择器”控件的一个简单修改

  • 下载演示项目 - 30 Kb
  • 下载源代码 - 14 Kb
  • Sample image

    这个控件是对Chris Maunder的“Office 97风格颜色选择器”控件的一个简单修改。您可以参考这篇文章了解更多细节。 新代码依赖于该文章中的CColourPopup类(尽管它包含在源代码和演示下载中),但对其代码进行了一处小修改,该修改断言父类是CColourPicker

    我希望捕捉到出现在某些Windows对话框中的颜色按钮的外观,包括显示属性|外观属性页。 代码非常简单明了,尽可能地从原始的CColourPicker中借用,并引入新的绘图代码以尽可能地匹配Windows颜色选择器。

    与原始控件一样,此按钮可以代替任何对话框或窗口按钮控件使用,并支持通过DDX_Control进行直接创建或子类化。 它还包含一个DDX_ColorButton例程,用于在DoDataExchange中设置和检索COLORREF值。

    CColorButton具有以下公共成员

    //***********************************************************************
    // Name:        CColorButton
    // Description:	Default constructor.
    // Parameters:	None.
    // Return:      None.	
    // Notes:       None.
    //***********************************************************************
    CColorButton(void);
    
    //***********************************************************************
    // Name:        CColorButton
    // Description:	Destructor.
    // Parameters:	None.
    // Return:      None.		
    // Notes:       None.
    //***********************************************************************
    virtual ~CColorButton(void);
    
    //***********************************************************************
    //**                        Property Accessors                         **
    //***********************************************************************	
    __declspec(property(get=GetColor,put=SetColor))	COLORREF Color;
    __declspec(property(get=GetDefaultColor,put=SetDefaultColor)) COLORREF DefaultColor;
    __declspec(property(get=GetTrackSelection,put=SetTrackSelection)) BOOL TrackSelection;
    __declspec(property(put=SetCustomText))	 LPCTSTR CustomText;
    __declspec(property(put=SetDefaultText)) LPCTSTR DefaultText;
    
    //***********************************************************************
    // Name:        GetColor
    // Description:	Returns the current color selected in the control.
    // Parameters:	void
    // Return:      COLORREF 
    // Notes:       None.
    //***********************************************************************
    COLORREF GetColor(void) const;
    
    //***********************************************************************
    // Name:        SetColor
    // Description:	Sets the current color selected in the control.
    // Parameters:	COLORREF Color
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetColor(COLORREF Color);
    
    
    //***********************************************************************
    // Name:        GetDefaultColor
    // Description:	Returns the color associated with the 'default' selection.
    // Parameters:	void
    // Return:      COLORREF 
    // Notes:       None.
    //***********************************************************************
    COLORREF GetDefaultColor(void) const;
    
    //***********************************************************************
    // Name:        SetDefaultColor
    // Description:	Sets the color associated with the 'default' selection.
    //              The default value is COLOR_APPWORKSPACE.
    // Parameters:	COLORREF Color
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetDefaultColor(COLORREF Color);
    
    //***********************************************************************
    // Name:        SetCustomText
    // Description:	Sets the text to display in the 'Custom' selection of the
    //              CColourPicker control, the default text is "More Colors...".
    // Parameters:  LPCTSTR tszText
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetCustomText(LPCTSTR tszText);
    
    //***********************************************************************
    // Name:        SetDefaultText
    // Description:	Sets the text to display in the 'Default' selection of the
    //              CColourPicker control, the default text is "Automatic". If
    //              this value is set to "", the 'Default' selection will not
    //              be shown.
    // Parameters:	LPCTSTR tszText
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetDefaultText(LPCTSTR tszText);
    
    //***********************************************************************
    // Name:        SetTrackSelection
    // Description:	Turns on/off the 'Track Selection' option of the control
    //              which shows the colors during the process of selection.
    // Parameters:	BOOL bTrack
    // Return:      None. 
    // Notes:       None.
    //***********************************************************************
    void SetTrackSelection(BOOL bTrack);
    
    //***********************************************************************
    // Name:		GetTrackSelection
    // Description:	Returns the state of the 'Track Selection' option.
    // Parameters:	void
    // Return:		BOOL 
    // Notes:		None.
    //***********************************************************************
    BOOL GetTrackSelection(void) const;
    

    下表列出了CColorButton类生成的消息。 它们与CColourPicker消息相同。

    Message 描述 lParam wParam
    CPN_SELCHANGE 颜色选择器选择更改Color控件 ID
    CPN_DROPDOWN 颜色选择器下拉Color控件 ID
    CPN_CLOSEUP 颜色选择器关闭Color控件 ID
    CPN_SELENDOK 颜色选择器结束(确定)Color控件 ID
    CPN_SELENDCANCEL 颜色选择器结束(取消)Color控件 ID

    为了处理CColorButton控件生成的消息,您需要在消息映射中手动添加ON_MESSAGE(CPN_XXX, MessageFn)处理程序。 处理程序函数将采用如下所示的标准Windows消息处理程序的形式。

    BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
    	//{{AFX_MSG_MAP(CMyDialog)
    	ON_MESSAGE(CPN_SELENDOK,     OnSelEndOK)
    	ON_MESSAGE(CPN_SELENDCANCEL, OnSelEndCancel)
    	ON_MESSAGE(CPN_SELCHANGE,    OnSelChange)
    	ON_MESSAGE(CPN_CLOSEUP,      OnCloseUp)
    	ON_MESSAGE(CPN_DROPDOWN,     OnDropDown)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    LONG CMyDialog::OnSelEndOK(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection ended OK\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnSelEndCancel(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection cancelled\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnSelChange(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Selection changed\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnCloseUp(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Colour picker close up\n");
        return TRUE;
    }
    
    LONG CMyDialog::OnDropDown(UINT /*lParam*/, LONG /*wParam*/)
    {
        TRACE0("Colour picker drop down\n");
        return TRUE;
    }
    

    致谢

    包含的源代码是对Chris Maunder已经很棒的代码的一个小改动,他得到了Alexander Bischofberger、Paul Wilkerson和Geir Arne Trillhus的帮助。 所有赞赏性的反馈都应该传递给他们,投诉给我。

    © . All rights reserved.