📄 cgridctrl.cpp
字号:
}
else
{
if(m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
{
int x = nFixedColWidth;
for(col = minVisibleCol; col <= maxVisibleCol; col++)
{
x += GetColumnWidth(col);
for(int i=minVisibleRow; i<=maxVisibleRow;i++)
{
pCell=GetCell(i,col);
if(!pCell->m_bSkipV)
{
GetCellRect(i,col,removelinerect);
pDC->MoveTo(x-1,removelinerect.top);
pDC->LineTo(x-1, removelinerect.bottom+1);
}
}
}
}
if(m_nGridLines==GVL_BOTH||m_nGridLines==GVL_HORZ)
{
int y = nFixedRowHeight;
for(row=minVisibleRow; row<=maxVisibleRow;row++)
{
y+=GetRowHeight(row);
for(int colj = minVisibleCol; colj <= maxVisibleCol; colj++)
{
pCell=GetCell(row,colj);
if(!pCell->m_bSkipH)
{
GetCellRect(row,colj,removelinerect);
pDC->MoveTo(removelinerect.left-1,y-1 );
pDC->LineTo(removelinerect.right,y-1);
}
}
}
}
}
pDC->SelectStockObject(NULL_PEN);
#ifdef USE_MEMDC // Use a memDC for flicker free update
}
#else // Use normal DC - this helps in debugging
}
#endif
////////////////////////////////////////////////////////////////////////////////////////
// CGridCtrl Cell selection stuff
BOOL CGridCtrl::IsValid(int nRow, int nCol) const
{
return (nRow >= 0 && nRow < m_nRows && nCol >= 0 && nCol < m_nCols);
}
BOOL CGridCtrl::IsValid(const CCellID& cell) const
{
return IsValid(cell.row, cell.col);
}
BOOL CGridCtrl::IsValid(const CCellRange& range) const
{
return (range.GetMinRow() >= 0 && range.GetMinCol() >= 0 && range.GetMaxRow() >= 0 && range.GetMaxCol() >= 0 && range.GetMaxRow() < m_nRows && range.GetMaxCol() < m_nCols);
}
void CGridCtrl::SetRedraw(BOOL bAllowDraw, BOOL bResetScrollBars /* = FALSE */)
{
TRACE(_T("%s: Setting redraw to %s\n"), GetRuntimeClass()->m_lpszClassName, bAllowDraw? _T("TRUE") : _T("FALSE"));
if (bAllowDraw && !m_bAllowDraw)
{
Invalidate();
}
m_bAllowDraw = bAllowDraw;
if (bResetScrollBars)
{
ResetScrollBars();
}
}
BOOL CGridCtrl::RedrawCell(const CCellID& cell, CDC* pDC /* = NULL */)
{
return RedrawCell(cell.row, cell.col, pDC);
}
BOOL CGridCtrl::RedrawCell(int nRow, int nCol, CDC* pDC /* = NULL */)
{
BOOL bResult = TRUE;
BOOL bMustReleaseDC = FALSE;
if (!m_bAllowDraw || !IsCellVisible(nRow, nCol))
{
return FALSE;
}
CRect rect;
if (!GetCellRect(nRow, nCol, rect))
{
return FALSE;
}
if (!pDC)
{
pDC = GetDC();
if(pDC)
{
bMustReleaseDC = TRUE;
}
}
if (pDC)
{
if (nRow < m_nFixedRows || nCol < m_nFixedCols)
{
if(m_bFocusCell)
{
CGridCell *pCell=GetCell(nRow,nCol);
bResult = DrawFixedCell(pDC, nRow, nCol, rect, TRUE);
}
}
else
{
CGridCell *pCell=GetCell(nRow,nCol);
CGridCell *pTopLeftCell;
CRect rectTemp;
int theRightCol=0;
int theBottomRow=0;
if(pCell->m_bHide==TRUE)
{
pTopLeftCell=GetCell(pCell->m_TopLeftCellId.row,pCell->m_TopLeftCellId.col);
nRow=pCell->m_TopLeftCellId.row;
nCol=pCell->m_TopLeftCellId.col;
theRightCol=nCol+pTopLeftCell->m_nVhide;
theBottomRow=nRow+pTopLeftCell->m_nHhide;
}
else if(pCell->m_nHhide!=0 || pCell->m_nVhide!=0)
{
theRightCol=nCol+pCell->m_nVhide;
theBottomRow=nRow+pCell->m_nHhide;
}
if(pCell->m_bHide==TRUE || pCell->m_nHhide!=0 || pCell->m_nVhide!=0)
{
if(GetCellRect(theBottomRow,theRightCol,rectTemp))
{
if( rect.right!=rectTemp.right)
{
rect.right=rectTemp.right;
}
if( rect.bottom!=rectTemp.bottom)
{
rect.bottom=rectTemp.bottom;
}
CCellID idTopLeft = GetTopleftNonFixedCell();
int minVisibleRow = idTopLeft.row;
int minVisibleCol = idTopLeft.col;
minVisibleRow=minVisibleRow > nRow ? minVisibleRow : nRow;
minVisibleCol=minVisibleCol > nCol ? minVisibleCol : nCol;
GetCellRect(minVisibleRow,minVisibleCol,rectTemp);
rect.left=rectTemp.left;
rect.top=rectTemp.top;
}
}
bResult =DrawCell(pDC, nRow, nCol, rect, TRUE);
CPen pen;
TRY
{
pen.CreatePen(PS_SOLID, 0, m_crGridColour);
}
CATCH (CException, e)
{
e->Delete();
}
END_CATCH
CPen* pOldPen = (CPen*) pDC->SelectObject(&pen);
if(m_bLineType)
{
if(pCell->m_bLeft)
{
pDC->MoveTo(rect.left-1,rect.top-1);
pDC->LineTo(rect.left-1,rect.bottom);
}
if(pCell->m_bTop)
{
pDC->MoveTo(rect.left-1,rect.top-1);
pDC->LineTo(rect.right,rect.top-1);
}
if(pCell->m_bRight)
{
pDC->MoveTo(rect.right,rect.top-1);
pDC->LineTo(rect.right,rect.bottom);
}
if(pCell->m_bBottom)
{
pDC->MoveTo(rect.left-1,rect.bottom);
pDC->LineTo(rect.right,rect.bottom);
}
}
else
{
if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_HORZ)
{
pDC->MoveTo(rect.left, rect.bottom);
pDC->LineTo(rect.right+1, rect.bottom);
}
if (m_nGridLines == GVL_BOTH || m_nGridLines == GVL_VERT)
{
pDC->MoveTo(rect.right, rect.top);
pDC->LineTo(rect.right, rect.bottom+1);
}
}
pDC->SelectObject(pOldPen);
}
}
else
{
InvalidateRect(rect, TRUE);
}
if (bMustReleaseDC)
{
ReleaseDC(pDC);
}
return bResult;
}
BOOL CGridCtrl::RedrawRow(int row)
{
BOOL bResult = TRUE;
CDC* pDC = GetDC();
for (int col = 0; col < GetColumnCount(); col++)
{
bResult = RedrawCell(row, col, pDC) && bResult;
}
if (pDC)
{
ReleaseDC(pDC);
}
return bResult;
}
BOOL CGridCtrl::RedrawColumn(int col)
{
BOOL bResult = TRUE;
CDC* pDC = GetDC();
for (int row = 0; row < GetRowCount(); row++)
{
bResult = RedrawCell(row, col, pDC) && bResult;
}
if (pDC)
{
ReleaseDC(pDC);
}
return bResult;
}
CCellID CGridCtrl::SetFocusCell(int nRow, int nCol)
{
return SetFocusCell(CCellID(nRow, nCol));
}
CCellID CGridCtrl::SetFocusCell(CCellID cell)
{
if (cell == m_idCurrentCell)
{
return m_idCurrentCell;
}
//在进行选中之前,就替换焦点单元格
if(cell.row>-1 && cell.col>-1)
{
CGridCell* pCell=GetCell(cell.row,cell.col);
if(pCell)
{
if(pCell->m_bHide) //如果是隐藏单元格
{
cell=pCell->m_TopLeftCellId;
}
}
else
{
return cell;
}
}
CCellID idPrev = m_idCurrentCell; //保存下以前的单元格数据
m_idCurrentCell = cell; //设置当前选中单元格的数据
if (IsValid(idPrev)) //检测之前的单元格是否有效
{
//先向父窗发送一个改变点击单元格位置的消息
SendMessageToParent(idPrev.row, idPrev.col, GVN_SELCHANGING);
// if(!m_bEnableSelection) /////////////////20040824//////////////////////
// {
//取消之前点中单元格的选中状态
SetItemState(idPrev.row, idPrev.col, GetItemState(idPrev.row, idPrev.col) & ~GVIS_FOCUSED);
// }
RedrawCell(idPrev); //重新绘制之前选中的单元格
if (idPrev.col != m_idCurrentCell.col)
{
for (int row = 0; row < m_nFixedRows; row++)
{
RedrawCell(row, idPrev.col); //重新绘制之前选中的单元格的固定行与列
}
}
if (idPrev.row != m_idCurrentCell.row)
{
for (int col = 0; col < m_nFixedCols; col++)
{
RedrawCell(idPrev.row, col);
}
}
}
if (IsValid(m_idCurrentCell)) //如果当前选中的单元格不是失效的
{
// if(!m_bEnableSelection) /////////////////20040824//////////////////////
// {
SetItemState(m_idCurrentCell.row, m_idCurrentCell.col, GetItemState(m_idCurrentCell.row, m_idCurrentCell.col) | GVIS_FOCUSED);
// }
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);
}
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();
}
// Unselect all previously selected cells
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)
{
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;
}
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;
}
//在此处根据单元格的类型进行选择范围的判断
/*
1,普通显示的单元格
2,合并单元格的显示单元格
3,合并单元格的隐藏单元格
*/
int nMinRow,nMinCol,nMaxRow,nMaxCol;
int nMinRow1,nMinCol1,nMaxRow1,nMaxCol1;
nMinRow=min(m_SelectionStartCell.row, row);
nMinCol=min(m_SelectionStartCell.col, col);
nMaxRow=max(m_SelectionStartCell.row, row);
nMaxCol=max(m_SelectionStartCell.col, col);
nMinRow1=nMinRow;
nMinCol1=nMinCol;
nMaxRow1=nMaxRow;
nMaxCol1=nMaxCol;
int x1,y1,x2,y2;
CGridCell* pCell;
for(int i=nMinRow;i<=nMaxRow;i++)
{
for(int j=nMinCol;j<=nMaxCol;j++)
{
pCell=GetCell(i,j);
if(pCell->m_nHhide!=0 || pCell->m_nVhide!=0) //合并区域的显示单元格
{
x1=i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -