📄 xcolourpopup.cpp
字号:
// XColourPopup.cpp : implementation file
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Extended by Alexander Bischofberger (bischofb@informatik.tu-muenchen.de)
// Copyright (c) 1998.
//
// Updated 30 May 1998 to allow any number of colours, and to
// make the appearance closer to Office 97.
// Also added "Default" text area. (CJM)
//
// 13 June 1998 Fixed change of focus bug (CJM)
// 30 June 1998 Fixed bug caused by focus bug fix (D'oh!!)
// Solution suggested by Paul Wilkerson.
//
// ColourPopup is a helper class for the colour picker control
// CColourPicker. Check out the header file or the accompanying
// HTML doc file for details.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
#include "stdafx.h"
#include <math.h>
#include "XColourPicker.h"
#include "XColourPopup.h"
#include "windowsx.h" // for GET_X_LPARAM
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DIVIDER_LINE_ROW 9
#define CUSTOM_BUTTON_ROW 10
#define FIRST_CUSTOM_ROW 11
///////////////////////////////////////////////////////////////////////////////
// The following builtin color table is extracted from the complete list of
// X11 colors that are supported by Internet Explorer and Netscape Navigator.
// To see the complete list of colors and color names, read the article by
// Robert Hess at
// http://www.microsoft.com/workshop/design/color/colorname.asp
ColorTableEntry CXColourPopup::m_crColors[] =
{
// row 0
RGB(128, 0, 64), _T("Light Maroon"), -1,
RGB(139, 0, 0), _T("Dark Red"), -1,
RGB(255, 0, 0), _T("Red"), -1,
RGB(255, 0, 128), _T("Deep Pink"), -1,
RGB(255, 0, 255), _T("Magenta"), -1,
RGB(255, 128, 192), _T("Pink"), -1,
RGB(255, 128, 255), _T("Light Pink"), -1,
RGB(255, 208, 232), _T("Very Light Pink"), -1,
// row 1
RGB(255, 140, 0), _T("Dark Orange"), -1,
RGB(255, 165, 0), _T("Orange"), -1,
RGB(255, 128, 128), _T("Light Salmon"), -1,
RGB(255, 224, 160), _T("Light Orange"), -1,
RGB(255, 215, 0), _T("Gold"), -1,
RGB(255, 255, 0), _T("Yellow"), -1,
RGB(255, 255, 128), _T("Desert Yellow"), -1,
RGB(255, 255, 184), _T("Pale Yellow"), -1,
// row 2
RGB( 0, 64, 0), _T("Dark Green"), -1,
RGB( 0, 64, 64), _T("Medium Green"), -1,
RGB( 0, 128, 0), _T("Green"), -1,
RGB( 0, 128, 64), _T("Dark Teal"), -1,
RGB(128, 128, 0), _T("Olive"), -1,
RGB( 0, 128, 128), _T("Teal"), -1,
RGB( 64, 128, 128), _T("Turquoise"), -1,
RGB( 0, 255, 64), _T("Medium Spring Green"), -1,
// row 3
RGB(128, 255, 0), _T("Mint Green"), -1,
RGB( 0, 255, 0), _T("Lime"), -1,
RGB( 0, 255, 128), _T("Spring Green"), -1,
RGB(152, 251, 152), _T("Pale Green"), -1,
RGB(127, 255, 212), _T("Aquamarine"), -1,
RGB(192, 220, 192), _T("Very Pale Green"), -1,
RGB( 64, 0, 64), _T("Deep Purple"), -1,
RGB(128, 0, 128), _T("Purple"), -1,
// row 4
RGB( 64, 0, 128), _T("Deep Violet"), -1,
RGB( 0, 0, 64), _T("Midnight Blue"), -1,
RGB( 0, 0, 160), _T("Dark Blue"), -1,
RGB( 0, 0, 128), _T("Navy"), -1,
RGB(128, 0, 255), _T("Violet"), -1,
RGB( 0, 0, 255), _T("Blue"), -1,
RGB( 0, 64, 128), _T("Medium Blue"), -1,
RGB( 0, 128, 255), _T("Light Sky Blue"), -1,
// row 5
RGB(100, 149, 237), _T("Cornflower Blue"), -1,
RGB(128, 128, 192), _T("Sky Blue"), -1,
RGB( 0, 128, 192), _T("Dark Cyan"), -1,
RGB(128, 128, 255), _T("Medium Slate Blue"), -1,
RGB(128, 255, 255), _T("Light Blue"), -1,
RGB( 0, 255, 255), _T("Cyan"), -1,
RGB(176, 224, 230), _T("Powder Blue"), -1,
RGB(224, 255, 255), _T("Light Cyan"), -1,
// row 6
RGB( 0, 0, 0), _T("Black"), -1,
RGB(128, 128, 128), _T("Gray"), -1,
RGB(152, 152, 152), _T("Gray 40%"), -1,
RGB(192, 192, 192), _T("Silver"), -1,
RGB(211, 211, 211), _T("Light Gray"), -1,
RGB(245, 245, 220), _T("Beige"), -1,
RGB(255, 251, 240), _T("Floral White"), -1,
RGB(255, 255, 255), _T("White"), -1,
// row 7 - window system colors
RGB( 0, 0, 0), _T("Window"), COLOR_WINDOW,
RGB( 0, 0, 0), _T("WindowText"), COLOR_WINDOWTEXT,
RGB( 0, 0, 0), _T("InfoBk"), COLOR_INFOBK,
RGB( 0, 0, 0), _T("InfoText"), COLOR_INFOTEXT,
RGB( 0, 0, 0), _T("ActiveCaption"), COLOR_ACTIVECAPTION,
RGB( 0, 0, 0), _T("CaptionText"), COLOR_CAPTIONTEXT,
RGB( 0, 0, 0), _T("InactiveCaption"), COLOR_INACTIVECAPTION,
RGB( 0, 0, 0), _T("InactiveCaptionText"), COLOR_INACTIVECAPTIONTEXT,
// row 8 - window system colors, row #2
RGB( 0, 0, 0), _T("BtnFace"), COLOR_BTNFACE,
RGB( 0, 0, 0), _T("BtnText"), COLOR_BTNTEXT,
RGB( 0, 0, 0), _T("BtnShadow"), COLOR_BTNSHADOW,
RGB( 0, 0, 0), _T("BtnHighlight"), COLOR_BTNHIGHLIGHT,
RGB( 0, 0, 0), _T("Highlight"), COLOR_HIGHLIGHT,
RGB( 0, 0, 0), _T("HighlightText"), COLOR_HIGHLIGHTTEXT,
RGB( 0, 0, 0), _T("3DLight"), COLOR_3DLIGHT,
RGB( 0, 0, 0), _T("Desktop"), COLOR_DESKTOP,
// row 9 - divider line
RGB(192, 192, 192), _T("1"), -1,
RGB(192, 192, 192), _T("2"), -1,
RGB(192, 192, 192), _T("3"), -1,
RGB(192, 192, 192), _T("4"), -1,
RGB(192, 192, 192), _T("5"), -1,
RGB(192, 192, 192), _T("6"), -1,
RGB(192, 192, 192), _T("7"), -1,
RGB(192, 192, 192), _T("8"), -1,
// row 10 - custom button
RGB(192, 192, 192), _T("9"), -1,
RGB(192, 192, 192), _T("10"), -1,
RGB(192, 192, 192), _T("11"), -1,
RGB(192, 192, 192), _T("12"), -1,
RGB(192, 192, 192), _T("13"), -1,
RGB(192, 192, 192), _T("14"), -1,
RGB(192, 192, 192), _T("15"), -1,
RGB(192, 192, 192), _T("16"), -1,
// custom colors
// row 11
(unsigned long)-1, _T("Custom color 1"), -1,
(unsigned long)-1, _T("Custom color 2"), -1,
(unsigned long)-1, _T("Custom color 3"), -1,
(unsigned long)-1, _T("Custom color 4"), -1,
(unsigned long)-1, _T("Custom color 5"), -1,
(unsigned long)-1, _T("Custom color 6"), -1,
(unsigned long)-1, _T("Custom color 7"), -1,
(unsigned long)-1, _T("Custom color 8"), -1,
// row 12
(unsigned long)-1, _T("Custom color 9"), -1,
(unsigned long)-1, _T("Custom color 10"), -1,
(unsigned long)-1, _T("Custom color 11"), -1,
(unsigned long)-1, _T("Custom color 12"), -1,
(unsigned long)-1, _T("Custom color 13"), -1,
(unsigned long)-1, _T("Custom color 14"), -1,
(unsigned long)-1, _T("Custom color 15"), -1,
(unsigned long)-1, _T("Custom color 16"), -1
};
/////////////////////////////////////////////////////////////////////////////
// CXColourPopup
BEGIN_MESSAGE_MAP(CXColourPopup, CWnd)
//{{AFX_MSG_MAP(CXColourPopup)
ON_WM_NCDESTROY()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CXColourPopup::CXColourPopup()
{
Init();
}
CXColourPopup::CXColourPopup(CWnd * pParentWnd,
CPoint p,
COLORREF crColor,
COLORREF * pCustomColors,
int nSelectedRow,
int nSelectedCol,
LPCTSTR szCustomText /* = NULL */)
{
Init();
m_nSelectedRow = nSelectedRow;
m_nSelectedCol = nSelectedCol;
m_crColor = m_crInitialColor = crColor;
m_pParent = pParentWnd;
int i;
if (nSelectedRow == -1 || nSelectedCol == -1)
{
// no selection specified - look thru the color table for a match
for (i = 0; i < m_nNumColors; i++)
{
if (m_crColors[i].crColor == crColor)
{
m_nSelectedRow = i / 8;
m_nSelectedCol = i % 8;
break;
}
}
}
if (pCustomColors)
{
m_pCustomColors = pCustomColors;
for (i = 0; i < 16; i++)
{
m_crColors[FIRST_CUSTOM_ROW * 8 + i].crColor = m_pCustomColors[i];
}
}
Create(p, crColor, pParentWnd, szCustomText);
}
void CXColourPopup::Init()
{
m_nNumColors = sizeof(m_crColors) / sizeof(ColorTableEntry);
ASSERT(m_nNumColors == 104);
m_nNumColumns = 0;
m_nNumRows = 0;
m_nBoxSize = 18;
m_nMargin = ::GetSystemMetrics(SM_CXEDGE);
m_nCurrentRow = -1;
m_nCurrentCol = -1;
m_nSelectedRow = -1;
m_nSelectedCol = -1;
m_crColor = m_crInitialColor = RGB(255, 255, 255);
m_pCustomColors = NULL;
m_bShowCustom = TRUE;
m_strCustomText = _T("Custom...");
m_pParent = NULL;
// Make sure the color square is at least 5 x 5;
if (m_nBoxSize - 2 * m_nMargin - 2 < 5)
m_nBoxSize = 5 + 2 * m_nMargin + 2;
// set system colors
for (int i = 0; i < m_nNumColors; i++)
if (m_crColors[i].nSystemColor != -1)
m_crColors[i].crColor = GetSysColor(m_crColors[i].nSystemColor);
}
CXColourPopup::~CXColourPopup()
{
if (m_Font.GetSafeHandle())
m_Font.DeleteObject();
}
///////////////////////////////////////////////////////////////////////////////
// Create
BOOL CXColourPopup::Create(CPoint p,
COLORREF crColor,
CWnd * pParentWnd,
LPCTSTR szCustomText /* = NULL */)
{
ASSERT(pParentWnd && ::IsWindow(pParentWnd->GetSafeHwnd()));
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;
// Set the window size
SetWindowSize();
// Create the tooltips
CreateToolTips();
// Capture all mouse events for the life of this window
SetCapture();
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// PreTranslateMessage - for tooltips
BOOL CXColourPopup::PreTranslateMessage(MSG * pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CWnd::PreTranslateMessage(pMsg);
}
///////////////////////////////////////////////////////////////////////////////
// OnKeyDown - if an arrow key is pressed, then move the selection
void CXColourPopup::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int row = m_nCurrentRow;
int col = m_nCurrentCol;
if (row == -1 || col == -1)
{
row = m_nSelectedRow;
col = m_nSelectedCol;
}
if (nChar == VK_DOWN)
{
row++;
if (row < 0)
{
row = 0;
col = 0;
}
else if (row == DIVIDER_LINE_ROW)
{
row++;
}
else if (row >= m_nNumRows)
{
row = 0;
}
ChangeSelection(row, col);
}
if (nChar == VK_UP)
{
row--;
if (row < 0)
{
row = m_nNumRows - 1;
}
else if (row == DIVIDER_LINE_ROW)
{
row--;
}
ChangeSelection(row, col);
}
if (nChar == VK_RIGHT)
{
if (col < 0)
{
row = 0;
col = 0;
}
else if (col < m_nNumColumns - 1)
{
col++;
}
else
{
col = 0;
}
ChangeSelection(row, col);
}
if (nChar == VK_LEFT)
{
if (col < 0)
{
row = m_nNumRows - 1;
col = m_nNumColumns - 1;
}
else if (col > 0)
{
col--;
}
else
{
col = m_nNumColumns - 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -