CGridCtrl控件类的用法

开源的CGridCtrl类,是VC中的可用的表格控件。相对VC自带的CListCtrl网格控件功能要强很多。但是网上除自带的示例,很少有完整描述使用的过程。在VC2015中的用法如下:

(1)先将源代码的中的GridCtrl_src文件夹和NewCellTypes文件夹复制到当前新建工程源代码目录下。

在窗口的.h文件中添加:#include "GridCtrl_src\GridCtrl.h"

在窗口的.cpp文件中添加:

#include "NewCellTypes/GridURLCell.h"

#include "NewCellTypes/GridCellCombo.h"

#include "NewCellTypes/GridCellCheck.h"

#include "NewCellTypes/GridCellNumeric.h"

#include "NewCellTypes/GridCellDateTime.h"

(2)在对话框上添加一个自定义控件(Custom Control)将ID设为:IDC_GRID

在窗口中,添加关联变量:CGridCtrl m_Grid;

(3)在窗口的OnInitDialog函数中,添加如下代码:

fillData();
m_Grid.GetDefaultCell(FALSE, FALSE)->SetBackClr(RGB(0xFF, 0xFF, 0xE0));
m_Grid.SetFixedColumnSelection(TRUE);
m_Grid.SetFixedRowSelection(TRUE);
m_Grid.EnableColumnHide();
m_Grid.AutoSize();
m_Grid.SetCompareFunction(CGridCtrl::pfnCellNumericCompare);
//填充数据
VOID Ctest1Dlg::fillData()
{
	INT m_nFixCols = 0;
	INT m_nFixRows = 1;
	INT m_nCols    = 6;
	INT m_nRows    = 16;
	m_Grid.SetAutoSizeStyle();
	TRY
	{
		m_Grid.SetRowCount(m_nRows);            //设置行数
		m_Grid.SetColumnCount(m_nCols);         //设置列数
		m_Grid.SetFixedRowCount(m_nFixRows);    //固定行
		m_Grid.SetFixedColumnCount(m_nFixCols); //固定列
	}
	CATCH(CMemoryException, e)
	{
		e->ReportError();
		return;
	}
	END_CATCH

	//用文本填充行列数据
	for (int row = 0; row < m_Grid.GetRowCount(); row++)
	{
		for (int col = 0; col < m_Grid.GetColumnCount(); col++)
		{
			CString str;

			GV_ITEM Item;

			Item.mask = GVIF_TEXT;
			Item.row = row;
			Item.col = col;

			if (row < m_nFixRows)
				str.Format(_T("列 %d"), col);
			else if (col < m_nFixCols)
				str.Format(_T("行 %d"), row);
			else
				str.Format(_T("%d"), row*col);

			Item.strText = str;

			if (rand() % 10 == 1)
			{
				COLORREF clr = RGB(rand() % 128 + 128, 
				                   rand() % 128 + 128, 
				                   rand() % 128 + 128);
				Item.crBkClr = clr;
				//或者m_Grid.SetItemBkColour(row, col, clr);
				Item.crFgClr = RGB(255, 0, 0);
				//或者m_Grid.SetItemFgColour(row, col, RGB(255,0,0));				    
				Item.mask |= (GVIF_BKCLR | GVIF_FGCLR);
			}
			m_Grid.SetItem(&Item);
		}
	}
}

(4)编译时提示:C4996: 'GetVersionExW': 被声明为已否决

处理方法如下:

1.Project Properties > Configuration Properties > C/C++ > General > SDL checks关掉

2.#pragma warning(disable: 4996)          

3./wd 4996

任选一种方法即可。

猜你喜欢

转载自blog.51cto.com/9233403/2122176