MFC FlexGrid控件的使用示例

个人博客地址 www.tmbcode.com

MFC FlexGrid控件的使用

一、创建一个FlexGrid

        MFC FlexGrid控件的使用示例_第1张图片

 

二、为FlexGrid绑定一个变量

 

        MFC FlexGrid控件的使用示例_第2张图片

 

三、编写代码设置属性

 

         //这个函数是设置每个列的宽度,这里一个六列。

void CPACKView::SetRowCol(int rows,int cols)

{

         m_flex.SetRows(rows);

         m_flex.SetCols(cols);

        

         m_flex.SetColWidth(0,320);//0代表第0列 320代表宽度为320像素

         m_flex.SetColWidth(1,2100);

         m_flex.SetColWidth(2,2100);

         m_flex.SetColWidth(3,1750);

         m_flex.SetColWidth(4,1750);

         m_flex.SetColWidth(5,1750);

 

         //reset current rowand col

         this->m_cur_row=1;   // m_cur_row 和m_cur_col是int型,表示当前坐标

         this->m_cur_col=1;

}

 

//设置数据居中和每一列的名字

void CPACKView::SetNumText(int rows)

{

         //设置FLEX居中

         for(introw=0;row

         {

                   m_flex.SetRow(row);

                   m_flex.SetCol(1);

                   m_flex.SetCellAlignment(4);

                  

                   m_flex.SetRow(row);

                   m_flex.SetCol(2);

                   m_flex.SetCellAlignment(4);

                  

                   m_flex.SetRow(row);

                   m_flex.SetCol(3);

                   m_flex.SetCellAlignment(4);

 

                   m_flex.SetRow(row);

                   m_flex.SetCol(4);

                   m_flex.SetCellAlignment(4);

 

                   m_flex.SetRow(row);

                   m_flex.SetCol(5);

                   m_flex.SetCellAlignment(4);

         }

        

         //设置序列号

         CString strT;

         for(intcount=1;count

         {

                   strT.Format("%d",count);

                   m_flex.SetTextMatrix(count,0,strT);

         }

        

         m_flex.SetTextMatrix(0,1,"姓名");

         m_flex.SetTextMatrix(0,2,"学号");

         m_flex.SetTextMatrix(0,3,"班级");

         m_flex.SetTextMatrix(0,4,"性别");

         m_flex.SetTextMatrix(0,5,"联系方式");

}

 

四、添加数据

 

         //m_Name、m_Num、m_Class、m_Sex、m_Phone都是CStringArray类型

//设置FLEX格数

         this->SetRowCol(m_Name.GetSize()+1,6);//添加数据前先调用上面两个函数初始化一下

         this->SetNumText(m_Name.GetSize()+1);

 

         for(int i=0;i

         {

                   m_flex.SetTextMatrix(i+1,1,m_Name.GetAt(i));

 

                  m_flex.SetTextMatrix(i+1,2,m_Num.GetAt(i));

 

                  m_flex.SetTextMatrix(i+1,3,m_Class.GetAt(i));

 

                   m_flex.SetTextMatrix(i+1,4,m_Sex.GetAt(i));

                  

                   m_flex.SetTextMatrix(i+1,5,m_Phone.GetAt(i));

         }

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(MFC FlexGrid控件的使用示例)