author:songyanwu
Grid control 属性和Execl 很相似,我经常对类似数据处理都会使用 listctrl。
MFC Grid control属性介绍:
The control features:
The sample project demonstrates most of the features of the grid control。
接下来看看强大的友好界面:
接下来 也看看结构实现:
To use the Grid control in your project you will need to add a number of files to your project:
gridctrl.cpp, gridctrl.h
Main grid control source and header files. gridcellbase.cpp, gridcellbase.h
Main grid cell base class. gridcell.cpp, gridcell.h
Main grid cell default class implementation. CellRange.h
Definition of CCellID and CCellRange helper classes. MemDC.h
Keith Rule's memory DC helper class. InPlaceEdit.cpp, InPlaceEdit.h
In-place edit windows source and header files. GridDropTarget.cpp, GridDropTarget.h
Grid control OLE drag and drop target. Only necessary if you don't define GRIDCONTROL_NO_DRAGDROP in gridctrl.h Titletip.cpp, Titletip.h
Titletips for cells, from Zafir Anjum. Only necessary if you don't define GRIDCONTROL_NO_TITLETIPS in gridctrl.h
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.
链接地址
下面是我自己对源码的简单实用效果:
// 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(); };
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); } }