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.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
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 a
CGridCell
class that handles basic data storage and editing. Extensions such as the
CGridCellCombo
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, use
CGridCtrL::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); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。