源代码可到这里下载:http://download.csdn.net/wuyao721
rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_editdata.mso"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">
rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_editdata.mso"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">
1. 新建基于Dialog的MFC工程GridCtrlTest。
2. 将GridCtrl.cpp、GridCtrl.h等文件加到工程中,并编译通过。
3. 在对话框CGridCtrlTestDlg中增加Custom Control,就是工具箱中的人头像:
设置Custom Control的属性:
注意:Class项的值一定要是MFCGridCtrl,不然就无法将控件关联起来。
4. 在文件GridCtrlTestDlg.h为类CGridCtrlTestDlg增加成员:
- ......
- #include "GridCtrl/GridCtrl.h"
- /
- // CGridCtrlTestDlg dialog
- class CGridCtrlTestDlg : public CDialog
- {
- // Construction
- public:
- CGridCtrlTestDlg(CWnd* pParent = NULL); // standard constructor
- CGridCtrl m_pGrid;
- void GridCtrlInit();
- ......
- }
在GridCtrlTestDlg.cpp文件里的函数DoDataExchange增加映射:
- void CGridCtrlTestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CGridCtrlTestDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- DDX_GridControl(pDX, IDC_MY_GRIDCTRL, m_pGrid);
- //}}AFX_DATA_MAP
- }
5. 让控件显示起来,在OnInitDialog函数里初始化GridCtrl
- void CGridCtrlTestDlg::GridCtrlInit()
- {
- m_pGrid.SetEditable(true);
- m_pGrid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));
- m_pGrid.SetRowCount(8); //初始为10行
- m_pGrid.SetColumnCount(8); //初始化为11列
- m_pGrid.SetFixedRowCount(1); //表头为一行
- m_pGrid.SetFixedColumnCount(1); //表头为一列
- for (int row = 0; row < m_pGrid.GetRowCount(); row++)
- for (int col = 0; col < m_pGrid.GetColumnCount(); col++)
- {
- //设置表格显示属性
- GV_ITEM Item;
- Item.mask = GVIF_TEXT|GVIF_FORMAT;
- Item.row = row;
- Item.col = col;
- 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_pGrid.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_pGrid.SetItem(&Item);
- }
- }
- BOOL CGridCtrlTestDlg::OnInitDialog()
- {
- ......
- // TODO: Add extra initialization here
- GridCtrlInit();
- return TRUE; // return TRUE unless you set the focus to a control
- }