void CGridReportCtrl::SetDataSource(long hwnd) 
{
    char lpClassName[255];  
    //获取类名  
    if (::GetClassName((HWND)hwnd, lpClassName, 255))
    {
        CString strClassName;
        strClassName+=lpClassName;
        if(strClassName == "MSFlexGridWndClass")
        {
            CMSFlexGrid * m_pMSFlexGrid = (CMSFlexGrid *)CMSFlexGrid::FromHandle((HWND)hwnd);
            if(m_pMSFlexGrid !=NULL)
            {
                long row = m_pMSFlexGrid->GetRows();
                
                if (row <= 0) return;
                
                long col = m_pMSFlexGrid->GetCols();
                if (col <= 0) return ;
                CString str1;
                str1.Format("%d,%d",row,col);
                AfxMessageBox(str1);
                
                
                CString strText;                for(int i=0;i<row;i++)
                    for(int j=0;j<col;j++)
                {
                    strText=m_pMSFlexGrid->GetTextMatrix(i,j);
                                    }
                
            }        }
    }    
}
这段代码如果不在控件中,和MSFlexGrid在一个程序里,运行结果是对的,但是放在控件中,行列值都不对,表格里的内容也得不到不知道哪里有问题?

解决方案 »

  1.   

    CMSFlexGrid m_grid;
    CGridReport m_rp;
    CString s;
    m_grid.SetRows(20);
    m_grid.SetCols(11);


    for (int a=0;a<20;a++)
    {
    for (int b=0;b<11;b++)
    {
    s.Format("Row %d, Col %d",a,b);
    m_grid.SetTextMatrix(a,b,s);
    }
    }
    unsigned long color[]={0x00ff0000,0x0000ff00,0x000000ff,0x00cc00ff,0x0000ccff,0x00ffffcc,0x002000aa,0x00ffccff,0x0020aab0,0x00b53589}; m_grid.SetRow(1);
    for (a=1;a<11;a++)
    {
    m_grid.SetCol(a);
    m_grid.SetCellBackColor(color[a-1]);
    m_grid.SetCellForeColor(color[10-a]);

    }//-----------------------------------------------------------------------------
    测试
    m_rp.SetDataSource((long)m_grid.GetHWnd());