MFC Grid Control
Author : Songyanwu
The MFC Grid Control property describes:
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 is disabled for row, columns or both.
- Auto row or column sizing when dividers is double-clicked.
- Any number of the 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 has 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 when a cell has focus, editing'll start on the that cell, and the arrow keys to allow Navig ation to other keys. If The current focus cell was clicked on, editing would start and the arrow keys would 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 PR Eview).
- 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 is 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.
Next look at the powerful friendly interface:
Let's look at the structure implementation:
Files
To use the Grid control in your project, you'll 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 would pass on certain mouse Messages if it considers it necessary. The grid also contains a drag and drop target ( cgriddroptarget
) that's registered to handle drop Notificatio NS, and there is also a title Tip object ( ctitletip
) so displays the contents of cells when the physical di Mensions of a cell is insufficient to display it ' contents in their entirety.
The grid cells can be of any type as long as the class was derived from CGridBaseCell
. Included with the package are 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 is both main types of cell-fixed and non-fixed. The Fixed cells is typically on the left and top of the grid and does not move when the grid is scrolled. Typically these contain column and row headings and is 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 is stored in CGridDefaultCell
objects. There is 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 is both fixed rows and columns cells. Thus in order to set a default property for the grid, use to CGridCtrL::GetDefaultCell
get the default cell implementation. 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 are only allocated and used if you set that cell's font to A Non-default value.
The grid also has a virtual mode This stops the grid from actually creating cells, and allows your to specify Eith Er a callback funtion or message handler that the grid would call each time it needs information on a cell. This saves enourmously in memory at the expense of slightly decreased performance. There is a message that's sent to the grid's parent that would help you GVN_ODCACHEHINT
cache data in preparation for the grid's cel L Info requests.
Grid cells is stored row-by-row, so all operations on large numbers of cells should is done row-by-row as well.
Link Address
Here is my own simple and practical effect on the source code:
The introduction of the preceding properties is very powerful!
Next talk about the problems that should be paid attention to in the process of using source code
1 Memdc.h defined in the class CMEMDC need to re-change the name GCMEMDC or the other line, CMemDC This class of Microsoft has been encapsulated, so the names. 2 The control class should also be instantiated (but others are done, directly modify the control variable name OK), of course, you can also declare the object to register.
The following shows my code implementation.
1 Add header File # include "GridCtrl.h" 2 for Declaration and binding declaration: Cgridctrl M_pgrid; Bound control:
GridCtrlDemoDlg.h: Header file//#pragma once#include "gridctrl.h"//Cgridctrldemodlg Dialog Class Cgridctrldemodlg:public cdial ogex{//construct Public:cgridctrldemodlg (cwnd* pparent = NULL);//Standard constructor Cgridctrl m_pgrid;//dialog Data enum {IDD = Idd_gridctrldemo_ DIALOG};p rotected:virtual void DoDataExchange (cdataexchange* pDX);//DDX/DDV support//implement Protected:hicon M_hicon; CImageList m_imagelist;//generated message map function virtual BOOL OnInitDialog () afx_msg void OnSysCommand (UINT NID, LPARAM LPARAM); afx_ msg void OnPaint (); afx_msg hcursor Onquerydragicon ();D eclare_message_map () Public:cgridctrl m_grid;void gridctrlinit ( );};
3 initialization
void Cgridctrldemodlg::gridctrlinit () {m_grid.seteditable (true); M_grid.settextbkcolor (RGB (0xFF, 0xFF, 0xE0));// Yellow background m_grid.setrowcount (10); The initial line is 8 M_grid.setcolumncount (12); Initialized to 8 columns M_grid.setfixedrowcount (1); The table header is a row of m_grid.setfixedcolumncount (1); The table header is a column for (int row = 0; row < M_grid.getrowcount (); row++) for (int col = 0; col < M_grid.getcolumncount (); col++) { Set table Display Properties Gv_item ITEM; Item.mask = gvif_text| Gvif_format;item.row = Row;item.col = Col;m_grid.setrowheight (row,45); Set the row height m_grid.setcolumnwidth (0,94); Set 0 column width m_grid.setcolumnwidth (col,84); Set each column width if (row==0&&col==0)//(0,0) {Item.nformat = dt_center| Dt_wordbreak;item.strtext.format (_t ("Report display"), col);} else if (Row < 1)//Set 0-row header display {Item.nformat = dt_center| Dt_wordbreak;item.strtext.format (_t ("Fixed value%d"), col);} else if (Col < 1)//Set 0 list header display {if (row< M_grid.getrowcount ()) {Item.nformat = dt_center| dt_vcenter| dt_singleline| Dt_end_ellipsis;item.strtext.format (_t ("%d value"), row);}} Else{item.nformat = Dt_center| dt_vcenter| dt_singleline| Dt_end_ellipsis;item.strtext.format (_t (""), 2);} M_grid.setitem (&item); }}
So after these steps are done, then that is the effect I have above ha!
I will then Chris Maunder code encapsulated into DLLs is more convenient and quick to use, if you are in use or other problems hope we can communicate together to learn.
Special attention points:
after I add the gridctrl_src file, VS2010 Compile Error: gridctrltest\memdc.h: Error C2011: "CMEMDC": "Class" type redefinition I think Microsoft has implemented their own CMEMDC class library, so using Keith Rule to memory DC The library will report a redefinition error. Workaround rename CMemDC to GCMEMDC and remember to modify both MemDC.h andGridCtrl.cpp All the CMEMDC items used.
Then look at the points of attention I mentioned earlier can be solved!
MFC Grid Control 2.27