GridCtrl实例及要点

源代码可到这里下载: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">

1.  新建基于DialogMFC工程GridCtrlTest


2.  GridCtrl.cppGridCtrl.h等文件加到工程中,并编译通过。


3.  在对话框CGridCtrlTestDlg中增加Custom Control,就是工具箱中的人头像:



设置Custom Control的属性:


注意:Class项的值一定要是MFCGridCtrl,不然就无法将控件关联起来。



4.  在文件GridCtrlTestDlg.h为类CGridCtrlTestDlg增加成员:

  1. ......
  2. #include "GridCtrl/GridCtrl.h" 
  3. /
  4. // CGridCtrlTestDlg dialog
  5. class CGridCtrlTestDlg : public CDialog
  6. {
  7. // Construction
  8. public:
  9.     CGridCtrlTestDlg(CWnd* pParent = NULL); // standard constructor
  10.     CGridCtrl m_pGrid;
  11.     void GridCtrlInit();
  12. ......
  13. }

GridCtrlTestDlg.cpp文件里的函数DoDataExchange增加映射:

  1. void CGridCtrlTestDlg::DoDataExchange(CDataExchange* pDX)
  2. {
  3.     CDialog::DoDataExchange(pDX);
  4.     //{{AFX_DATA_MAP(CGridCtrlTestDlg)
  5.         // NOTE: the ClassWizard will add DDX and DDV calls here
  6.     DDX_GridControl(pDX, IDC_MY_GRIDCTRL, m_pGrid);
  7.     //}}AFX_DATA_MAP
  8. }


5.  让控件显示起来,在OnInitDialog函数里初始化GridCtrl

  1. void CGridCtrlTestDlg::GridCtrlInit()
  2. {
  3.     m_pGrid.SetEditable(true);
  4.     m_pGrid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));
  5.     m_pGrid.SetRowCount(8); //初始为10行
  6.     m_pGrid.SetColumnCount(8); //初始化为11列
  7.     m_pGrid.SetFixedRowCount(1); //表头为一行
  8.     m_pGrid.SetFixedColumnCount(1); //表头为一列 
  9.     for (int row = 0; row < m_pGrid.GetRowCount(); row++)
  10.     for (int col = 0; col < m_pGrid.GetColumnCount(); col++)
  11.     { 
  12.         //设置表格显示属性
  13.         GV_ITEM Item; 
  14.         Item.mask = GVIF_TEXT|GVIF_FORMAT;
  15.         Item.row = row;
  16.         Item.col = col;
  17.         if(row==0&&col==0) //第(0,0)格
  18.         {
  19.             Item.nFormat = DT_CENTER|DT_WORDBREAK;
  20.             Item.strText.Format(_T("报表显示"),col);
  21.         }
  22.         else if (row < 1) //设置0行表头显示
  23.         {        
  24.             Item.nFormat = DT_CENTER|DT_WORDBREAK;
  25.             Item.strText.Format(_T(" 项目%d"),col);
  26.         }
  27.         else if (col < 1) //设置0列表头显示
  28.         {
  29.             if(row< m_pGrid.GetRowCount())
  30.             {
  31.                 Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  32.                 Item.strText.Format(_T("第%d次"),row);
  33.             }
  34.         }
  35.         else
  36.         {
  37.             Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  38.             Item.strText.Format(_T(""),2);
  39.         }
  40.         m_pGrid.SetItem(&Item); 
  41.     }
  42. }
  43. BOOL CGridCtrlTestDlg::OnInitDialog()
  44. {
  45. ......
  46.     // TODO: Add extra initialization here
  47.     GridCtrlInit();
  48.     return TRUE;  // return TRUE  unless you set the focus to a control
  49. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值