📄 cjcolorpopup.cpp
字号:
// Has the row/col selection changed? If yes, then redraw old and new cells.
if (nNewSelection != m_nCurrentSel)
ChangeSelection(nNewSelection);
CWnd::OnMouseMove(nFlags, point);
}
// End selection on LButtonUp
void CCJColorPopup::OnLButtonUp(UINT nFlags, CPoint point)
{
CWnd::OnLButtonUp(nFlags, point);
DWORD pos = GetMessagePos();
point = CPoint(LOWORD(pos), HIWORD(pos));
if (m_WindowRect.PtInRect(point))
EndSelection(CPN_SELENDOK);
else
EndSelection(CPN_SELENDCANCEL);
}
/////////////////////////////////////////////////////////////////////////////
// CCJColorPopup implementation
int CCJColorPopup::GetIndex(int row, int col) const
{
if ((row == CUSTOM_BOX_VALUE || col == CUSTOM_BOX_VALUE) && m_strCustomText.GetLength())
return CUSTOM_BOX_VALUE;
else if ((row == DEFAULT_BOX_VALUE || col == DEFAULT_BOX_VALUE) && m_strDefaultText.GetLength())
return DEFAULT_BOX_VALUE;
else if (row < 0 || col < 0 || row >= m_nNumRows || col >= m_nNumColumns)
return INVALID_Color;
else
{
if (row*m_nNumColumns + col >= m_nNumColors)
return INVALID_Color;
else
return row*m_nNumColumns + col;
}
}
int CCJColorPopup::GetRow(int nIndex) const
{
if (nIndex == CUSTOM_BOX_VALUE && m_strCustomText.GetLength())
return CUSTOM_BOX_VALUE;
else if (nIndex == DEFAULT_BOX_VALUE && m_strDefaultText.GetLength())
return DEFAULT_BOX_VALUE;
else if (nIndex < 0 || nIndex >= m_nNumColors)
return INVALID_Color;
else
return nIndex / m_nNumColumns;
}
int CCJColorPopup::GetColumn(int nIndex) const
{
if (nIndex == CUSTOM_BOX_VALUE && m_strCustomText.GetLength())
return CUSTOM_BOX_VALUE;
else if (nIndex == DEFAULT_BOX_VALUE && m_strDefaultText.GetLength())
return DEFAULT_BOX_VALUE;
else if (nIndex < 0 || nIndex >= m_nNumColors)
return INVALID_Color;
else
return nIndex % m_nNumColumns;
}
void CCJColorPopup::FindCellFromColor(COLORREF crColor)
{
if (crColor == CLR_DEFAULT && m_strDefaultText.GetLength())
{
m_nChosenColorSel = DEFAULT_BOX_VALUE;
return;
}
for (int i = 0; i < m_nNumColors; i++)
{
if (GetColor(i) == crColor)
{
m_nChosenColorSel = i;
return;
}
}
if (m_strCustomText.GetLength())
m_nChosenColorSel = CUSTOM_BOX_VALUE;
else
m_nChosenColorSel = INVALID_Color;
}
// Gets the dimensions of the Color cell given by (row,col)
BOOL CCJColorPopup::GetCellRect(int nIndex, const LPRECT& rect)
{
if (nIndex == CUSTOM_BOX_VALUE)
{
::SetRect(rect,
m_CustomTextRect.left, m_CustomTextRect.top,
m_CustomTextRect.right, m_CustomTextRect.bottom);
return TRUE;
}
else if (nIndex == DEFAULT_BOX_VALUE)
{
::SetRect(rect,
m_DefaultTextRect.left, m_DefaultTextRect.top,
m_DefaultTextRect.right, m_DefaultTextRect.bottom);
return TRUE;
}
if (nIndex < 0 || nIndex >= m_nNumColors)
return FALSE;
rect->left = GetColumn(nIndex) * m_nBoxSize + m_nMargin;
rect->top = GetRow(nIndex) * m_nBoxSize + m_nMargin;
// Move everything down if we are displaying a default text area
if (m_strDefaultText.GetLength())
rect->top += (m_nMargin + m_DefaultTextRect.Height());
rect->right = rect->left + m_nBoxSize;
rect->bottom = rect->top + m_nBoxSize;
return TRUE;
}
// Works out an appropriate size and position of this window
void CCJColorPopup::SetWindowSize()
{
CSize TextSize;
// If we are showing a custom or default text area, get the font and text size.
if (m_strCustomText.GetLength() || m_strDefaultText.GetLength())
{
CClientDC dc(this);
CFont* pOldFont = (CFont*) dc.SelectObject(&m_Font);
// Get the size of the custom text (if there IS custom text)
TextSize = CSize(0,0);
if (m_strCustomText.GetLength())
TextSize = dc.GetTextExtent(m_strCustomText);
// Get the size of the default text (if there IS default text)
if (m_strDefaultText.GetLength())
{
CSize DefaultSize = dc.GetTextExtent(m_strDefaultText);
if (DefaultSize.cx > TextSize.cx) TextSize.cx = DefaultSize.cx;
if (DefaultSize.cy > TextSize.cy) TextSize.cy = DefaultSize.cy;
}
dc.SelectObject(pOldFont);
TextSize += CSize(2*m_nMargin,2*m_nMargin);
// Add even more space to draw the horizontal line
TextSize.cy += 2*m_nMargin + 2;
}
// Get the number of columns and rows
//m_nNumColumns = (int) sqrt((double)m_nNumColors); // for a square window (yuk)
m_nNumColumns = NUM_COLUMNS;
m_nNumRows = m_nNumColors / m_nNumColumns;
if (m_nNumColors % m_nNumColumns) m_nNumRows++;
// Get the current window position, and set the new size
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 custom text, then expand window if necessary, and set text width as
// window width
if (m_strDefaultText.GetLength())
{
if (TextSize.cx > m_WindowRect.Width())
m_WindowRect.right = m_WindowRect.left + TextSize.cx;
TextSize.cx = m_WindowRect.Width()-2*m_nMargin;
// Work out the text area
m_DefaultTextRect.SetRect(m_nMargin, m_nMargin,
m_nMargin+TextSize.cx, 2*m_nMargin+TextSize.cy);
m_WindowRect.bottom += m_DefaultTextRect.Height() + 2*m_nMargin;
}
// if custom text, then expand window if necessary, and set text width as
// window width
if (m_strCustomText.GetLength())
{
if (TextSize.cx > m_WindowRect.Width())
m_WindowRect.right = m_WindowRect.left + TextSize.cx;
TextSize.cx = m_WindowRect.Width()-2*m_nMargin;
// Work out the text area
m_CustomTextRect.SetRect(m_nMargin, m_WindowRect.Height(),
m_nMargin+TextSize.cx,
m_WindowRect.Height()+m_nMargin+TextSize.cy);
m_WindowRect.bottom += m_CustomTextRect.Height() + 2*m_nMargin;
}
// Need to check it'll fit on screen: Too far right?
CSize ScreenSize(::GetSystemMetrics(SM_CXSCREEN), ::GetSystemMetrics(SM_CYSCREEN));
if (m_WindowRect.right > ScreenSize.cx)
m_WindowRect.OffsetRect(-(m_WindowRect.right - ScreenSize.cx), 0);
// Too far left?
if (m_WindowRect.left < 0)
m_WindowRect.OffsetRect( -m_WindowRect.left, 0);
// Bottom falling out of screen?
if (m_WindowRect.bottom > ScreenSize.cy)
{
CRect ParentRect;
m_pParent->GetWindowRect(ParentRect);
m_WindowRect.OffsetRect(0, -(ParentRect.Height() + m_WindowRect.Height()));
}
// Set the window size and position
MoveWindow(m_WindowRect, TRUE);
}
void CCJColorPopup::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 CCJColorPopup::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_Color;
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 Color
if (m_nCurrentSel == CUSTOM_BOX_VALUE)
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crInitialColor, 0);
else if (m_nCurrentSel == DEFAULT_BOX_VALUE)
{
m_crColor = CLR_DEFAULT;
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) CLR_DEFAULT, 0);
}
else
{
m_crColor = GetColor(m_nCurrentSel);
m_pParent->SendMessage(CPN_SELCHANGE, (WPARAM) m_crColor, 0);
}
}
void CCJColorPopup::EndSelection(int nMessage)
{
ReleaseCapture();
// If custom text selected, perform a custom Color selection
if (nMessage != CPN_SELENDCANCEL && m_nCurrentSel == CUSTOM_BOX_VALUE)
{
CColorDialog dlg(m_crInitialColor, CC_FULLOPEN | CC_ANYCOLOR, this);
if (dlg.DoModal() == IDOK)
m_crColor = dlg.GetColor();
else
nMessage = CPN_SELENDCANCEL;
}
if (nMessage == CPN_SELENDCANCEL)
m_crColor = m_crInitialColor;
m_pParent->SendMessage(nMessage, (WPARAM) m_crColor, 0);
DestroyWindow();
}
void CCJColorPopup::DrawCell(CDC* pDC, int nIndex)
{
// 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;
// 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));
// Draw button
if (m_nCurrentSel == nIndex)
pDC->DrawEdge(TextButtonRect, BDR_RAISEDINNER, BF_RECT);
else if (m_nChosenColorSel == nIndex)
pDC->DrawEdge(TextButtonRect, BDR_SUNKENOUTER, BF_RECT);
// 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 (m_strDefaultText.GetLength() && 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);
// fill background
if (m_nChosenColorSel == nIndex && m_nCurrentSel != nIndex)
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DLIGHT));
else
pDC->FillSolidRect(TextButtonRect, ::GetSysColor(COLOR_3DFACE));
// Draw thin 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)
pDC->DrawEdge(TextButtonRect, BDR_RAISEDINNER, BF_RECT);
else if (m_nChosenColorSel == nIndex)
pDC->DrawEdge(TextButtonRect, BDR_SUNKENOUTER, BF_RECT);
// 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;
}
CRect rect;
if (!GetCellRect(nIndex, rect)) return;
// Select and realize the palette
CPalette* pOldPalette = NULL;
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, ::GetSysColor(COLOR_3DHILIGHT));
else
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
// Draw button
if (m_nCurrentSel == nIndex)
pDC->DrawEdge(rect, BDR_RAISEDINNER, BF_RECT);
else if (m_nChosenColorSel == nIndex)
pDC->DrawEdge(rect, BDR_SUNKENOUTER, BF_RECT);
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 Color
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 (pOldPalette && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
pDC->SelectPalette(pOldPalette, FALSE);
}
BOOL CCJColorPopup::OnQueryNewPalette()
{
Invalidate();
return CWnd::OnQueryNewPalette();
}
void CCJColorPopup::OnPaletteChanged(CWnd* pFocusWnd)
{
CWnd::OnPaletteChanged(pFocusWnd);
if (pFocusWnd->GetSafeHwnd() != GetSafeHwnd())
Invalidate();
}
void CCJColorPopup::OnKillFocus(CWnd* pNewWnd)
{
CWnd::OnKillFocus(pNewWnd);
ReleaseCapture();
//DestroyWindow(); - causes crash when Custom Color dialog appears.
}
// KillFocus problem fix suggested by Paul Wilkerson.
void CCJColorPopup::OnActivateApp(BOOL bActive, HTASK hTask)
{
CWnd::OnActivateApp(bActive, hTask);
// If Deactivating App, cancel this selection
if (!bActive)
EndSelection(CPN_SELENDCANCEL);
}
void CCJColorPopup::UpdateFont()
{
m_Font.DeleteObject();
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
VERIFY(m_Font.CreateFontIndirect(&ncm.lfMessageFont));
SetFont(&m_Font);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -