📄 colorpopup.cpp
字号:
void CColorPopup::CreateToolTips()
{
// Create the tool tip
if (!m_ToolTip.Create(this)) return;
// Add a tool for each cell
for (int i = 0; i < m_nNumColors; i++)
{
CRect rect;
if (!GetCellRect(i, rect)) continue;
m_ToolTip.AddTool(this, GetColorName(i), rect, 1);
}
}
//////////////////////////////////////////////////////////////////
void CColorPopup::ChangeSelection(int nIndex)
{
CClientDC dc(this); // device context for drawing
if (nIndex > m_nNumColors)
nIndex = CUSTOM_BOX_VALUE;
if ((m_nCurrentSel >= 0 && m_nCurrentSel < m_nNumColors) ||
m_nCurrentSel == CUSTOM_BOX_VALUE || m_nCurrentSel == DEFAULT_BOX_VALUE)
{
// Set Current selection as invalid and redraw old selection (this way
// the old selection will be drawn unselected)
int OldSel = m_nCurrentSel;
m_nCurrentSel = INVALID_COLOUR;
DrawCell(&dc, OldSel);
}
// Set the current selection as row/col and draw (it will be drawn selected)
m_nCurrentSel = nIndex;
DrawCell(&dc, m_nCurrentSel);
// Store the current colour
if (m_nCurrentSel == CUSTOM_BOX_VALUE)
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crInitialColor, 0);
else if (m_nCurrentSel == DEFAULT_BOX_VALUE)
{
m_crColor = m_crDefaultColor;
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crDefaultColor, 0);
}
else
{
m_crColor = GetColor(m_nCurrentSel);
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crColor, 0);
}
}
///////////////////////////////////////////////////////////////////
/// Send the end message
void CColorPopup::EndSelection(int nMessage)
{
// If custom text selected, perform a custom colour selection
if (nMessage != CPN_SELENDCANCEL && m_nCurrentSel == CUSTOM_BOX_VALUE)
{
CColorDialog dlg;
if (dlg.DoModal() == IDOK)
m_crColor = dlg.GetColor();
else
m_crColor = m_crInitialColor;
}
if (nMessage == CPN_SELENDCANCEL)
m_crColor = m_crInitialColor;
m_pParent->SendMessage(nMessage, (WPARAM) m_crColor, 0);
ReleaseCapture();
DestroyWindow();
}
////////////////////////////////////////////////////////////////
void CColorPopup::DrawCell(CDC* pDC, int nIndex)
{
CRect rect;
/////////////////////////////////////
// For the Custom Text area
if (m_strCustomText.GetLength() && nIndex == CUSTOM_BOX_VALUE)
{
// The extent of the actual text button
CRect TextButtonRect = m_CustomTextRect;
TextButtonRect.top += 2*m_nMargin;
TextButtonRect.DeflateRect(MAX_TEXTRECT,0);
// Fill background
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DFACE));
// Draw horizontal line
pDC->FillSolidRect(m_CustomTextRect.left+2*m_nMargin, m_CustomTextRect.top,
m_CustomTextRect.Width()-4*m_nMargin, 1,
::GetSysColor(COLOR_3DSHADOW));
pDC->FillSolidRect(m_CustomTextRect.left+2*m_nMargin, m_CustomTextRect.top+1,
m_CustomTextRect.Width()-4*m_nMargin, 1,
::GetSysColor(COLOR_3DHILIGHT));
TextButtonRect.DeflateRect(1,1);
// fill background
if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DLIGHT));
else
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DFACE));
// fill Selected background
if (m_nChosenColorSel == nIndex)
pDC->FillSolidRect(TextButtonRect, RGB(0xE6,0xE6,0xE6));
// Draw button
if (m_nCurrentSel == nIndex){
if (m_nChosenColorSel == nIndex){
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNSHADOW),
::GetSysColor(COLOR_BTNHILIGHT));
rect = TextButtonRect;
rect.DeflateRect (1,1);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
}
else
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNHILIGHT) ,
::GetSysColor(COLOR_BTNSHADOW));
}
else if (m_nChosenColorSel == nIndex)
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNSHADOW),
::GetSysColor(COLOR_BTNHILIGHT));
// Draw custom text
CFont *pOldFont = (CFont*) pDC->SelectObject(&m_Font);
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_strCustomText, TextButtonRect, DT_CENTER |
DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject(pOldFont);
return;
}
//////////////////////////////////////////
// For the Default Text area
if (nIndex == DEFAULT_BOX_VALUE)
{
// Fill background
pDC->FillSolidRect(m_DefaultTextRect, ::GetSysColor(COLOR_3DFACE));
// The extent of the actual text button
CRect TextButtonRect = m_DefaultTextRect;
TextButtonRect.DeflateRect(1,1);
TextButtonRect.DeflateRect(MAX_TEXTRECT,0);
// fill background
if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DLIGHT));
else
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DFACE));
// fill Selected background
if (m_nChosenColorSel == nIndex)
pDC->FillSolidRect(TextButtonRect, RGB(0xE6,0xE6,0xE6));
// Draw the line around text
CRect LineRect = TextButtonRect;
LineRect.DeflateRect(2*m_nMargin,2*m_nMargin);
CPen pen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
CPen* pOldPen = pDC->SelectObject(&pen);
pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(LineRect);
pDC->SelectObject(pOldPen);
// Draw button
if (m_nCurrentSel == nIndex){
if (m_nChosenColorSel == nIndex){
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNSHADOW),
::GetSysColor(COLOR_BTNHILIGHT));
//CRect rect;
rect = TextButtonRect;
rect.DeflateRect (1,1);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
pOldPen = pDC->SelectObject(&pen);
pDC->SelectStockObject(NULL_BRUSH);
pDC->Rectangle(LineRect);
pDC->SelectObject(pOldPen);
}
else
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNHILIGHT) ,
::GetSysColor(COLOR_BTNSHADOW));
}
else if (m_nChosenColorSel == nIndex)
pDC->Draw3dRect(TextButtonRect, ::GetSysColor(COLOR_BTNSHADOW),
::GetSysColor(COLOR_BTNHILIGHT));
// Draw the default demo color cell
CBrush brush(::GetSysColor(COLOR_BTNTEXT));
CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
CPen penDefault;
penDefault.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
CRect DefRect ;
DefRect = LineRect;
DefRect.DeflateRect(m_nMargin,m_nMargin);
CRect DefaltRect(DefRect.left+5 ,DefRect.top ,DefRect.left+15 ,DefRect.top+10);
CBrush* pOldBrushDefault = (CBrush*) pDC->SelectObject(&brush);
CPen* pOldPenDefault = (CPen*) pDC->SelectObject(&pen);
pDC->Rectangle(DefaltRect);
// Draw custom text
CFont *pOldFont = (CFont*) pDC->SelectObject(&m_Font);
pDC->SetBkMode(TRANSPARENT);
pDC->DrawText(m_strDefaultText, TextButtonRect, DT_CENTER |
DT_VCENTER | DT_SINGLELINE);
pDC->SelectObject(pOldFont);
return;
}
/////////////////////////////////////////////
//// For the color cells area
if (!GetCellRect(nIndex, rect)) return;
// Select and realize the palette
CPalette* pOldPalette;
if (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
pOldPalette = pDC->SelectPalette(&m_Palette, FALSE);
pDC->RealizePalette();
}
// fill background
if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
pDC->FillSolidRect(rect, RGB(0xDC,0xDC,0xDC));
else
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
if (m_nChosenColorSel == nIndex){
pDC->FillSolidRect(rect, RGB(0xE6,0xE6,0xE6));
pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
}
// Draw button
if (m_nCurrentSel == nIndex){
if (m_nChosenColorSel == nIndex){
pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNSHADOW), ::GetSysColor(COLOR_BTNHILIGHT));
CRect rectbtn;
rectbtn = rect;
rectbtn.DeflateRect (1,1);
pDC->FillSolidRect(rectbtn, ::GetSysColor(COLOR_3DFACE));
}
else
pDC->Draw3dRect(rect, ::GetSysColor(COLOR_BTNHILIGHT) ,::GetSysColor(COLOR_BTNSHADOW));
}
CBrush brush(PALETTERGB(GetRValue(GetColor(nIndex)),
GetGValue(GetColor(nIndex)),
GetBValue(GetColor(nIndex)) ));
CPen pen;
pen.CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));
CBrush* pOldBrush = (CBrush*) pDC->SelectObject(&brush);
CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);
// Draw the cell colour
rect.DeflateRect(m_nMargin+1, m_nMargin+1);
pDC->Rectangle(rect);
// restore DC and cleanup
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
brush.DeleteObject();
pen.DeleteObject();
if (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
pDC->SelectPalette(pOldPalette, FALSE);
}
///////////////////////////////////////////////////////////////
BOOL CColorPopup::OnQueryNewPalette()
{
Invalidate();
return CWnd::OnQueryNewPalette();
}
//////////////////////////////////////////////////////////////////
void CColorPopup::OnPaletteChanged(CWnd* pFocusWnd)
{
CWnd::OnPaletteChanged(pFocusWnd);
if (pFocusWnd->GetSafeHwnd() != GetSafeHwnd())
Invalidate();
}
///////////////////////////////////////////////////////////////////
void CColorPopup::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
//m_pParent->SendMessage(CPN_SELENDCANCEL, (WPARAM) m_crColor, 0);
//EndSelection(CPN_SELENDCANCEL);
//ReleaseCapture();
//DestroyWindow();
}
/////////////////////////////////////////////////////////////////////
// KillFocus
void CColorPopup::OnActivateApp(BOOL bActive, HTASK hTask)
{
CWnd::OnActivateApp(bActive, hTask);
// If Deactivating App, cancel this selection
if (!bActive)
EndSelection(CPN_SELENDCANCEL);
}
void CColorPopup::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//SendMessage(WM_SYSCOMMAND, 0xF012);
CWnd::OnLButtonDown(nFlags, point);
PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
CWnd::OnLButtonDown(nFlags, point);
}
///////////////////////////////////////////////
void CColorPopup::DrawName (CDC *pDC,CRect rectName)
{
pDC->DrawText ("选择颜色",rectName, DT_CENTER |
DT_VCENTER | DT_SINGLELINE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -