首页 > 代码库 > MFC Grid control 2.27

MFC Grid control 2.27

MFC Grid control

author:songyanwu


MFC Grid control属性介绍:


The control features:

  • Cell selection using the mouse, with optional Control and Shift key combinations. Selection can be disabled.
  • Row and Column resizing. Sizing can be disabled for row, columns or both.
  • Auto row or column sizing when dividers are double-clicked.
  • Any number of fixed rows and columns.
  • Individual cells can have separate text and background colours.
  • Individual cells can have separate fonts.
  • Individual cells can be marked "Read-Only", or have their modification status set and checked.
  • OLE Drag and drop.
  • Ctrl-C, Ctrl-X and Ctrl-V perform clipboard copy, cut and paste, and Ctrl-A for "Select All"
  • In place editing of cell contents. If a character key is pressed while a cell has focus, editing will start on that cell, and the arrow keys will allow navigation to other keys. If the current focus cell is clicked on, editing will start and the arrow keys will move the carat inside the edit control. Editing can be disabled.
  • Support for Microsoft intellimouse.
  • Optional grid lines.
  • Images in any cell
  • "Virtual" mode for large datasets
  • Full printing support, for either a Doc/View environment (inc Print preview) or a standalone dialog based app (no print preview).
  • Optional "List mode", including full row selection, single row selection, and sort on column header click.
  • Numerous virtual functions to allow this control to be extended very easily.
  • Unicode support.
  • WinCE support
  • Titletips for cells that are too small to display their data.
  • Hidden rows and columns
  • Compiles under VC 4.2, 5.0, 6.0 and under the CE toolkit version 2.0 and 3.0

The sample project demonstrates most of the features of the grid control。




接下来看看强大的友好界面:


接下来 也看看结构实现:

Files

To use the Grid control in your project you will need to add a number of files to your project:

gridctrl.cpp, gridctrl.hMain grid control source and header files.
gridcellbase.cpp, gridcellbase.hMain grid cell base class.
gridcell.cpp, gridcell.hMain grid cell default class implementation.
CellRange.hDefinition of CCellID and CCellRange helper classes.
MemDC.hKeith Rule‘s memory DC helper class.
InPlaceEdit.cpp, InPlaceEdit.hIn-place edit windows source and header files.
GridDropTarget.cpp, GridDropTarget.hGrid control OLE drag and drop target. Only necessary if you don‘t define GRIDCONTROL_NO_DRAGDROP in gridctrl.h
Titletip.cpp, Titletip.hTitletips for cells, from Zafir Anjum. Only necessary if you don‘t define GRIDCONTROL_NO_TITLETIPS in gridctrl.h


Structure

The grid is based on a framework (the CGridCtrl object) that organises and controls a collection of cells (CGridBaseCell) that contain the data, perform operations such as drawing, and handle methods such as button clicks. The grid object itself handles events such as button clicks before the cells get a chance, and will pass on certain mouse messages if it considers it necessary. The grid also contains a drag and drop target (CGridDropTarget) that is registered to handle drop notifications, and there is also a title tip object (CTitleTip) that displays the contents of cells when the physical dimensions of a cell are insufficient to display its‘ contents in their entirety.

The grid cells can be of any type as long as the class is derived from CGridBaseCell. Included with the package is aCGridCell class that handles basic data storage and editing. Extensions such as theCGridCellCombo and CGridURLCell class demonstrate how to create your own cell classes.

There are two main types of cell - fixed and non-fixed. Fixed cells are typically on the left and top of the grid and do not move when the grid is scrolled. Typically these contain column and row headings and are not editable. Non fixed cells make up the "interior" of the grid and can be edited and selected.

Default values for various properties of the grid are stored in CGridDefaultCell objects. There are currently 4 of these objects per grid - one each to hold default values for the non-fixed cells, fixed columns, foxed rows, and cells that are both fixed rows and columns cells. Thus in order to set a default property for the grid, useCGridCtrL::GetDefaultCell to get the default cell implementation you are after, then set it‘s values directly.

Cells hold their property values explicitely, except for the font property. Each cell holds a pointer to a font structure, and this pointer is only allocated and used if you set that cell‘s font to a non-default value.

