📄 colorpicker.cpp
字号:
////////////////////////////////////////////////////////////////
// ColorPicker.cpp : implementation file //
// //
// Copyright 2001 WangJun //
// All Rights Reserved. //
// //
// Email: wangjun98@sohu.com //
// URL: http://www.vckbase.com //
// //
// 1.0 2001/10/6 First release version. //
// //
////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ColorPicker.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define LPARAM_X(lp) ((int)(short)LOWORD(lp))
#define LPARAM_Y(lp) ((int)(short)HIWORD(lp))
#define WM_SETCOLOR WM_USER+1
#define WM_COLORDLG WM_USER+2
#define IDC_COLORDLG_BUTTON 100
CPen _pen3DDKShadow(PS_SOLID,1,::GetSysColor(COLOR_3DDKSHADOW));
CPen _penW(PS_SOLID,1,RGB(0xff,0xff,0xff));
CPen _penB(PS_SOLID,1,RGB(0,0,0));
/////////////////////////////////////////////////////////////////////////////
// CColorPicker
CColorPicker::CColorPicker()
{
const unsigned char pvANDPlaneC[]={0xff,0xf1,0xff,0xe0,0xff,0xc0,0xff,0x00,0xff,0x81,0xff,0x03,0xfe,0x07,0xfc,0x17,
0xf8,0x3f,0xf0,0x7f,0xe0,0xff,0xc1,0xff,0x83,0xff,0x87,0xff,0x0f,0xff,0x3f,0xff};
const unsigned char pvXORPlaneC[]={0x00,0x00,0x00,0x0c,0x00,0x08,0x00,0x10,0x00,0x00,0x00,0x40,0x00,0xe0,0x01,0xc0,
0x03,0x80,0x07,0x00,0x0e,0x00,0x1c,0x00,0x38,0x00,0x30,0x00,0x40,0x00,0x00,0x00};
int cxCursor = ::GetSystemMetrics(SM_CXCURSOR);
int cyCursor = ::GetSystemMetrics(SM_CYCURSOR);
m_hCursorStraw = NULL;
if(cxCursor >=16 && cxCursor < 100 && cyCursor < 100 && cyCursor >= 16)
{
int size = cxCursor*cyCursor/8;
unsigned char *pvANDPlane = new unsigned char[size];
unsigned char *pvXORPlane = new unsigned char[size];
if(pvANDPlane && pvXORPlane)
{
memset(pvANDPlane,0xff,size);
memset(pvXORPlane,0x00,size);
for(int j=0;j<16;j++)
for(int i=0;i<2;i++)
{
*(pvANDPlane+j*cxCursor/8 + i) = *(pvANDPlaneC + j*2 + i);
*(pvXORPlane+j*cxCursor/8 + i) = *(pvXORPlaneC + j*2 + i);
}
m_hCursorStraw = ::CreateCursor(::AfxGetInstanceHandle(),0,15,cxCursor,cyCursor,pvANDPlane,pvXORPlane);
delete pvANDPlane;
delete pvXORPlane;
}
}
m_CurrentColor = COLORREF(::GetSysColor(COLOR_3DFACE));
m_hwndBuddy = NULL;
m_hPaletteWnd = NULL;
m_bPaletteWndActive = FALSE;
}
CColorPicker::~CColorPicker()
{
::DestroyCursor(m_hCursorStraw);
}
BEGIN_MESSAGE_MAP(CColorPicker, CButton)
//{{AFX_MSG_MAP(CColorPicker)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorPicker message handlers
void CColorPicker::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
}
BOOL CColorPicker::OnEraseBkgnd(CDC* pDC)
{
CPen *pOldPen;
COLORREF clr3DFace = GetSysColor(COLOR_3DFACE);
CRect rect;
GetClientRect(&rect);
if(!m_bPaletteWndActive)
{
pDC->Draw3dRect (rect,RGB(0xff,0xff,0xff),GetSysColor (COLOR_3DDKSHADOW));
rect.DeflateRect(1,1);
pDC->Draw3dRect (rect,clr3DFace,clr3DFace);
rect.InflateRect(1,1);
}
rect.DeflateRect(3,3);
pDC->FillSolidRect(rect,m_CurrentColor);
rect.left -= 1;rect.top -= 1;
pOldPen = pDC->SelectObject(&_pen3DDKShadow);
pDC->MoveTo(rect.right - 1,rect.top);
pDC->LineTo(rect.left,rect.top);
pDC->LineTo(rect.left,rect.bottom);
pDC->SelectObject(&_penW);
pDC->LineTo(rect.right - 6,rect.bottom);
pDC->LineTo(rect.right - 6,rect.bottom - 4);
pDC->LineTo(rect.right,rect.bottom - 4);
pDC->MoveTo(rect.right,rect.top);
pDC->LineTo(rect.right,rect.bottom - 3);
rect.right += 1;
rect.bottom += 1;
rect.left = rect.right - 6;
rect.top = rect.bottom - 4;
pDC->FillSolidRect(rect,clr3DFace);
pDC->SelectObject(&_penB);
pDC->MoveTo(rect.left + 1,rect.top + 1);
pDC->LineTo(rect.right,rect.top + 1);
pDC->MoveTo(rect.left + 2,rect.top + 2);
pDC->LineTo(rect.right-1,rect.top + 2);
pDC->MoveTo(rect.left + 3,rect.top + 3);
pDC->LineTo(rect.right-2,rect.top + 3);
pDC->SelectObject(pOldPen);
return TRUE;
}
void CColorPicker::OnLButtonDown(UINT nFlags, CPoint point)
{
CButton::OnLButtonDown(nFlags, point);
::ClientToScreen(this->m_hWnd,&point);
if(CreatePaletteWindow())
{
nFlags = (UINT)m_CurrentColor;
::PostMessage(m_hPaletteWnd,WM_LBUTTONDOWN,nFlags,MAKELPARAM(point.x,point.y));
}
HWND hwndParent = GetParent()->m_hWnd;
if(hwndParent)
{
POINT p1,p2;
RECT rect;
::GetWindowRect(m_hWnd,&rect);
p1.x = rect.left;
p1.y = rect.top;
p2.x = rect.right;
p2.y = rect.bottom;
::ScreenToClient(hwndParent,&p1);
::ScreenToClient(hwndParent,&p2);
rect.left = p1.x;
rect.top = p1.y;
rect.right = p2.x;
rect.bottom = p2.y;
::InvalidateRect(hwndParent,&rect,TRUE);
}
}
void CColorPicker::OnLButtonUp(UINT nFlags, CPoint point)
{
DestroyPaletteWindow();
if((COLORREF)nFlags != m_CurrentColor)
SetColor((COLORREF)nFlags);
else
Invalidate();
}
LONG FAR PASCAL CColorPicker::PaletteWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
POINT point;
CRect crtColorRect(5,125,40,143);
static POINT oldPoint;
static BOOL bMouseMoved = FALSE;
static HWND hwndParent;
static HWND hwndColorBtn;
static HFONT hFont = NULL;
static COLORREF crtColor,oldColor;
static int iActiveRect = -1;
switch(nMsg)
{
case WM_CREATE:
{
hwndParent = ((LPCREATESTRUCT)lParam)->hwndParent;
crtColor = ::GetSysColor(COLOR_3DFACE);
LOGFONT logFont;
ZeroMemory((void*)&logFont,sizeof(logFont));
strcpy(logFont.lfFaceName,"宋体");
logFont.lfHeight = -12;
logFont.lfWeight = 400;
logFont.lfCharSet = GB2312_CHARSET;
logFont.lfOutPrecision = 3;
logFont.lfClipPrecision = 2;
logFont.lfQuality = 1;
logFont.lfPitchAndFamily = 2;
hFont = ::CreateFontIndirect(&logFont);
////色彩对话框按钮
hwndColorBtn = ::CreateWindow("BUTTON","",WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,167,126,16,16,hWnd,HMENU(IDC_COLORDLG_BUTTON),::AfxGetInstanceHandle(),NULL);
if(hwndColorBtn)
::ShowWindow(hwndColorBtn,SW_SHOW);
}
break;
case WM_DESTROY:
::DeleteObject(hFont);
break;
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT lpDis = (LPDRAWITEMSTRUCT)lParam;
if(lpDis->CtlID == IDC_COLORDLG_BUTTON)
{
CRect rect(3,3,8,8);
::FillRect(lpDis->hDC,&lpDis->rcItem,(HBRUSH)::GetStockObject(LTGRAY_BRUSH));
HPEN hOldPen = (HPEN)::SelectObject(lpDis->hDC,_penW);
if(lpDis->itemState == 17)
{
::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
::SelectObject(lpDis->hDC,_penB);
::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
rect.OffsetRect(1,1);
}
else
{
::MoveToEx(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top,NULL);
::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.top);
::LineTo(lpDis->hDC,lpDis->rcItem.left,lpDis->rcItem.bottom-1);
::SelectObject(lpDis->hDC,_penB);
::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.bottom-1);
::LineTo(lpDis->hDC,lpDis->rcItem.right-1,lpDis->rcItem.top);
}
::SelectObject(lpDis->hDC,_pen3DDKShadow);
::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
::SetBkColor(lpDis->hDC, RGB(0xff,0,0));
::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.OffsetRect(2,2);
::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
::SetBkColor(lpDis->hDC, RGB(0,0xff,0));
::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
rect.OffsetRect(2,2);
::Rectangle(lpDis->hDC,rect.left-1,rect.top-1,rect.right+1,rect.bottom+1);
::SetBkColor(lpDis->hDC, RGB(0xff,0xff,0));
::ExtTextOut(lpDis->hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
::SelectObject(lpDis->hDC,hOldPen);
}
}
break;
case WM_SETCOLOR:
crtColor = (COLORREF)wParam;
oldColor = crtColor;
break;
case WM_COMMAND:
::PostMessage(hwndParent,WM_COLORDLG,NULL,NULL);
break;
case WM_MOUSEMOVE:
{
BOOL bInCtrlArea = FALSE;
point.x = LPARAM_X(lParam);
point.y = LPARAM_Y(lParam);
::ClientToScreen(hWnd,&point);
if(point.x != oldPoint.x || point.y != oldPoint.y)
{
bMouseMoved = TRUE;
HWND hwndPoint = ::WindowFromPoint(point);
if(hwndPoint)
{
COLORREF crtTMPColor;
CRect rect;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -