从CButton派生一个选择颜色的颜色选择控件


#pragma once

// ColourPicker.h : header file

/
// CColourPicker window
void AFXAPI DDX_ColourPicker(CDataExchange *pDX, UINT nIDC, COLORREF& crColour);

class CColourPicker : public CButton
{
// Construction
public:
  CColourPicker();

public:
  COLORREF GetColour() { return( m_crColourBk ); };
  void SetColour(COLORREF color) { m_crColourBk = color; Invalidate(); }

// Attributes
private:
  COLORREF m_crColourBk, m_crColourText;

// Operations
public:

// Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CColourPicker)
public:
  virtual BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
  virtual void PreSubclassWindow(); 
  virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
  //}}AFX_VIRTUAL

// Implementation
public:
  virtual ~CColourPicker();

  // Generated message map functions
protected:
  //{{AFX_MSG(CColourPicker)
  afx_msg void OnClicked();
  //}}AFX_MSG

  DECLARE_MESSAGE_MAP()

};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.



// ColourPicker.cpp : implementation file
//

#include "stdafx.h"
#include "ColourPicker.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void AFXAPI DDX_ColourPicker(CDataExchange *pDX, UINT nIDC, COLORREF& crColour)
{
  HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  ASSERT (hWndCtrl != NULL);                
  
  CColourPicker* pColourPicker = (CColourPicker*) CWnd::FromHandle(hWndCtrl);
  if (pDX->m_bSaveAndValidate)
  {
    crColour = pColourPicker->GetColour();
  }
  else // initializing
  {
    pColourPicker->SetColour(crColour);
  }
}

/
// CColourPicker

CColourPicker::CColourPicker()
{
  m_crColourText = RGB(0,0,0);
  m_crColourBk = RGB(255,255,255);
}

CColourPicker::~CColourPicker()
{
}


BEGIN_MESSAGE_MAP(CColourPicker, CButton)
  //{{AFX_MSG_MAP(CColourPicker)
  ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// CColourPicker message handlers
BOOL CColourPicker::Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
  dwStyle |= BS_OWNERDRAW;
  return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
}

void CColourPicker::PreSubclassWindow()
{
  CButton::PreSubclassWindow();
  ModifyStyle(0, BS_OWNERDRAW, 0);
}

void CColourPicker::OnClicked() 
{
  //CColorDialog m_ColorDlg( GetColour() );
  CColorDialog m_ColorDlg( GetColour(), 0, this );
  if(m_ColorDlg.DoModal() == IDOK)
  {
    SetColour( m_ColorDlg.GetColor() );
  }
}

void CColourPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
  ASSERT(lpDrawItemStruct);
  
  CDC*    pDC     = CDC::FromHandle(lpDrawItemStruct->hDC);
  CRect   rect    = lpDrawItemStruct->rcItem;
  UINT    state   = lpDrawItemStruct->itemState;
  DWORD   dwStyle = GetStyle();
  CString m_strText;

  CSize MarginSize(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));
  // Get size of dropdown arrow
  INT nArrowWidth = max(::GetSystemMetrics(SM_CXHTHUMB), 5*MarginSize.cx);
  INT nArrowHeight = max(::GetSystemMetrics(SM_CYVTHUMB), 5*MarginSize.cy);
  CSize ArrowSize(max(nArrowWidth, nArrowHeight), max(nArrowWidth, nArrowHeight));
  CRect m_ArrowRect(rect.right - ArrowSize.cx - MarginSize.cx, 
                      rect.top + MarginSize.cy, rect.right - MarginSize.cx,
                      rect.bottom - MarginSize.cy);

  CSize Margins(::GetSystemMetrics(SM_CXEDGE), ::GetSystemMetrics(SM_CYEDGE));

  // Draw arrow
  //if (m_bActive) state |= ODS_SELECTED;
  pDC->DrawFrameControl(&m_ArrowRect, DFC_SCROLL, DFCS_SCROLLDOWN  | 
                        ((state & ODS_SELECTED) ? DFCS_PUSHED : 0) |
                        ((state & ODS_DISABLED) ? DFCS_INACTIVE : 0));

  pDC->DrawEdge(rect, EDGE_SUNKEN, BF_RECT);

  // Must reduce the size of the "client" area of the button due to edge thickness.
  rect.DeflateRect(Margins.cx, Margins.cy);

  // Fill remaining area with colour
  rect.right -= m_ArrowRect.Width();

  CBrush brush((state & ODS_DISABLED)? ::GetSysColor(COLOR_3DFACE) : m_crColourBk);
  CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
  pDC->SelectStockObject(NULL_PEN);
  pDC->Rectangle(rect);
  pDC->SelectObject(pOldBrush);

  // Draw the window text (if any)
  GetWindowText(m_strText);
  if (m_strText.GetLength())
  {
    pDC->SetBkMode(TRANSPARENT);
    if (state & ODS_DISABLED)
    {
      rect.OffsetRect(1,1);
      pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
      pDC->DrawText(m_strText, rect, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
      rect.OffsetRect(-1,-1);
      pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
      pDC->DrawText(m_strText, rect, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
    }
    else
    {
      pDC->SetTextColor(m_crColourText);
      pDC->DrawText(m_strText, rect, DT_CENTER|DT_SINGLELINE|DT_VCENTER);
    }
  }

  // Draw focus rect
  if (state & ODS_FOCUS) 
  {
    rect.DeflateRect(1,1);
    pDC->DrawFocusRect(rect);
  }  
}

//测试CDlg1000Dlg.h

  // Dialog Data
  //{{AFX_DATA(CDlg1000Dlg)
  CColourPicker	m_ColorPickerButton;
  //}}AFX_DATA
  COLORREF crColorPicker;

void CDlg1000Dlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CDlg1000Dlg)
  DDX_Control(pDX, IDC_BUTTON1, m_ColorPickerButton);
  DDX_ColourPicker(pDX, IDC_BUTTON1, crColorPicker);
  //}}AFX_DATA_MAP
}

运行效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值