GridCtrl学习笔记(1)建立使用GridCtrl的工程

1、同事给的和我在网上找的开源代码是一样的。
(我http://www.codeproject.com/KB/miscctrl/gridctrl.aspx下载Keith Rule的源码,包括他的实例源码
2、关于以下两个写法:
DDX_GridControl(pDX, IDC_GRIDCTRL, m_pGrid);//推荐
DDX_Control(pDX, IDC_GRIDCTRL, m_pGrid);
Grid的基本类是源于CWnd的CgridCtrl。为了使用它,你可以使用微软的 VC++的对话框编辑器,把一个普通的控件放在对话框上,并且输入"MFCGridCtrl"(不包括引号)作为类名。Grid的子类使用DDX机制(可以通过ClassWizard来进行默认设置),使用DDX_GridControl函数代替DDX_Control(可以通过手动设置 ClassWizard的输入来实现)。这些保证你的控件作为一个注册对象而不会产生一些莫名其妙的WIN95问题。
3、建立工程步骤:
(1)在rc中选择并添加custom control控件;
一定要把Class设置为MFCGridCtrl哦;

(2)在Dlg.h中做声明:
// GridControlTest02Dlg.h : header file
//
 
#pragma once
#include "GridCtrl_src/GridCtrl.h"
class CGridControlTest02DlgAutoProxy;
 
 
// CGridControlTest02Dlg dialog
class CGridControlTest02Dlg : public CDialogEx
{
    DECLARE_DYNAMIC(CGridControlTest02Dlg);
    friend class CGridControlTest02DlgAutoProxy;
 
// Construction
public:
    CGridControlTest02Dlg(CWnd* pParent = NULL);    // standard constructor
    virtual ~CGridControlTest02Dlg();
    CGridCtrl m_pGrid;
    void GridCtrlInit();
 
// Dialog Data
    enum { IDD = IDD_GRIDCONTROLTEST02_DIALOG };
 
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
(3)在Dlg.cpp中做声明及添加初始化函数:
要注意了哦:要把声明放在对应的class里,我一开始放错了class,编译时无法通过,TAT,都是泪。
void CGridControlTest02Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_GridControl(pDX, IDC_GRIDCTRL, m_pGrid);
}
BOOL CGridControlTest02Dlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    // Add "About..." menu item to system menu.
 
    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);
 
    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }
 
    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
 
    ShowWindow(SW_MINIMIZE);
 
    // TODO: Add extra initialization here
    GridCtrlInit();
 
    return TRUE;  // return TRUE  unless you set the focus to a control
}
 
  1. voidCGridControlTest04Dlg::GridCtrlInit()
  2. {
  3. m_pGrid.SetEditable(true);
  4. m_pGrid.SetTextBkColor(RGB(0xFF,0xFF,0xE0));//黄色背景
  5. m_pGrid.SetRowCount(101);//初始为100行
  6. m_pGrid.SetColumnCount(26);//初始化为25列
  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. m_pGrid.SetRowHeight(row,25);//设置各行高
  18. m_pGrid.SetColumnWidth(0,64);//设置0列宽
  19. m_pGrid.SetColumnWidth(col,64);//设置各列宽
  20. if(row==0&&col==0)//第(0,0)格
  21. {
  22. Item.nFormat = DT_CENTER|DT_WORDBREAK;
  23. Item.strText.Format(_T("报表显示"),col);
  24. }
  25. elseif(row <1)//设置0行表头显示
  26. {
  27. Item.nFormat = DT_CENTER|DT_WORDBREAK;
  28. Item.strText.Format(_T(" 项目%d"),col);
  29. }
  30. elseif(col <1)//设置0列表头显示
  31. {
  32. if(row< m_pGrid.GetRowCount())
  33. {
  34. Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  35. Item.strText.Format(_T("第%d次"),row);
  36. }
  37. }
  38. else
  39. {
  40. Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
  41. Item.strText.Format(_T(""),2);
  42. }
  43. m_pGrid.SetItem(&Item);
  44. }
  45. }
 
 
 
(4)把源码整个文件夹复制到E:LHLWorkProjectsDebugSEVisual Studio 2010ProjectsGridControlTest02GridControlTest02
然后建立文件夹存放cpp及h文件

解释:

gridctrl.cpp, gridctrl.h Grid控件资源文件和头文件

gridcellbase.cpp, gridcellbase.h 单元格的基础类

gridcell.cpp, gridcell.h 单元格的默认执行文件

CellRange.h CcellID和CcellRange类的定义

MemDC.h Keith Rule's的直接存储类

InPlaceEdit.cpp, InPlaceEdit.h 定位编辑窗口的源文件和头文件

GridDropTarget.cpp, GridDropTarget.h Grid容器的drag和drop对象 只有在gridctrl.h中没有定义 GRIDCONTROL_NO_DRAGDROP的时候才有必要使用。

Titletip.cpp, Titletip.h 从Zafir Anjum那里的到的单元格标题提示. 只有在gridctrl.h中没有定义 GRIDCONTROL_NO_TITLETIPS 的时候才有必要使用

(5)运行结果如下:

 
 
 
 
 
 
 

附件列表

原文地址:https://www.cnblogs.com/ciuciu/p/4462760.html