CGridCtrl 在vc6下的使用

来源:互联网 发布:观韬中茂律师所 知乎 编辑:程序博客网 时间:2024/05/22 06:52

      最近在做一个小型的进销存。因为后期要显示相关的报表信息。。在经过一系列的搜索 终于选择使用CGridCtrl。为什么选择这个呢~一个是功能的强大。。再就是比较灵活。 最重要的我选择这个的原因是可以合并单元格。

     但是在使用CGridCtrl控件的时候也遇到过很多问题。一个是版本问题。我在CodeProject上下载了一个CGridCtrl 地址:http://www.codeproject.com/KB/miscctrl/gridctrl.aspx  ,下载了这个版本程序。经过一系列折腾。。最终失败。。。原因是不能合并单元格。也许是我的E文不好。

     在经过大量的时间搜索。。终于找到一个可以用的CGridCtrl的版本。 这个版本是从这里 http://www.vckbase.com/document/viewdoc/?id=256 下载了程序。。然后从这个程序中提取了CGridCtrl的文件来用。。。

 

 

现在来说说如何使用CGridCtrl:

 

我是创建的Dialog的程序。

 

我先创建了一个全局变量: CGridCtrl *m_pGrid;

 

然后创建对话框的消息 SHOW_WINDOW

 

在函数 onShowWindow函数中加入以下代码:

 

    CRect rect;
    CDC *pDC;
    GetClientRect(rect);
    m_pGrid = new CGridCtrl();
    rect.left = 130;
    rect.top = 80;
    rect.right = 900;
    rect.bottom = 400;
    m_pGrid->Create(rect,this,100);
    m_pGrid->SetEditable(false);
   
    try{
        m_pGrid->SetRowCount(50);
        m_pGrid->SetColumnCount(28);
        m_pGrid->SetFixedRowCount(3);
        m_pGrid->SetFixedColumnCount(0);
    }catch(CMemoryException *e){
        e->ReportError();
        e->Delete();
    }
   
    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 < 1){
               
                item.nFormat = DT_LEFT|DT_WORDBREAK;
                switch(col){
                case 1:
                    item.szText.Format("纱织");
                    break;
                case 2:
                    item.szText.Format("工艺编号");
                    break;
                case 3:
                    item.szText.Format("纱织成分");
                    break;
                case 4:
                    item.szText.Format("期初数");
                    break;
                case 10:
                    item.szText.Format("本月入库");
                    break;
                case 16:
                    item.szText.Format("本月出库");
                    break;
                case 22:
                    item.szText.Format("累计库存");
                    break;
                }//swtich结束
            }else{ //else
                item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
                item.szText.Format(" ");
               
            }//if结束
            if(row>1 && row<3){
                switch(col){
                case 4:
                    item.szText.Format("大货入库");
                    break;
                case 5:
                    item.szText.Format("一等入库");
                    break;
                case 6:
                    item.szText.Format("二等入库");
                    break;
                case 7:
                    item.szText.Format("中大零入库");
                    break;
                case 8:
                    item.szText.Format("次零入库");
                    break;
                case 9:
                    item.szText.Format("期初合计");
                    break;
                case 10:
                    item.szText.Format("大货入库");
                    break;
                case 11:
                    item.szText.Format("一等入库");
                    break;
                case 12:
                    item.szText.Format("二等入库");
                    break;
                case 13:
                    item.szText.Format("中大零入库");
                    break;
                case 14:
                    item.szText.Format("次零入库");
                    break;
                case 15:
                    item.szText.Format("入库合计");
                    break;
                case 16:
                    item.szText.Format("大货出库");
                    break;
                case 17:
                    item.szText.Format("一等出库");
                    break;
                case 18:
                    item.szText.Format("二等出库");
                    break;
                case 19:
                    item.szText.Format("中大零出库");
                    break;
                case 20:
                    item.szText.Format("次零出库");
                    break;
                case 21:
                    item.szText.Format("出库合计");
                    break;
                case 22:
                    item.szText.Format("大货库存");
                    break;
                case 23:
                    item.szText.Format("一等库存");
                    break;
                case 24:
                    item.szText.Format("二等库存");
                    break;
                case 25:
                    item.szText.Format("中大零库存");
                    break;
                case 26:
                    item.szText.Format("次零库存");
                    break;
                case 27:
                    item.szText.Format("库存合计");
                    break;
                }
            }
           
            m_pGrid->SetItem(&item);
        }//for结束
        m_pGrid->AutoSize();
        m_pGrid->SetRowHeight(0,3*m_pGrid->GetRowHeight(0)/2);
       
        pDC = m_pGrid->GetDC();
        CSize  cellSize;
        GV_ITEM item;
        item.mask = GVIF_TEXT|GVIF_FORMAT;
        item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
        CRect tmpRect;

/*

                下面的代码是用来合并单元格的

*/


        if(m_pGrid->GetSafeHwnd()){
            m_pGrid->SetFixedCellCombine(0,1,2,0);
            m_pGrid->SetFixedCellCombine(0,2,2,0);
            m_pGrid->SetFixedCellCombine(0,3,2,0);
            m_pGrid->SetFixedCellCombine(0,4,1,5);
            m_pGrid->SetFixedCellCombine(0,10,1,5);
            m_pGrid->SetFixedCellCombine(0,16,1,5);
            m_pGrid->SetFixedCellCombine(0,22,1,5);
        }

 

通过上面的代码就能创建一个CGridCtrl的图表~~

 

文章功底不好~请大家不要见谅~~有问题请下载面留言~~

效果图(部分):