📄 colorpopup.cpp
字号:
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 CColorPopup::OnNcDestroy()
{
CWnd::OnNcDestroy();
delete this;
}
/////////////////////////////////////////////////////////////////////
//Paint the Dialog's background
BOOL CColorPopup::OnEraseBkgnd(CDC* pDC)
{
CBrush br(GetSysColor(COLOR_3DFACE));
CRect rc;
pDC->GetClipBox(rc);
CBrush* pOldBrush = pDC->SelectObject(&br);
pDC->PatBlt(rc.left,rc.top,rc.Width(),rc.Height(),PATCOPY);
pDC->SelectObject(pOldBrush);
return TRUE;
}
/////////////////////////////////////////////////////////////////////
void CColorPopup::OnPaint()
{
// device context for painting
CPaintDC dc(this);
// Draw the title with my name
CRect rectName(4, 1, 154, 17);
dc.SetBkMode(TRANSPARENT);
DrawName(&dc,rectName);
// Draw the Default Area text
DrawCell(&dc, DEFAULT_BOX_VALUE);
// Draw colour cells
for (int i = 0; i < m_nNumColors; i++)
DrawCell(&dc, i);
// Draw custom text
DrawCell(&dc, CUSTOM_BOX_VALUE);
// Draw raised window edge
CRect rect;
GetClientRect(rect);
dc.DrawEdge(rect, EDGE_RAISED, BF_RECT);
}
/////////////////////////////////////////////////////////////////////
void CColorPopup::OnMouseMove(UINT nFlags, CPoint point)
{
int nNewSelection = INVALID_COLOUR;
// Translate points to be relative raised window edge
point.x -= m_nMargin;
point.y -= m_nMargin;
// First check whether in text box
if (m_CustomTextRect.PtInRect(point))
nNewSelection = CUSTOM_BOX_VALUE;
else if (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 - MAX_BOTTOM) / m_nBoxSize,
(point.x - MAX_OFFSET) / m_nBoxSize);
// In range? If not, default and exit
if (nNewSelection < 0 || nNewSelection >= m_nNumColors)
{
CWnd::OnMouseMove(nFlags, point);
return;
}
}
// the row and column of the current selection have been found there,
// and if the row/col selection has changed? then redraw old and new cells.
if (nNewSelection != m_nCurrentSel)
ChangeSelection(nNewSelection);
CWnd::OnMouseMove(nFlags, point);
}
///////////////////////////////////////////////////////////////////
// End selection on LButtonUp
void CColorPopup::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);
}
/////////////////////////////////////////////////////////////////////////////
// Get the index of clolor cell
int CColorPopup::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_COLOUR;
else
{
if (row*m_nNumColumns + col >= m_nNumColors)
return INVALID_COLOUR;
else
return row*m_nNumColumns + col;
}
}
////////////////////////////////////////////////////////////////////////////
// Get the Row number
int CColorPopup::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_COLOUR;
else
return nIndex / m_nNumColumns;
}
/////////////////////////////////////////////////////////////////
// Get the Column number
int CColorPopup::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_COLOUR;
else
return nIndex % m_nNumColumns;
}
///////////////////////////////////////////////////////////////////
//
void CColorPopup::FindCellFromColor(COLORREF crColor)
{
if (crColor == m_crDefaultColor && 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_COLOUR;
}
///////////////////////////////////////////////////////////////////
// Gets the dimensions of the colour cell given by (row,col)
BOOL CColorPopup::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 = MAX_OFFSET + GetColumn(nIndex) * m_nBoxSize + m_nMargin;
rect->top = MAX_BOTTOM + 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 CColorPopup::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
TextSize = CSize(0,0);
if (m_strCustomText.GetLength())
TextSize = dc.GetTextExtent(m_strCustomText);
// Get the size of the 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 = 8;
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+ 2* MAX_OFFSET,
rect.top + m_nNumRows*m_nBoxSize + 2*m_nMargin + MAX_BOTTOM );
// 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-20;
// Work out the text area
m_DefaultTextRect.SetRect(m_nMargin, m_nMargin,
m_nMargin+TextSize.cx+2 , 2*m_nMargin+TextSize.cy);
m_DefaultTextRect.OffsetRect ( 2 * MAX_OFFSET,MAX_BOTTOM);
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-20;
// 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_CustomTextRect.OffsetRect ( 2 * MAX_OFFSET , 0 );
m_WindowRect.bottom += m_CustomTextRect.Height() + 2*m_nMargin + MAX_OFFSET;
}
// Need to check it'll fit on screen
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);
}
////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -