📄 cgridctrl.cpp
字号:
y1=j;
x2=x1+pCell->m_nHhide;
y2=y1+pCell->m_nVhide;
nMinRow1=min(x1,nMinRow1);
nMinCol1=min(y1,nMinCol1);
nMaxRow1=max(x2,nMaxRow1);
nMaxCol1=max(y2,nMaxCol1);
}
else if(pCell->m_bHide) //合并区域的隐藏单元格
{
x1=pCell->m_TopLeftCellId.row;
y1=pCell->m_TopLeftCellId.col;
pCell=GetCell(pCell->m_TopLeftCellId.row,pCell->m_TopLeftCellId.col);
x2=x1+pCell->m_nHhide;
y2=y1+pCell->m_nVhide;
nMinRow1=min(x1,nMinRow1);
nMinCol1=min(y1,nMinCol1);
nMaxRow1=max(x2,nMaxRow1);
nMaxCol1=max(y2,nMaxCol1);
}
}
}
SetSelectedRange(nMinRow1,nMinCol1,nMaxRow1,nMaxCol1);
}
void CGridCtrl::OnSelecting(const CCellID& currentCell)
{
if (!m_bEnableSelection)
{
return;
}
switch(m_MouseMode)
{
case MOUSE_SELECT_ALL:
SelectAllCells();
break;
case MOUSE_SELECT_COL:
SelectColumns(currentCell);
break;
case MOUSE_SELECT_ROW:
SelectRows(currentCell);
break;
case MOUSE_SELECT_CELLS:
SelectCells(currentCell);
break;
}
}
CCellID CGridCtrl::GetCellFromPt(CPoint point, BOOL bAllowFixedCellCheck /*=TRUE*/) const
{
CCellID idTopLeft = GetTopleftNonFixedCell();
CCellID cellID;
int fixedColWidth = GetFixedColumnWidth();
if (point.x < 0 || (!bAllowFixedCellCheck && point.x < fixedColWidth))
{
cellID.col = -1;
}
else if (point.x < fixedColWidth)
{
int xpos = 0;
for (int col = 0; col < m_nFixedCols; col++)
{
xpos += GetColumnWidth(col);
if (xpos > point.x)
{
break;
}
}
cellID.col = col;
}
else
{
int xpos = fixedColWidth;
for (int col = idTopLeft.col; col < GetColumnCount(); col++)
{
xpos += GetColumnWidth(col);
if (xpos > point.x)
{
break;
}
}
if (col >= GetColumnCount())
{
cellID.col = -1;
}
else
{
cellID.col = col;
}
}
int fixedRowHeight = GetFixedRowHeight();
if (point.y < 0 || (!bAllowFixedCellCheck && point.y < fixedRowHeight))
{
cellID.row = -1;
}
else if (point.y < fixedRowHeight)
{
int ypos = 0;
for (int row = 0; row < m_nFixedRows; row++)
{
ypos += GetRowHeight(row);
if (ypos > point.y)
{
break;
}
}
cellID.row = row;
}
else
{
int ypos = fixedRowHeight;
for (int row = idTopLeft.row; row < GetRowCount(); row++)
{
ypos += GetRowHeight(row);
if (ypos > point.y)
{
break;
}
}
if (row >= GetRowCount())
{
cellID.row = -1;
}
else
{
cellID.row = row;
}
}
return cellID;
}
////////////////////////////////////////////////////////////////////////////////
// CGridCtrl cellrange functions
CCellID CGridCtrl::GetTopleftNonFixedCell() const
{
int nVertScroll = GetScrollPos(SB_VERT);
int nHorzScroll = GetScrollPos(SB_HORZ);
int nColumn = m_nFixedCols, nRight = 0;
while (nRight < nHorzScroll && nColumn < (GetColumnCount()-1))
{
nRight += GetColumnWidth(nColumn++);
}
int nRow = m_nFixedRows, nTop = 0;
while (nTop < nVertScroll && nRow < (GetRowCount()-1))
{
nTop += GetRowHeight(nRow++);
}
//TRACE("TopLeft cell is row %d, col %d\n",nRow, nColumn);
return CCellID(nRow, nColumn);
}
CCellRange CGridCtrl::GetVisibleNonFixedCellRange(LPRECT pRect /*=NULL*/) const
{
CRect rect;
GetClientRect(rect);
CCellID idTopLeft = GetTopleftNonFixedCell();
// calc bottom
int bottom = GetFixedRowHeight();
for (int i = idTopLeft.row; i < GetRowCount(); i++)
{
bottom += GetRowHeight(i);
if (bottom >= rect.bottom)
{
bottom = rect.bottom;
break;
}
}
int maxVisibleRow = min(i, GetRowCount() - 1);
// calc right
int right = GetFixedColumnWidth();
for (i = idTopLeft.col; i < GetColumnCount(); i++)
{
right += GetColumnWidth(i);
if (right >= rect.right)
{
right = rect.right;
break;
}
}
int maxVisibleCol = min(i, GetColumnCount() - 1);
if (pRect)
{
pRect->left = pRect->top = 0;
pRect->right = right;
pRect->bottom = bottom;
}
return CCellRange(idTopLeft.row, idTopLeft.col, maxVisibleRow, maxVisibleCol);
}
// used by ResetScrollBars() - This gets only fully visible cells
CCellRange CGridCtrl::GetUnobstructedNonFixedCellRange() const
{
CRect rect;
GetClientRect(rect);
CCellID idTopLeft = GetTopleftNonFixedCell();
// calc bottom
int bottom = GetFixedRowHeight();
for (int i = idTopLeft.row; i < GetRowCount(); i++)
{
bottom += GetRowHeight(i);
if (bottom >= rect.bottom)
{
break;
}
}
int maxVisibleRow = min(i, GetRowCount() - 1);
if (maxVisibleRow > 0 && bottom > rect.bottom)
{
maxVisibleRow--;
}
int right = GetFixedColumnWidth();
for (i = idTopLeft.col; i < GetColumnCount(); i++)
{
right += GetColumnWidth(i);
if (right >= rect.right)
{
break;
}
}
int maxVisibleCol = min(i, GetColumnCount() - 1);
if (maxVisibleCol > 0 && right > rect.right)
{
maxVisibleCol--;
}
return CCellRange(idTopLeft.row, idTopLeft.col, maxVisibleRow, maxVisibleCol);
}
CCellRange CGridCtrl::GetSelectedCellRange() const
{
CCellRange Selection(GetRowCount(), GetColumnCount(), -1,-1);
DWORD key;
CCellID cell;
for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL;)
{
m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
Selection.SetMinRow( min(Selection.GetMinRow(), cell.row) );
Selection.SetMinCol( min(Selection.GetMinCol(), cell.col) );
Selection.SetMaxRow( max(Selection.GetMaxRow(), cell.row) );
Selection.SetMaxCol( max(Selection.GetMaxCol(), cell.col) );
}
return Selection;
}
CCellRange CGridCtrl::GetCellRange() const
{
return CCellRange(0, 0, GetRowCount() - 1, GetColumnCount() - 1);
}
void CGridCtrl::ResetSelectedRange()
{
SetSelectedRange(-1,-1,-1,-1);
SetFocusCell(-1,-1);
}
int CGridCtrl::GetScrollPos32(int nBar, BOOL bGetTrackPos /* = FALSE */)
{
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
if (bGetTrackPos)
{
if (GetScrollInfo(nBar, &si, SIF_TRACKPOS))
{
return si.nTrackPos;
}
}
else
{
if (GetScrollInfo(nBar, &si, SIF_POS))
{
return si.nPos;
}
}
return 0;
}
BOOL CGridCtrl::SetScrollPos32(int nBar, int nPos, BOOL bRedraw /* = TRUE */)
{
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
si.nPos = nPos;
return SetScrollInfo(nBar, &si, bRedraw);
}
void CGridCtrl::ResetScrollBars()
{
if (!m_bAllowDraw || !::IsWindow(GetSafeHwnd()))
{
return;
}
CRect rect;
GetClientRect(rect);
rect.left += GetFixedColumnWidth();
rect.top += GetFixedRowHeight();
if (rect.left >= rect.right || rect.top >= rect.bottom)
{
return;
}
CRect VisibleRect(GetFixedColumnWidth(), GetFixedRowHeight(), rect.right, rect.bottom);
CRect VirtualRect(GetFixedColumnWidth(), GetFixedRowHeight(), GetVirtualWidth(), GetVirtualHeight());
CCellRange visibleCells = GetUnobstructedNonFixedCellRange();
if (!IsValid(visibleCells))
{
return;
}
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE;
si.nPage = VisibleRect.Width();
SetScrollInfo(SB_HORZ, &si, FALSE);
si.nPage = VisibleRect.Height();
SetScrollInfo(SB_VERT, &si, FALSE);
if (VisibleRect.Height() < VirtualRect.Height())
{
m_nVScrollMax = VirtualRect.Height()-1;
}
else
{
m_nVScrollMax = 0;
}
if (VisibleRect.Width() < VirtualRect.Width())
{
m_nHScrollMax = VirtualRect.Width()-1;
}
else
{
m_nHScrollMax = 0;
}
ASSERT(m_nVScrollMax < INT_MAX && m_nHScrollMax < INT_MAX);
SetScrollRange(SB_VERT, 0, m_nVScrollMax, TRUE);
SetScrollRange(SB_HORZ, 0, m_nHScrollMax, TRUE);
}
BOOL CGridCtrl::GetCellOrigin(int nRow, int nCol, LPPOINT p) const
{
int i;
if (!IsValid(nRow, nCol))
{
return FALSE;
}
CCellID idTopLeft;
if (nCol >= m_nFixedCols || nRow >= m_nFixedRows)
{
idTopLeft = GetTopleftNonFixedCell();
}
if ((nRow >= m_nFixedRows && nRow < idTopLeft.row) || (nCol>= m_nFixedCols && nCol < idTopLeft.col))
{
return FALSE;
}
p->x = 0;
if (nCol < m_nFixedCols)
{
for (i = 0; i < nCol; i++)
{
p->x += GetColumnWidth(i);
}
}
else
{
for (i = 0; i < m_nFixedCols; i++)
{
p->x += GetColumnWidth(i);
}
for (i = idTopLeft.col; i < nCol; i++)
{
p->x += GetColumnWidth(i);
}
}
p->y = 0;
if (nRow < m_nFixedRows)
{
for (i = 0; i < nRow; i++)
{
p->y += GetRowHeight(i);
}
}
else
{
for (i = 0; i < m_nFixedRows; i++)
{
p->y += GetRowHeight(i);
}
for (i = idTopLeft.row; i < nRow; i++)
{
p->y += GetRowHeight(i);
}
}
return TRUE;
}
BOOL CGridCtrl::GetCellOrigin(const CCellID& cell, LPPOINT p) const
{
return GetCellOrigin(cell.row, cell.col, p);
}
BOOL CGridCtrl::GetCellRect(const CCellID& cell, LPRECT pRect) const
{
return GetCellRect(cell.row, cell.col, pRect);
}
BOOL CGridCtrl::GetCellRect(int nRow, int nCol, LPRECT pRect) const
{
CPoint CellOrigin;
if (!GetCellOrigin(nRow, nCol, &CellOrigin))
{
return FALSE;
}
pRect->left = CellOrigin.x;
pRect->top = CellOrigin.y;
pRect->right = CellOrigin.x + GetColumnWidth(nCol)-1;
pRect->bottom = CellOrigin.y + GetRowHeight(nRow)-1;
return TRUE;
}
BOOL CGridCtrl::GetCellRangeRect(const CCellRange& cellRange, LPRECT lpRect) const
{
CPoint MinOrigin,MaxOrigin;
if (!GetCellOrigin(cellRange.GetMinRow(), cellRange.GetMinCol(), &MinOrigin))
{
return FALSE;
}
if (!GetCellOrigin(cellRange.GetMaxRow(), cellRange.GetMaxCol(), &MaxOrigin))
{
return FALSE;
}
lpRect->left = MinOrigin.x;
lpRect->top = MinOrigin.y;
lpRect->right = MaxOrigin.x + GetColumnWidth(cellRange.GetMaxCol()-1);
lpRect->bottom = MaxOrigin.y + GetRowHeight(cellRange.GetMaxRow()-1);
return TRUE;
}
LRESULT CGridCtrl::OnSetFont(WPARAM hFont, LPARAM /*lParam */)
{
LRESULT result = Default();
LOGFONT lf;
if (!GetObject((HFONT) hFont, sizeof(LOGFONT), &lf))
{
return result;
}
memcpy(&m_Logfont, &lf, sizeof(LOGFONT));
for (int row = 0; row < GetRowCount(); row++)
{
for (int col = 0; col < GetColumnCount(); col++)
{
SetItemFont(row, col, &lf);
}
}
CDC* pDC = GetDC();
if (pDC)
{
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&m_Logfont);
CFont* pOldFont = pDC->SelectObject(&m_Font);
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
m_nDefCellHeight = tm.tmHeight+tm.tmExternalLeading + 2*m_nMargin;
m_nDefCellWidth = tm.tmAveCharWidth*12 + 2*m_nMargin;
}
if(::IsWindow(GetSafeHwnd()))
{
Invalidate();
}
return result;
}
LRESULT CGridCtrl::OnGetFont(WPARAM /*wParam*/, LPARAM /*lParam*/)
{
return (LRESULT) (HFONT) m_Font;
}
BOOL CGridCtrl::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (nHitTest == HTCLIENT)
{
switch (m_MouseMode)
{
case MOUSE_OVER_COL_DIVIDE:
SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
break;
case MOUSE_OVER_ROW_DIVIDE:
SetCursor(::LoadCursor(NULL, IDC_SIZENS));
break;
case MOUSE_DRAGGING:
break;
default:
SetCursor(::LoadCursor(NULL, IDC_ARROW));
}
return TRUE;
}
return CWnd::OnSetCursor(pWnd, nHitTest, message);
}
BOOL CGridCtrl::SetFixedRowCount(int nFixedRows)
{
ASSERT(nFixedRows >= 0);
if (nFixedRows > GetRowCount())
{
if (!SetRowCount(nFixedRows))
{
return FALSE;
}
}
if (m_idCurrentCell.row < nFixedRows)
{
SetFocusCell(-1,-1);
}
m_nFixedRows = nFixedRows;
if (GetSafeHwnd() && m_bAllowDraw)
{
Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -