📄 colourpopup.cpp
字号:
// ColourPopup.cpp : implementation file
//
#include "stdafx.h"
#include "CProgressCtrl.h"
#include "ColourPopup.h"
//#include "ColourPicker.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TEXT_BOX_VALUE -2 //定义用户文本显示的按钮坐标
#define MAX_COLOURS 100 //定义颜色板中最多的颜色数
/////////////////////////////////////////////////////////////////////////////
// CColourPopup
ColourTableEntry CColourPopup::m_crColours[]=
{
{RGB(0x00,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(0x28,0x28,0x28), _T("深灰色") },
{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(0x66,0x66,0x66), _T("灰色") },
{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(0x7F,0x7F,0x7F), _T("灰色") },
{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(0xC0,0xC0,0xC0), _T("淡灰色") },
{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("白色") },
};
CColourPopup::CColourPopup()
{
Initialize();
}
CColourPopup::~CColourPopup()
{
m_Font.DeleteObject();
m_Palette.DeleteObject();
}
BEGIN_MESSAGE_MAP(CColourPopup, CWnd)
//{{AFX_MSG_MAP(CColourPopup)
ON_WM_PAINT()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_KEYDOWN()
ON_WM_PALETTECHANGED()
ON_WM_QUERYNEWPALETTE()
ON_WM_NCDESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CColourPopup message handlers
void CColourPopup::Initialize()
{
m_nNumColours =sizeof(m_crColours)/sizeof(ColourTableEntry);
ASSERT(m_nNumColours<=MAX_COLOURS);
if(m_nNumColours>MAX_COLOURS)
m_nNumColours=MAX_COLOURS;
m_nNumColumns=0;
m_nNumRows=0;
m_nBoxSize=18;
m_nMargin=::GetSystemMetrics(SM_CXEDGE);
m_nCurrentRow=-1;
m_nCurrentCol=-1;
m_nChosenColourRow=-1;
m_nChosenColourCol=-1;;
m_bShowCustom=TRUE;
m_strCustomText=_T("其它颜色");
m_crInitialColour=m_crColour=RGB(0,0,0);
m_pParent=NULL;
if(m_nBoxSize-2*m_nMargin-2<5) m_nBoxSize=5+2*m_nMargin+2;
//在颜色板中使用非客户区的字体
//以下代码创建NONCLIENTMETRICS结构,
NONCLIENTMETRICS ncm;
ncm.cbSize=sizeof(NONCLIENTMETRICS);
VERIFY(SystemParametersInfo(SPI_GETNONCLIENTMETRICS,sizeof(NONCLIENTMETRICS),&ncm,0));
m_Font.CreateFontIndirect(&(ncm.lfMessageFont));
//创建 应用颜色板结构
struct
{
//逻辑颜色板
LOGPALETTE Logpalette;
PALETTEENTRY PalEntry[MAX_COLOURS];
}pal;
//颜色板的入口数为定义的颜色数
LOGPALETTE* pLogPalette=(LOGPALETTE*)&pal;
pLogPalette->palNumEntries=m_nNumColours;
for(int i=0;i<m_nNumColours;i++)
{
pLogPalette->palPalEntry[i].peRed=GetRValue(m_crColours[i].crColour);
pLogPalette->palPalEntry[i].peGreen=GetGValue(m_crColours[i].crColour);
pLogPalette->palPalEntry[i].peBlue=GetBValue(m_crColours[i].crColour);
pLogPalette->palPalEntry[i].peFlags=0;
}
//
//以创建的逻辑板为模板来生成MFC的CPalette类
m_Palette.CreatePalette(pLogPalette);
}
CColourPopup::CColourPopup(CPoint p, COLORREF crColour, CWnd *pParentWnd, UINT nID, LPCTSTR szCustomText/*=NULL*/)
{
Initialize();
m_crColour=m_crInitialColour=crColour;
if(szCustomText!=NULL)
{
m_bShowCustom=TRUE;
m_strCustomText=szCustomText;
}
else
m_bShowCustom=FALSE;
m_pParent=pParentWnd;
CColourPopup::Create(p,crColour,pParentWnd,nID,szCustomText);
}
BOOL CColourPopup::Create(CPoint p, COLORREF crColour, CWnd *pParentWnd, UINT nID, LPCTSTR szCustomText)
{
ASSERT(pParentWnd&&::IsWindow(pParentWnd->GetSafeHwnd()));
// ASSERT(pParentWnd->IsKindOf(RUNTIME_CLASS(CColourPicker)));
m_pParent=pParentWnd;
m_crColour=m_crInitialColour=crColour;
//创建 窗体并注册类名
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,
pParentWnd->GetSafeHwnd(),0,NULL))
return FALSE;
if(szCustomText!=NULL)
m_strCustomText=szCustomText;
//设定窗体的尺寸
SetWindowSize();
//创建ToolTips显示的文字
CreateToolTips();
FindCellFromColour(crColour);
SetCapture();
return TRUE;
}
void CColourPopup::SetWindowSize()
{
CSize TextSize;
//若显示用户文本区,则定义字体和文本尺寸
if(m_bShowCustom)
{
//得到用户文本尺寸
CClientDC dc(this);
CFont* pOldFont=(CFont*)dc.SelectObject(&m_Font);
TextSize=dc.GetTextExtent(m_strCustomText)+CSize(2*m_nMargin,2*m_nMargin);
dc.SelectObject(pOldFont);
//添加足够的空间来绘制水平线
TextSize.cy+=2*m_nMargin+2;
}
m_nNumColumns=8;
m_nNumRows=m_nNumColours/m_nNumColumns;
if(m_nNumColours%m_nNumColumns) m_nNumRows++;
//得到当前窗体位置,并设定新的尺寸
CRect rect;
GetWindowRect(rect);
m_WindowRect.SetRect(rect.left,rect.top,rect.left+m_nNumColumns*m_nBoxSize+2*m_nMargin,
rect.top+m_nNumRows*m_nBoxSize+2*m_nMargin);
if(m_bShowCustom)
{
m_WindowRect.bottom+=(m_nMargin+TextSize.cy);
if(TextSize.cx>m_WindowRect.Width())
m_WindowRect.right=m_WindowRect.left+TextSize.cx;
TextSize.cx=m_WindowRect.Width()-2*m_nMargin;
m_TextRect.SetRect(m_nMargin,m_nMargin,
m_nMargin+TextSize.cx,m_nMargin+TextSize.cy);
}
//检验是否超出了屏幕的显示范围
CSize ScreenSize(::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN));
//太靠右
if(m_WindowRect.right>ScreenSize.cx)
m_WindowRect.OffsetRect(-(m_WindowRect.right-ScreenSize.cx),0);
//太靠左
if(m_WindowRect.left<0)
m_WindowRect.OffsetRect(-m_WindowRect.left,0);
//底超出了屏幕
if(m_WindowRect.bottom>ScreenSize.cy)
{
CRect ParentRect;
m_pParent->GetWindowRect(ParentRect);
m_WindowRect.OffsetRect(0,-(ParentRect.Height()+m_WindowRect.Height()));
}
MoveWindow(m_WindowRect,TRUE);
}
void CColourPopup::FindCellFromColour(COLORREF crColour)
{
for(int row=0;row<m_nNumRows;row++)
for(int col=0;col<m_nNumColumns;col++)
{
if(GetColour(row,col)==crColour)
{
m_nChosenColourRow=row;
m_nChosenColourCol=col;
return;
}
}
m_nChosenColourRow=TEXT_BOX_VALUE;
m_nChosenColourCol=TEXT_BOX_VALUE;
}
void CColourPopup::CreateToolTips()
{
if(!m_ToolTip.Create(this)) return;
for(int row=0;row<m_nNumRows;row++)
for(int col=0;col<m_nNumColumns;col++)
{
CRect rect;
if(!GetCellRect(row,col,rect)) continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -