?? 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 + -