如图所示
如果想做成工具箱式,直接生成一个DialogBar,将此调色板镶入即可
CColorPicker m_picker;
m_picker.Create("kdfj","dfdf",WS_VISIBLE,CRect(0,0,400,50),this,1023,NULL);
CColorPicker类
#if !defined(AFX_COLORPICKER_H__30EE9CBC_5C0B_4B5E_88E7_95C6D42E3923__INCLUDED_)
#define AFX_COLORPICKER_H__30EE9CBC_5C0B_4B5E_88E7_95C6D42E3923__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ColorPicker.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CColorPicker window
class CColorPicker : public CWnd
{
// Construction
public:
CColorPicker();
UINT m_nStyle;
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CColorPicker)
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
//}}AFX_VIRTUAL
// Implementation
public:
COLORREF GetColor();
virtual ~CColorPicker();
// Generated message map functions
protected:
//{{AFX_MSG(CColorPicker)
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
COLORREF color[25];
COLORREF m_color;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_COLORPICKER_H__30EE9CBC_5C0B_4B5E_88E7_95C6D42E3923__INCLUDED_)
// ColorPicker.cpp : implementation file
//
#include "stdafx.h"
#include "ColorPicker.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorPicker
CColorPicker::CColorPicker()
{m_color=RGB(0,0,0);
color[0]=RGB(0,0,0);
color[1]=RGB(128,0,0);
color[2]=RGB(0,128,0);
color[3]=RGB(128,128,0);
color[4]=RGB(0,0,128);
color[5]=RGB(128,0,128);
color[6]=RGB(0,128,128);
color[7]=RGB(192,192,192);
color[8]=RGB(192,220,192);
color[9]=RGB(166,202,240);
color[10]=RGB(255,251,240);
color[11]=RGB(160,160,164);
color[12]=RGB(128,128,128);
color[13]=RGB(255,0,0);
color[14]=RGB(0,255,0);
color[15]=RGB(255,255,0);
color[16]=RGB(0,0,255);
color[17]=RGB(255,0,0);
color[18]=RGB(255,0,255);
color[19]=RGB(0,255,255);
color[20]=RGB(255,255,255);
}
CColorPicker::~CColorPicker()
{
}
BEGIN_MESSAGE_MAP(CColorPicker, CWnd)
//{{AFX_MSG_MAP(CColorPicker)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorPicker message handlers
BOOL CColorPicker::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
m_nStyle = dwStyle;
if (CWnd::Create(AfxRegisterWndClass(CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, AfxGetApp()->LoadStandardCursor(IDC_ARROW), (HBRUSH)GetStockObject(LTGRAY_BRUSH), NULL),
NULL,
dwStyle&~ WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
rect,
pParentWnd,
nID))
return TRUE;
return FALSE;
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
void CColorPicker::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
CBrush br;
// CBrush *pr;
int index=0;
this->GetClientRect(&rect);
long x1=rect.left+rect.Width()/10,y1=rect.top,x2=rect.right-rect.Width()/10,y2=rect.bottom;
for(int i=x1;i<x2;i+=(x2-x1)/10)
for(int j=y1;j<y2;j+=rect.Height()/2)
{ br.CreateSolidBrush(color[index]);
dc.SelectObject(&br);
dc.Rectangle(CRect(i,j,i+rect.Width()/10,j+rect.Height()/2));
index++;
br.Detach();
}
br.CreateSolidBrush(m_color);
dc.SelectObject(&br);
dc.Rectangle(CRect(0,0,rect.Width()/11,rect.Height()));
br.Detach();
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
}
void CColorPicker::OnLButtonDown(UINT nFlags, CPoint point)
{CRect rect;
//this->ClientToScreen(&point);
this->GetClientRect(&rect);
int step=0;
long x1=rect.left+rect.Width()/10,y1=rect.top,x2=rect.right-rect.Width()/10,y2=rect.bottom;
for(int i=x1;i<x2;i+=(x2-x1)/10)
for(int j=y1;j<y2;j+=rect.Height()/2)
{
if(point.x>i&&point.x<i+(x2-x1)/10
&&point.y>j&&point.y<j+rect.Height()/2)
{
m_color=color[step];//+(step)*519500;
}
step++;
}
this->Invalidate();
CWnd::OnLButtonDown(nFlags, point);
}
void CColorPicker::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CColorDialog dlg;
if(dlg.DoModal() == IDOK)
{m_color = dlg.GetColor();}
this->Invalidate();
CWnd::OnLButtonDblClk(nFlags, point);
}
COLORREF CColorPicker::GetColor()
{return m_color;
}
BOOL CColorPicker::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return CWnd::OnEraseBkgnd(pDC);
}
演示程序
本文地址:http://com.8s8s.com/it/it490.htm