The grid also has a virtual mode that stops the grid from actually creating cells, and allows you to specify either a callback funtion or message handler that the grid will call each time it needs information on a cell. This saves enourmously on memory at the expense of slightly decreased performance. There is a GVN_ODCACHEHINT message that is sent to the grid‘s parent that will help you cache data in preparation for the grid‘s cell info requests.

Grid cells are stored row-by-row, so all operations on large numbers of cells should be done row-by-row as well.


链接地址



下面是我自己对源码的简单实用效果:



前面属性的介绍已近很是强大了吧!

接下来说说在使用源码过程中应该注意的问题

1  Memdc.h 中定义的类 CMemDC 需要重新改变个 名称  GCMemDC 或者 其他也行,CMemDC 这个类微软进行了封装,所以重名了。
2 还应该对控件类实例化 (不过人家也做好了,直接修改控件变量名就ok),当然你自己也可以申明对象进行注册。

下面展现我的代码实现。

1 添加头文件
#include "GridCtrl.h"
2 进行声明与绑定
 声明:CGridCtrl m_pGrid;
绑定控件:
// GridCtrlDemoDlg.h : 头文件
//

#pragma once
#include "gridctrl.h"


// CGridCtrlDemoDlg 对话框
class CGridCtrlDemoDlg : public CDialogEx
{
// 构造
public:
	CGridCtrlDemoDlg(CWnd* pParent = NULL);	// 标准构造函数
	CGridCtrl m_pGrid;

// 对话框数据
	enum { IDD = IDD_GRIDCTRLDEMO_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持


// 实现
protected:
	HICON m_hIcon;
	CImageList m_ImageList;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	CGridCtrl m_Grid;
	void GridCtrlInit();
};

3 初始化
void CGridCtrlDemoDlg::GridCtrlInit()
{
	m_Grid.SetEditable(true);
	m_Grid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));//黄色背景

	m_Grid.SetRowCount(10); //初始为8行
	m_Grid.SetColumnCount(12); //初始化为8列
	m_Grid.SetFixedRowCount(1); //表头为一行
	m_Grid.SetFixedColumnCount(1); //表头为一列 

	for (int row = 0; row < m_Grid.GetRowCount(); row++)
		for (int col = 0; col < m_Grid.GetColumnCount(); col++)
		{ 
			//设置表格显示属性
			GV_ITEM Item; 
			Item.mask = GVIF_TEXT|GVIF_FORMAT;
			Item.row = row;
			Item.col = col;

			m_Grid.SetRowHeight(row,45); //设置各行高          
			m_Grid.SetColumnWidth(0,94); //设置0列宽 
			m_Grid.SetColumnWidth(col,84); //设置各列宽

			if(row==0&&col==0) //第(0,0)格
			{
				Item.nFormat = DT_CENTER|DT_WORDBREAK;
				Item.strText.Format(_T("报表显示"),col);
			}
			else if (row < 1) //设置0行表头显示
			{        
				Item.nFormat = DT_CENTER|DT_WORDBREAK;
				Item.strText.Format(_T(" 定值%d"),col);
			}
			else if (col < 1) //设置0列表头显示
			{
				if(row< m_Grid.GetRowCount())
				{
					Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
					Item.strText.Format(_T("第%d次值"),row);
				}
			}
			else
			{
				Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
				Item.strText.Format(_T(""),2);
			}
			m_Grid.SetItem(&Item); 
		}
}

那么这几步做完以后,那么就是我上面的效果了哈!


我接下来也会将Chris Maunder 代码封装成dll 更方便快捷的使用了,如果你在使用或其他问题希望我们可以共同沟通学习。


特别注意点:

我添加GridCtrl_src下文件后,VS2010编译报错:
gridctrltest\memdc.h(26): error C2011: “CMemDC”:“class”类型重定义
 我认为微软已经实现了他们自己的CMemDC 类库,所以使用Keith Rule到memory DC库会报重定义错误。
   解决办法重命名CMemDC为GCMemDC,记得同时修改MemDC.h及GridCtrl.cpp所有用到的CMemDC项。

那么请看我前面提到的注意点即可解决!


MFC Grid control 2.27