📄 colorpopup.cpp
字号:
//贵州省安顺市第二高级中学 高二一班 蒋伟
//2002.7.1
//请勿修改版权信息 谢谢
//Neil
//
#include "stdafx.h"
#include <math.h>
#include "ColorPopup.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DEFAULT_BOX_VALUE -3
#define CUSTOM_BOX_VALUE -2
#define INVALID_COLOUR -1
#define MAX_COLOURS 100
#define MAX_OFFSET 5 // X direction offset
#define MAX_BOTTOM 15 // Y direction offset and to the bottom
#define MAX_TEXTRECT -5 // Width of the default text rect and the onwer text rect
ColorTableEntry CColorPopup::m_crColors[] =
{
{ RGB(0x01, 0x00, 0x00), _T("黑色") },
{ RGB(0xA5, 0x2A, 0x00), _T("褐色") },
{ RGB(0x00, 0x40, 0x40), _T("橄榄绿") },
{ RGB(0x00, 0x55, 0x00), _T("深绿") },
{ RGB(0x00, 0x00, 0x5E), _T("深灰蓝") },
{ RGB(0x00, 0x00, 0x8B), _T("深蓝") },
{ RGB(0x4B, 0x00, 0x82), _T("靛蓝") },
{ RGB(0x33, 0x33, 0x33), _T("深灰-80%") },
{ RGB(0x8B, 0x00, 0x00), _T("深红") },
{ RGB(0xFF, 0x68, 0x20), _T("桔黄") },
{ RGB(0x8B, 0x8B, 0x00), _T("深黄") },
{ RGB(0x00, 0x93, 0x00), _T("绿色") },
{ RGB(0x38, 0x8E, 0x8E), _T("青色") },
{ RGB(0x00, 0x00, 0xFF), _T("蓝色") },
{ RGB(0x7B, 0x7B, 0xC0), _T("蓝灰") },
{ RGB(0x7F, 0x7F, 0x7F), _T("灰色-50%") },
{ RGB(0xFF, 0x00, 0x00), _T("红色") },
{ RGB(0xFF, 0xAD, 0x5B), _T("浅桔色") },
{ RGB(0x32, 0xCD, 0x32), _T("酸桔色") },
{ RGB(0x3C, 0xB3, 0x71), _T("海绿") },
{ RGB(0x7F, 0xFF, 0xD4), _T("宝石蓝") },
{ RGB(0x7D, 0x9E, 0xC0), _T("浅蓝") },
{ RGB(0x80, 0x00, 0x80), _T("紫色") },
{ RGB(0x99, 0x99, 0x99), _T("灰色-40%") },
{ RGB(0xFF, 0xC0, 0xCB), _T("粉红") },
{ RGB(0xFF, 0xD7, 0x00), _T("金色") },
{ RGB(0xFF, 0xFF, 0x00), _T("黄色") },
{ RGB(0x00, 0xFF, 0x00), _T("鲜绿") },
{ RGB(0x40, 0xE0, 0xD0), _T("青绿") },
{ RGB(0xC0, 0xFF, 0xFF), _T("天蓝") },
{ RGB(0x48, 0x00, 0x48), _T("梅红") },
{ RGB(0xBF, 0xBF, 0xBF), _T("灰色-25%") },
{ RGB(0xFF, 0xE4, 0xE1), _T("玫瑰红") },
{ RGB(0xD2, 0xB4, 0x8C), _T("棕色") },
{ RGB(0xFF, 0xFF, 0xE0), _T("浅黄") },
{ RGB(0x98, 0xFB, 0x98), _T("浅绿 ") },
{ RGB(0xAF, 0xEE, 0xEE), _T("浅青绿") },
{ RGB(0x68, 0x83, 0x8B), _T("淡蓝") },
{ RGB(0xE6, 0xE6, 0xFA), _T("紫色") },
{ RGB(0xFF, 0xFF, 0xFF), _T("白色") }
};
/////////////////////////////////////////////////////////////////////////////
// CColorPopup
CColorPopup::CColorPopup()
{
Initialise();
}
CColorPopup::CColorPopup(CPoint p, COLORREF crColor, CWnd* pParentWnd,
LPCTSTR szDefaultText /* = NULL */,
LPCTSTR szCustomText /* = NULL */)
{
Initialise();
m_crColor = m_crInitialColor = crColor;
m_pParent = pParentWnd;
m_strDefaultText = (szDefaultText)? szDefaultText : _T("");
m_strCustomText = (szCustomText)? szCustomText : _T("");
m_crDefaultColor = ::GetSysColor(COLOR_BTNTEXT);
CColorPopup::Create(p, crColor, pParentWnd, szDefaultText, szCustomText);
}
////////////////////////////////////////////////////////////////////
void CColorPopup::Initialise()
{
m_nNumColors = sizeof(m_crColors)/sizeof(ColorTableEntry);
ASSERT(m_nNumColors <= MAX_COLOURS);
if (m_nNumColors > MAX_COLOURS)
m_nNumColors = MAX_COLOURS;
m_nNumColumns = 0;
m_nNumRows = 0;
m_nBoxSize = 18;
m_nMargin = ::GetSystemMetrics(SM_CXEDGE);
m_nCurrentSel = INVALID_COLOUR;
m_nChosenColorSel = INVALID_COLOUR;
m_pParent = NULL;
m_crColor = m_crInitialColor = RGB(0,0,0);
// Make sure the colour square is at least 5 x 5;
if (m_nBoxSize - 2*m_nMargin - 2 < 5) m_nBoxSize = 5 + 2*m_nMargin + 2;
// Create the font
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(NONCLIENTMETRICS);
VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
sizeof(NONCLIENTMETRICS), &ncm, 0));
m_Font.CreateFontIndirect(&(ncm.lfMessageFont));
// Create the palette
struct {
LOGPALETTE LogPalette;
PALETTEENTRY PalEntry[MAX_COLOURS];
} pal;
LOGPALETTE* pLogPalette = (LOGPALETTE*) &pal;
pLogPalette->palVersion = 0x300;
pLogPalette->palNumEntries = (WORD) m_nNumColors;
for (int i = 0; i < m_nNumColors; i++)
{
pLogPalette->palPalEntry[i].peRed = GetRValue(m_crColors[i].crColor);
pLogPalette->palPalEntry[i].peGreen = GetGValue(m_crColors[i].crColor);
pLogPalette->palPalEntry[i].peBlue = GetBValue(m_crColors[i].crColor);
pLogPalette->palPalEntry[i].peFlags = 0;
}
m_Palette.CreatePalette(pLogPalette);
}
////////////////////////////////////////////////////////////////////
CColorPopup::~CColorPopup()
{
m_Font.DeleteObject();
m_Palette.DeleteObject();
}
//////////////////////////////////////////////////////////////////////
BOOL CColorPopup::Create(CPoint p, COLORREF crColor, CWnd* pParentWnd,
LPCTSTR szDefaultText,
LPCTSTR szCustomText )
{
ASSERT(pParentWnd && ::IsWindow(pParentWnd->GetSafeHwnd()));
//ASSERT(pParentWnd->IsKindOf(RUNTIME_CLASS(CColorButton)));
m_pParent = pParentWnd;
m_crColor = m_crInitialColor = crColor;
// Get the class name and create the window
CString szClassName = AfxRegisterWndClass(CS_CLASSDC|CS_SAVEBITS|CS_HREDRAW|
CS_VREDRAW, 0,
(HBRUSH)GetStockObject(LTGRAY_BRUSH),0);
if (!CWnd::CreateEx(0, szClassName, _T(""), WS_VISIBLE|WS_POPUP,
p.x, p.y, 100, 100, // size updated soon
pParentWnd->GetSafeHwnd(), 0, NULL))
return FALSE;
// Store the Custom text
if (szCustomText != NULL)
m_strCustomText = szCustomText;
// Store the Default Area text
if (szDefaultText != NULL)
m_strDefaultText = szDefaultText;
// Set the window size
SetWindowSize();
// Create the tooltips
CreateToolTips();
// Find which cell corresponds to the initial color
FindCellFromColor(crColor);
// Capture all mouse events for the life of this window
SetCapture();
return TRUE;
}
BEGIN_MESSAGE_MAP(CColorPopup, CWnd)
//{{AFX_MSG_MAP(CColorPopup)
ON_WM_NCDESTROY()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
ON_WM_QUERYNEWPALETTE()
ON_WM_PALETTECHANGED()
ON_WM_KILLFOCUS()
ON_WM_ACTIVATEAPP()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColorPopup message handlers
// For tooltips
BOOL CColorPopup::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CWnd::PreTranslateMessage(pMsg);
}
///////////////////////////////////////////////////////////////////
// If an arrow key is pressed, then move the selection
void CColorPopup::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int row = GetRow(m_nCurrentSel);
int col = GetColumn(m_nCurrentSel);
int nSelection = m_nCurrentSel;
if (nChar == VK_DOWN)
{
if (row == DEFAULT_BOX_VALUE)
row = col = 0;
else if (row == CUSTOM_BOX_VALUE)
{
if (m_strDefaultText.GetLength())
row = col = DEFAULT_BOX_VALUE;
else
row = col = 0;
}
else
{
row++;
if (GetIndex(row,col) < 0)
{
if (m_strCustomText.GetLength())
row = col = CUSTOM_BOX_VALUE;
else if (m_strDefaultText.GetLength())
row = col = DEFAULT_BOX_VALUE;
else
row = col = 0;
}
}
ChangeSelection(GetIndex(row, col));
}
if (nChar == VK_UP)
{
if (row == DEFAULT_BOX_VALUE)
{
if (m_strCustomText.GetLength())
row = col = CUSTOM_BOX_VALUE;
else
{
row = GetRow(m_nNumColors-1);
col = GetColumn(m_nNumColors-1);
}
}
else if (row == CUSTOM_BOX_VALUE)
{
row = GetRow(m_nNumColors-1);
col = GetColumn(m_nNumColors-1);
}
else if (row > 0) row--;
else
{
if (m_strDefaultText.GetLength())
row = col = DEFAULT_BOX_VALUE;
else if (m_strCustomText.GetLength())
row = col = CUSTOM_BOX_VALUE;
else
{
row = GetRow(m_nNumColors-1);
col = GetColumn(m_nNumColors-1);
}
}
ChangeSelection(GetIndex(row, col));
}
if (nChar == VK_RIGHT)
{
if (row == DEFAULT_BOX_VALUE)
row = col = 0;
else if (row == CUSTOM_BOX_VALUE)
{
if (m_strDefaultText.GetLength())
row = col = DEFAULT_BOX_VALUE;
else
row = col = 0;
}
else if (col < m_nNumColumns-1)
col++;
else
{
col = 0; row++;
}
if (GetIndex(row,col) == INVALID_COLOUR)
{
if (m_strCustomText.GetLength())
row = col = CUSTOM_BOX_VALUE;
else if (m_strDefaultText.GetLength())
row = col = DEFAULT_BOX_VALUE;
else
row = col = 0;
}
ChangeSelection(GetIndex(row, col));
}
if (nChar == VK_LEFT)
{
if (row == DEFAULT_BOX_VALUE)
{
if (m_strCustomText.GetLength())
row = col = CUSTOM_BOX_VALUE;
else
{
row = GetRow(m_nNumColors-1);
col = GetColumn(m_nNumColors-1);
}
}
else if (row == CUSTOM_BOX_VALUE)
{
row = GetRow(m_nNumColors-1);
col = GetColumn(m_nNumColors-1);
}
else if (col > 0) col--;
else /* col == 0 */
{
if (row > 0) { row--; col = m_nNumColumns-1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -