📄 colorpane.cpp
字号:
// ColorPane.cpp : implementation file
//
#include "stdafx.h"
#include "Mainfrm.h"
#include "Ll.h"
#include "ColorPane.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CColorPane
const UINT ID_COLOR_DIALOG = ::RegisterWindowMessage("ID_COLOR_DIALOG");
CColorPane::CColorPane()
{
m_nColorPicked = -1;
m_clrCurrentColor = 0;
}
CColorPane::~CColorPane()
{
}
BEGIN_MESSAGE_MAP(CColorPane, CToolBar)
//{{AFX_MSG_MAP(CColorPane)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDBLCLK()
//}}AFX_MSG_MAP
ON_COMMAND(ID_COLOR_DIALOG,OnColorDialog)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorPane message handlers
COLORREF CColorPane::s_clrMosaic[28] = {
RGB(0, 0, 0), RGB(255, 255, 255), RGB(128, 128, 128), RGB(192, 192, 192),
RGB(128, 0, 0), RGB(255, 0, 0), RGB(128, 128, 64), RGB(255, 255, 0),
RGB(0, 128, 0), RGB(128, 255, 0), RGB(0, 128, 128), RGB(0, 255, 255),
RGB(0, 0, 128), RGB(0, 0, 255), RGB(128, 0, 128), RGB(255, 0, 255),
RGB(128, 128, 0), RGB(255, 255, 128), RGB(0, 64, 64), RGB(128, 255, 128),
RGB(0, 128, 255), RGB(128, 255, 255), RGB(0, 64, 128), RGB(128, 128, 255),
RGB(0, 0, 255), RGB(255, 0, 128), RGB(128, 64, 64), RGB(255, 128, 64)
};
void CColorPane::InitColorBox(CRect& rc)
{
int nHorzGap = 17;
int nVertGap = 17;
int nWidth = 15;
int nHeight = 15;
for(int i=0; i<14; i++)
{
m_rcColorBox[2*i].left = rc.left+42+i*nHorzGap;
m_rcColorBox[2*i].right = m_rcColorBox[2*i].left+nWidth;
m_rcColorBox[2*i].top = rc.top+5;
m_rcColorBox[2*i].bottom = m_rcColorBox[2*i].top+nHeight;
m_rcColorBox[2*i+1].left = m_rcColorBox[2*i].left;
m_rcColorBox[2*i+1].right = m_rcColorBox[2*i+1].left+nWidth;
m_rcColorBox[2*i+1].top = m_rcColorBox[2*i].top+nVertGap;
m_rcColorBox[2*i+1].bottom = m_rcColorBox[2*i+1].top+nHeight;
}
}
int CColorPane::PtInColorBox(POINT point)
{
for ( int i=0; i < 28; i++ )
if ( m_rcColorBox[i].PtInRect(point) ) break;
if ( i < 28 ) return i;
else return -1;
}
void CColorPane::DrawBigColorBox(CDC& dc, CRect& rc)
{
// Draw a big rectangle showing on left to display the color selected.
CRect rcBox(rc.left+5,rc.top+5,rc.left+38,rc.top+38);
dc.Draw3dRect(rcBox.left, rcBox.top, rcBox.Width(), rcBox.Height(), RGB(0, 0, 0), RGB(255, 255, 255));
int nBoxWidth = 16;
int nBoxHeight = 16;
int nBackOffset = 12;
int nFrontOffset = 5;
CRect rcBack(rcBox.left+nBackOffset, rcBox.top+nBackOffset, rcBox.left+nBackOffset+nBoxWidth, rcBox.top+nBackOffset+nBoxHeight);
dc.Draw3dRect(rcBack.left, rcBack.top, rcBack.Width(), rcBack.Height(),
RGB(255, 255, 255), RGB(0, 0, 0));
{// Draw canvas color box.
CPen pen(PS_SOLID, 1, GetCanvasColor());
CBrush br(GetCanvasColor());
CPen* pOldPen = dc.SelectObject(&pen);
CBrush* pOldBrush = dc.SelectObject(&br);
dc.Rectangle(rcBack.left+1, rcBack.top+1, rcBack.right-2, rcBack.bottom-2);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
}
CRect rcFront(rcBox.left+nFrontOffset, rcBox.top+nFrontOffset, rcBox.left+nFrontOffset+nBoxWidth, rcBox.top+nFrontOffset+nBoxHeight);
dc.Draw3dRect(rcFront.left, rcFront.top, rcFront.Width(), rcFront.Height(),
RGB(255, 255, 255), RGB(0, 0, 0));
{ // Draw paint color box in front.
CPen pen(PS_SOLID, 1, RGB(208,195,186)); // pen's color is same as background.
CBrush br(GetColor());
CPen* pOldPen = dc.SelectObject(&pen);
CBrush* pOldBrush = dc.SelectObject(&br);
dc.Rectangle(rcFront.left, rcFront.top, rcFront.right-1, rcFront.bottom-1);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
}
}
void CColorPane::DrawASmallColorBox(CDC& dc, CRect& rc, COLORREF color)
{
dc.Draw3dRect(rc.left, rc.top, rc.Width(), rc.Height(), RGB(0, 0, 0), RGB(255, 255, 255));
CPen pen(PS_SOLID, 1, color);
CBrush brush(color);
CPen* pOldPen = dc.SelectObject(&pen);
CBrush* pOldBrush = dc.SelectObject(&brush);
dc.Rectangle(rc.left+1, rc.top+1, rc.left+rc.Width()-1,rc.top+rc.Height()-1);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
}
void CColorPane::DrawSmallColorBoxes(CDC& dc, CRect& rc)
{
// draw each small color boxes.
for(int i=0; i < 28; i++)
{
DrawASmallColorBox(dc,m_rcColorBox[i],s_clrMosaic[i]);
}
}
void CColorPane::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CRect rc;
GetClientRect(&rc);
static BOOL bFirst=TRUE;
if ( bFirst ) // initialize once only.
InitColorBox(rc);
else
bFirst = FALSE;
DrawBigColorBox(dc, rc);
DrawSmallColorBoxes(dc, rc);
// Do not call CToolBarCtrl::OnPaint() for painting messages
}
COLORREF CColorPane::GetColor() const
{
// default color is black.
if( m_nColorPicked < -1 || m_nColorPicked > 27 )
return m_clrCurrentColor;
else if ( m_nColorPicked == -1 )
return s_clrMosaic[0];
else
return s_clrMosaic[m_nColorPicked];
}
COLORREF CColorPane::GetPaintColor() const
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if ( pFrame )
return pFrame->m_clrPaint;
else
return RGB(0,0,0);
}
void CColorPane::SetPaintColor(COLORREF color)
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if ( pFrame )
pFrame->m_clrPaint = color;
}
COLORREF CColorPane::GetCanvasColor() const
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if ( pFrame )
return pFrame->m_clrCanvas;
else
return RGB(255,255,255);
}
void CColorPane::SetCanvasColor(COLORREF color)
{
CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
if ( pFrame )
pFrame->m_clrCanvas = color;
}
void CColorPane::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CToolBar::OnMouseMove(nFlags, point);
}
void CColorPane::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CToolBar::OnLButtonDown(nFlags, point);
}
void CColorPane::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int nBoxIndex = PtInColorBox(point);
if ( nBoxIndex > -1 )
{
m_nColorPicked = nBoxIndex;
SetPaintColor(s_clrMosaic[m_nColorPicked]);
Invalidate();
}
CToolBar::OnLButtonUp(nFlags, point);
}
void CColorPane::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int nBoxIndex = PtInColorBox(point);
if ( nBoxIndex > -1 )
{
m_nColorPicked = nBoxIndex;
// it can't run dialog function in mouse procedure.
PostMessage(WM_COMMAND, ID_COLOR_DIALOG);
}
CToolBar::OnLButtonDblClk(nFlags, point);
}
void CColorPane::OnColorDialog()
{
CColorDialog dlg(GetColor(),0,NULL);
if ( dlg.DoModal() == IDOK )
{
s_clrMosaic[m_nColorPicked] = dlg.GetColor();
SetPaintColor(s_clrMosaic[m_nColorPicked]);
Invalidate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -