📄 gridctrl.cpp
字号:
RedrawCell(m_idCurrentCell);
if (idPrev.col != m_idCurrentCell.col)
for (int row = 0; row < m_nFixedRows; row++)
RedrawCell(row, m_idCurrentCell.col);
if (idPrev.row != m_idCurrentCell.row)
for (int col = 0; col < m_nFixedCols; col++)
RedrawCell(m_idCurrentCell.row, col);
SendMessageToParent(m_idCurrentCell.row, m_idCurrentCell.col, GVN_SELCHANGED,GetItemText(m_idCurrentCell.row,m_idCurrentCell.col));
}
return idPrev;
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SetSelectedRange(const CCellRange& Range,
BOOL bForceRepaint /* = FALSE */)
{
SetSelectedRange(Range.GetMinRow(), Range.GetMinCol(), Range.GetMaxRow(), Range.GetMaxCol(),
bForceRepaint);
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SetSelectedRange(int nMinRow, int nMinCol, int nMaxRow, int nMaxCol,
BOOL bForceRepaint /* = FALSE */)
{
if (!m_bEnableSelection)
return;
CDC* pDC = NULL;
if (bForceRepaint) pDC = GetDC();
for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
{
DWORD key;
CCellID cell;
m_SelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
if (IsValid(cell)) {
SetItemState(cell.row, cell.col,
GetItemState(cell.row, cell.col) & ~GVIS_SELECTED);
if (nMinRow <= cell.row && cell.row <= nMaxRow &&
nMinCol <= cell.col && cell.col <= nMaxCol)
continue;
if (bForceRepaint && pDC) // Redraw NOW
RedrawCell(cell.row, cell.col, pDC);
else
InvalidateCellRect(cell); // Redraw at leisure
}
}
for (pos = m_PrevSelectedCellMap.GetStartPosition(); pos != NULL; )
{
DWORD key;
CCellID cell;
m_PrevSelectedCellMap.GetNextAssoc(pos, key, (CCellID&)cell);
if (!IsValid(cell)) continue;
int nState = GetItemState(cell.row, cell.col);
SetItemState(cell.row, cell.col, nState | GVIS_SELECTED);
if (bForceRepaint && pDC)
RedrawCell(cell.row, cell.col, pDC);
else
InvalidateCellRect(cell);
}
if (nMinRow >= 0 && nMinCol >= 0 && nMaxRow >= 0 && nMaxCol >= 0 &&
nMaxRow < m_nRows && nMaxCol < m_nCols &&
nMinRow <= nMaxRow && nMinCol <= nMaxCol)
{
for (int row = nMinRow; row <= nMaxRow; row++)
for (int col = nMinCol; col <= nMaxCol; col++)
{
int nState = GetItemState(row, col);
if (nState & GVIS_SELECTED) {
continue; // Already selected - ignore
}
CCellID cell(row, col);
SetItemState(row, col, nState | GVIS_SELECTED);
if (bForceRepaint && pDC)
RedrawCell(row, col, pDC);
else
InvalidateCellRect(cell);
}
}
if (pDC != NULL) ReleaseDC(pDC);
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SelectAllCells()
{
if (!m_bEnableSelection) return;
SetSelectedRange(m_nFixedRows, m_nFixedCols, GetRowCount()-1, GetColumnCount()-1);
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SelectColumns(CCellID currentCell)
{
if (!m_bEnableSelection) return;
if (currentCell.col < m_nFixedCols) return;
if (!IsValid(currentCell)) return;
SetSelectedRange(GetFixedRowCount(), min(m_SelectionStartCell.col, currentCell.col),
GetRowCount()-1, max(m_SelectionStartCell.col, currentCell.col));
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SelectRows(CCellID currentCell)
{
if (!m_bEnableSelection) return;
if (currentCell.row < m_nFixedRows) return;
if (!IsValid(currentCell)) return;
SetSelectedRange(min(m_SelectionStartCell.row, currentCell.row), GetFixedColumnCount(),
max(m_SelectionStartCell.row, currentCell.row), GetColumnCount()-1);
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
void CGridCtrl::SelectCells(CCellID currentCell)
{
if (!m_bEnableSelection) return;
int row = currentCell.row;
int col = currentCell.col;
if (row < m_nFixedRows || col < m_nFixedCols) return;
if (!IsValid(currentCell)) return;
SetSelectedRange(min(m_SelectionStartCell.row, row), min(m_SelectionStartCell.col, col),
max(m_SelectionStartCell.row, row), max(m_SelectionStartCell.col, col));
}
//////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////
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)) // not in window
cellID.col = -1;
else if (point.x < fixedColWidth) // in fixed col
{
int xpos = 0;
for (int col = 0; col < m_nFixedCols; col++)
{
xpos += GetColumnWidth(col);
if (xpos > point.x) break;
}
cellID.col = col;
}
else // in non-fixed col
{
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)) // not in window
cellID.row = -1;
else if (point.y < fixedRowHeight) // in fixed col
{
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;
}
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
CCellID CGridCtrl::GetTopleftNonFixedCell() const
{
int nVertScroll = GetScrollPos(SB_VERT),
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++);
return CCellID(nRow, nColumn);
}
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
CCellRange CGridCtrl::GetVisibleNonFixedCellRange(LPRECT pRect /*=NULL*/) const
{
CRect rect;
GetClientRect(rect);
CCellID idTopLeft = GetTopleftNonFixedCell();
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);
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);
}
//////////////////////////////////////////////////
//
//////////////////////////////////////////////////
CCellRange CGridCtrl::GetUnobstructedNonFixedCellRange() const
{
CRect rect;
GetClientRect(rect);
CCellID idTopLeft = GetTopleftNonFixedCell();
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);
for (POSITION pos = m_SelectedCellMap.GetStartPosition(); pos != NULL; )
{
DWORD key;
CCellID cell;
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; //+ GetRowHeight(visibleCells.GetTopLeft().row);
else
m_nVScrollMax = 0;
if (VisibleRect.Width() < VirtualRect.Width())
m
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -