📄 gridctrl.cpp
字号:
// GridCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "MemDC.h"
#include "GridCtrl.h"
#include "MyGridFrame.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl
IMPLEMENT_DYNCREATE(CGridCtrl, CWnd)
CGridCtrl::CGridCtrl(int nRows, int nCols, int nFixedRows, int nFixedCols)
{
m_crWindowText = ::GetSysColor(COLOR_WINDOWTEXT);
m_crWindowColour = ::GetSysColor(COLOR_WINDOW);
m_cr3DFace = ::GetSysColor(COLOR_3DFACE);
m_crShadow = ::GetSysColor(COLOR_3DSHADOW);
m_crGridLineColour = RGB(192,192,192);
m_nRows = 0;
m_nCols = 0;
m_nFixedRows = 0;
m_nFixedCols = 0;
// m_bFixedColumnSelection = TRUE;
// m_bFixedRowSelection = TRUE;
m_nVScrollMax = 0; // Scroll position 滚动指针
m_nHScrollMax = 0;
m_bSortOnClick = FALSE; // Sort on header row click
m_bAscending = TRUE; // sorting stuff 排序...
m_nSortColumn = 1;
#ifdef _WIN32_WCE
m_bDoubleBuffer = FALSE; // Use double buffering to avoid flicker?
// 使用双缓冲消除闪烁
#else
m_bDoubleBuffer = TRUE; // Use double buffering to avoid flicker?
// 使用双缓冲消除闪烁
#endif
m_nGridLines = GVL_BOTH; //表格线
m_nBarState = GVL_NONE; //
m_bColSizing = FALSE; //列Sizing状态
m_bAllowDraw = TRUE; // allow draw updates 全部重画
m_bAllowColHide = TRUE; // Columns can be contracted to 0-width via mouse
m_bAllowRowHide = TRUE; // Rows can be contracted to 0-height via mouse
m_pRtcDefault = RUNTIME_CLASS(CGridCell);
m_nResizeCaptureRange = 3; // When resizing columns/row, the cursor has to be
//当改变行/列尺寸时,光标在分隔线
// within +/-3 pixels of the dividing line for
//左右+/-3个象素之内
// resizing to be possible
//可以改变尺寸
m_FillRect.top=m_FillRect.left=m_FillRect.bottom=m_FillRect.right=0;
m_pfnCompare = NULL;
SetGridBkColor(m_crShadow);
SetupDefaultCells();//装载缺省的单元格数据(字体、颜色的信息)
//也就是对m_cellDefault等几个变量的初始化
//所以也就可以理解GetDefaultCell()的返回值了.
// Set up the initial grid size
// 装配最初的表格尺寸
SetRowCount(nRows);
SetColumnCount(nCols);
SetFixedRowCount(nFixedRows);
SetFixedColumnCount(nFixedCols);
// set initial selection range (ie. none)
// m_SelectedCellMap.RemoveAll();
// m_PrevSelectedCellMap.RemoveAll();
}
CGridCtrl::~CGridCtrl()
{
//KillTimer(2);
}
BEGIN_MESSAGE_MAP(CGridCtrl, CWnd)
//{{AFX_MSG_MAP(CGridCtrl)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_CREATE()
ON_WM_LBUTTONDBLCLK()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGridCtrl message handlers
void CGridCtrl::SetupDefaultCells()
{
m_cellDefault.SetGrid(this); // Normal editable cell
//原型是SetGrid(CGridCtrl* pGrid),因为现在就在CGridCtrl类里面,所以可以用this
//另外,CGridDefaultCell继承自CGridCell,且没有改写SetGrid()函数,所以,其实是调用
//CGridCell的SetGrid()函数.
//又因为原型是SetGrid(CGridCtrl* pGrid),而CGridCtrl继承自CWnd,所以,参数其实是要一个
//窗口指针(父窗口?)
m_cellFixedColDef.SetGrid(this); // Cell for fixed columns
m_cellFixedRowDef.SetGrid(this); // Cell for fixed rows
m_cellFixedRowColDef.SetGrid(this); // Cell for area overlapped by fixed columns/rows
m_cellDefault.SetTextClr(m_crWindowText);
m_cellDefault.SetBackClr(m_crWindowColour);
m_cellFixedColDef.SetTextClr(m_crWindowText);
m_cellFixedColDef.SetBackClr(m_cr3DFace);
m_cellFixedRowDef.SetTextClr(m_crWindowText);
m_cellFixedRowDef.SetBackClr(m_cr3DFace);
m_cellFixedRowColDef.SetTextClr(m_crWindowText);
m_cellFixedRowColDef.SetBackClr(m_cr3DFace);
}
BOOL CGridCtrl::SetFixedRowCount(int nFixedRows)
{
if (m_nFixedRows == nFixedRows)
return TRUE;
ASSERT(nFixedRows >= 0);
// ResetSelectedRange();
// Force recalculation
m_idTopLeftCell.col = -1;
if (nFixedRows > GetRowCount())
if (!SetRowCount(nFixedRows))
return FALSE;
// if (m_idCurrentCell.row < nFixedRows)
// SetFocusCell(-1, - 1);
if (1/*!GetVirtualMode()*/)
{
if (nFixedRows > m_nFixedRows)
{
for (int i = m_nFixedRows; i < nFixedRows; i++)
for (int j = 0; j < GetColumnCount(); j++)
{
SetItemState(i, j, GetItemState(i, j) | GVIS_FIXED | GVIS_FIXEDROW);//m_nState是在这里改变的
SetItemBkColor(i, j, CLR_DEFAULT );
SetItemFgColor(i, j, CLR_DEFAULT );
}
}
else
{
for (int i = nFixedRows; i < m_nFixedRows; i++)
{
int j;
for (j = 0; j < GetFixedColumnCount(); j++)
SetItemState(i, j, GetItemState(i, j) & ~GVIS_FIXEDROW );
for (j = GetFixedColumnCount(); j < GetColumnCount(); j++)
{
SetItemState(i, j, GetItemState(i, j) & ~(GVIS_FIXED | GVIS_FIXEDROW) );
SetItemBkColor(i, j, CLR_DEFAULT );
SetItemFgColor(i, j, CLR_DEFAULT );
}
}
}
}
m_nFixedRows = nFixedRows;
Refresh();
return TRUE;
}
BOOL CGridCtrl::SetFixedColumnCount(int nFixedCols)
{
if (m_nFixedCols == nFixedCols)
return TRUE;
ASSERT(nFixedCols >= 0);
if (nFixedCols > GetColumnCount())
if (!SetColumnCount(nFixedCols))
return FALSE;
// if (m_idCurrentCell.col < nFixedCols)
// SetFocusCell(-1, - 1);
// ResetSelectedRange();
// Force recalculation
m_idTopLeftCell.col = -1;
if (1/*!GetVirtualMode()*/)
{
if (nFixedCols > m_nFixedCols)
{
for (int i = 0; i < GetRowCount(); i++)
for (int j = m_nFixedCols; j < nFixedCols; j++)
{
SetItemState(i, j, GetItemState(i, j) | GVIS_FIXED | GVIS_FIXEDCOL);
SetItemBkColor(i, j, CLR_DEFAULT );
SetItemFgColor(i, j, CLR_DEFAULT );
}
}
else
{
{ // Scope limit i,j
for (int i = 0; i < GetFixedRowCount(); i++)
for (int j = nFixedCols; j < m_nFixedCols; j++)
SetItemState(i, j, GetItemState(i, j) & ~GVIS_FIXEDCOL );
}
{// Scope limit i,j
for (int i = GetFixedRowCount(); i < GetRowCount(); i++)
for (int j = nFixedCols; j < m_nFixedCols; j++)
{
SetItemState(i, j, GetItemState(i, j) & ~(GVIS_FIXED | GVIS_FIXEDCOL) );
SetItemBkColor(i, j, CLR_DEFAULT );
SetItemFgColor(i, j, CLR_DEFAULT );
}
}
}
}
m_nFixedCols = nFixedCols;
Refresh();
return TRUE;
}
BOOL CGridCtrl::SetRowCount(int nRows)
{
BOOL bResult = TRUE;
ASSERT(nRows >= 0);
if (nRows == GetRowCount())//新行数=总行数
return bResult;
// Force recalculation
// m_idTopLeftCell.col = -1;
if (nRows < m_nFixedRows)
m_nFixedRows = nRows;
// if (m_idCurrentCell.row >= nRows)
// SetFocusCell(-1, - 1);
int addedRows = nRows - GetRowCount();//增加的行数
// If we are about to lose rows, then we need to delete the GridCell objects
// in each column within each row
if (addedRows < 0)//如果增加的行数<0(有多余的行)
{
//删掉它们:
for (int row = nRows; row < m_nRows; row++)//删掉新行后面的行
{
// Delete cells
//删掉单元格
for (int col = 0; col < m_nCols; col++)
DestroyCell(row, col);
// Delete rows
//删掉行
GRID_ROW* pRow = m_RowData[row];
if (pRow)
delete pRow;//删掉数组中的一个元素
}
m_nRows = nRows;//完成新设置
}
TRY
{
m_arRowHeights.SetSize(nRows);//记录行高的数组
// Change the number of rows.
m_RowData.SetSize(nRows);//记录每行单元格的数组(在这里对m_RowData初始化!!!)
// If we have just added rows, we need to construct new elements for each cell
// and set the default row height
if (addedRows > 0)
{
// initialize row heights and data
int startRow = nRows - addedRows;
for (int row = startRow; row < nRows; row++)
{
m_arRowHeights[row] = m_cellDefault.GetHeight();
m_RowData[row] = new GRID_ROW;//创建了m_RowData的一个元素
m_RowData[row]->SetSize(m_nCols);//对这个元素的初始化(因为它也是一个数组)
for (int col = 0; col < m_nCols; col++)
{
GRID_ROW* pRow = m_RowData[row];
if (pRow && 1/*!GetVirtualMode()*/)
pRow->SetAt(col, CreateCell(row, col));//对数祖赋值
}
m_nRows++;
}
}
}
CATCH (CMemoryException, e)
{
e->ReportError();
bResult = FALSE;
}
END_CATCH
// SetModified();
ResetScrollBars();
Refresh();
return bResult;
}
BOOL CGridCtrl::SetColumnCount(int nCols)
{
BOOL bResult = TRUE;
ASSERT(nCols >= 0);
if (nCols == GetColumnCount())
return bResult;
// Force recalculation
// m_idTopLeftCell.col = -1;//一个CCellID型的变量
if (nCols < m_nFixedCols)
m_nFixedCols = nCols;
// if (m_idCurrentCell.col >= nCols)
// SetFocusCell(-1, - 1);
int addedCols = nCols - GetColumnCount();
// If we are about to lose columns, then we need to delete the GridCell objects
// within each column
if (addedCols < 0 /*&& !GetVirtualMode()*/)
{
for (int row = 0; row < m_nRows; row++)
for (int col = nCols; col < GetColumnCount(); col++)
DestroyCell(row, col);
}
TRY
{
// Change the number of columns.
m_arColWidths.SetSize(nCols);
// Change the number of columns in each row.
if (1/*!GetVirtualMode()*/)
for (int i = 0; i < m_nRows; i++)
if (m_RowData[i])
m_RowData[i]->SetSize(nCols);
// If we have just added columns, we need to construct new elements for each cell
// and set the default column width
if (addedCols > 0)
{
// initialized column widths
int startCol = nCols - addedCols;
for (int col = startCol; col < nCols; col++)
m_arColWidths[col] = m_cellFixedColDef.GetWidth();
// initialise column data
if (1/*!GetVirtualMode()*/)
{
for (int row = 0; row < m_nRows; row++)
for (col = startCol; col < nCols; col++)
{
GRID_ROW* pRow = m_RowData[row];
if (pRow)
{
pRow->SetAt(col, CreateCell(row, col));
}
}
}
}
// else // check for selected cell ranges
// ResetSelectedRange();
}
CATCH (CMemoryException, e)
{
e->ReportError();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -