📄 cjcolorpopup.cpp
字号:
void CCJColorPopup::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int row = GetRow(m_nCurrentSel),
col = GetColumn(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 /* row == 0 */
{
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_Color)
{
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; }
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_ESCAPE)
{
m_crColor = m_crInitialColor;
EndSelection(CPN_SELENDCANCEL);
return;
}
if (nChar == VK_RETURN || nChar == VK_SPACE)
{
EndSelection(CPN_SELENDOK);
return;
}
CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}
// auto-deletion
void CCJColorPopup::OnNcDestroy()
{
CWnd::OnNcDestroy();
delete this;
}
void CCJColorPopup::OnPaint()
{
CPaintDC dc(this); // device context for painting
// KStowell - Get the client rect for this control.
CRect rcClient;
GetClientRect(&rcClient);
// KStowell - Create a memory device-context. This is done to help reduce
// screen flicker, since we will paint the entire control to the
// off screen device context first.
CDC memDC;
memDC.CreateCompatibleDC(&dc);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(&dc, rcClient.Width(), rcClient.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
memDC.FillSolidRect(rcClient, ::GetSysColor(COLOR_BTNFACE));
// Draw the Default Area text
if (m_strDefaultText.GetLength())
DrawCell(&memDC, DEFAULT_BOX_VALUE);
// Draw Color cells
for (int i = 0; i < m_nNumColors; i++)
DrawCell(&memDC, i);
// Draw custom text
if (m_strCustomText.GetLength())
DrawCell(&memDC, CUSTOM_BOX_VALUE);
// Draw raised window edge (ex-window style WS_EX_WINDOWEDGE is sposed to do this,
// but for some reason isn't
CRect rect;
GetClientRect(rect);
memDC.DrawEdge(rect, EDGE_RAISED, BF_RECT);
// KStowell - copy the memory device context back into the original DC via BitBlt().
dc.BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &memDC, 0,0, SRCCOPY);
// KStowell - cleanup.
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
bitmap.DeleteObject();
}
void CCJColorPopup::OnMouseMove(UINT nFlags, CPoint point)
{
int nNewSelection = INVALID_Color;
// Translate points to be relative raised window edge
point.x -= m_nMargin;
point.y -= m_nMargin;
// First check we aren't in text box
if (m_strCustomText.GetLength() && m_CustomTextRect.PtInRect(point))
nNewSelection = CUSTOM_BOX_VALUE;
else if (m_strDefaultText.GetLength() && m_DefaultTextRect.PtInRect(point))
nNewSelection = DEFAULT_BOX_VALUE;
else
{
// Take into account text box
if (m_strDefaultText.GetLength())
point.y -= m_DefaultTextRect.Height();
// Get the row and column
nNewSelection = GetIndex(point.y / m_nBoxSize, point.x / m_nBoxSize);
// In range? If not, default and exit
if (nNewSelection < 0 || nNewSelection >= m_nNumColors)
{
CWnd::OnMouseMove(nFlags, point);
return;
}
}
// OK - we have the row and column of the current selection (may be CUSTOM_BOX_VALUE)
// 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();
POINTS pt = MAKEPOINTS(pos);
point = CPoint(pt.x, pt.y);
//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()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -