📄 colourpicker.cpp
字号:
// ColourPicker.cpp : implementation file
//
#include "stdafx.h"
#include "CProgressCtrl.h"
#include "ColourPicker.h"
//#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColourPicker
IMPLEMENT_DYNCREATE(CColourPicker,CButton)
CColourPicker::CColourPicker()
{
SetBkColour(GetSysColor(COLOR_3DFACE));
SetTextColour(GetSysColor(COLOR_BTNTEXT));
m_bTrackSelection=FALSE;
m_nSelectionMode=CP_MODE_BK;
m_bActive=FALSE;
}
CColourPicker::~CColourPicker()
{
}
BEGIN_MESSAGE_MAP(CColourPicker, CButton)
//{{AFX_MSG_MAP(CColourPicker)
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_MESSAGE(CPN_SELENDOK, OnSelEndOK)
ON_MESSAGE(CPN_SELENDCANCEL,OnSelEndCancel)
ON_MESSAGE(CPN_SELCHANGE, OnSelChange)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColourPicker message handlers
void AFXAPI DDX_ColourPicker(CDataExchange* pDX,int nIDC,COLORREF& crColour)
{
//得到非编辑控件CColourPicker的窗体句柄
HWND hWndCtrl=pDX->PrepareCtrl(nIDC);
ASSERT(hWndCtrl!=NULL);
//由窗体句柄得到CColourPicker的窗体句柄
CColourPicker *pColourPicker=(CColourPicker* )CWnd::FromHandle(hWndCtrl);
//由变量m_bSaveandValideate 的指示状态来保存和获取颜色值
if(pDX->m_bSaveAndValidate)
{
crColour=pColourPicker->GetColour();
}
else
{
pColourPicker->SetColour(crColour);
}
}
int CColourPicker::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetWindowSize();
return 0;
}
void CColourPicker::OnClicked()
{
// TODO: Add your control notification handler code here
m_bActive=TRUE;
CRect rect;
GetWindowRect(rect);
new CColourPopup(CPoint(rect.left,rect.bottom),GetColour(),this,10,_T("其它颜色…"));
return ;
}
void CColourPicker::SetBkColour(COLORREF crColourBk)
{
m_crColourBk=crColourBk;
if(IsWindow(m_hWnd))
RedrawWindow();
}
void CColourPicker::SetTextColour(COLORREF crColourText)
{
m_crColourText=crColourText;
if(IsWindow(m_hWnd))
RedrawWindow();
}
void CColourPicker::SetWindowSize()
{
//得到边界的宽度
CSize MarginSize(::GetSystemMetrics(SM_CXEDGE),::GetSystemMetrics(SM_CYEDGE));
//得到下拉箭头的大小尺寸
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 rect;
GetWindowRect(rect);
CWnd* pParent=GetParent();
if(pParent)
//将rect矩形坐标转换为客户区坐标系统
//即以对话框窗体左上角为坐标参考点(0,0)
pParent->ScreenToClient(rect);
//将窗体设置的足够大
int nWidth=max(rect.Width(),2*ArrowSize.cx+2*MarginSize.cx);
MoveWindow(rect.left,rect.top,nWidth,ArrowSize.cy+2*MarginSize.cy,TRUE);
GetWindowRect(rect);
ScreenToClient(rect);
//设定下拉箭 头区域的位置
m_ArrowRect.SetRect(rect.right-ArrowSize.cx-MarginSize.cx,
rect.top+MarginSize.cy,rect.right-MarginSize.cx,
rect.bottom-MarginSize.cy);
}
LONG CColourPicker::OnSelChange(UINT lParam,LONG wParam)
{
if(m_bTrackSelection)
SetColour((COLORREF) lParam);
return TRUE;
}
LONG CColourPicker::OnSelEndCancel(UINT lParam,LONG wParam)
{
m_bActive =FALSE;
SetColour((COLORREF) lParam);
CWnd* pParent=GetParent();
return TRUE;
}
LONG CColourPicker::OnSelEndOK(UINT lParam,LONG wParam)
{
COLORREF crNewColour=(COLORREF) lParam;
m_bActive=FALSE;
SetColour(crNewColour);
CWnd* pParent=GetParent();
if(crNewColour!=GetColour())
if(pParent) pParent->SendMessage(CPN_SELCHANGE,lParam,(WPARAM)GetDlgCtrlID());
return TRUE;
}
void CColourPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
ASSERT(lpDrawItemStruct);
CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect =lpDrawItemStruct->rcItem;
UINT state =lpDrawItemStruct->itemState;
DWORD dwStyle=GetStyle();
CSize Margins(::GetSystemMetrics(SM_CXEDGE),::GetSystemMetrics(SM_CYEDGE));
//绘制下拉箭头
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);
rect.DeflateRect(Margins.cx,Margins.cy);
//除去下拉箭头外的区域并涂上颜色
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);
//写窗体文本
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);
}
}
//绘制选中的颜色拾取器
if(state&ODS_FOCUS)
{
rect.DeflateRect(1,1);
pDC->DrawFocusRect(rect);
}
}
void CColourPicker::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
ModifyStyle(0,BS_OWNERDRAW);
CButton::PreSubclassWindow();
SetWindowSize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -