VC中Microsoft FlexGrid控件的使用及合并单元格、可编辑操作(EDIT控件添加)

来源:互联网 发布:linux怎么改变当前路径 编辑:程序博客网 时间:2024/05/22 16:52

转自:http://www.cnblogs.com/jxnclyk/archive/2010/05/25/1743403.html

1. 给FlexGrid控件绑定成员变量:m_FlexGrid。

2. 设置行列数
              m_FlexGrid.SetCols(4);                  //设置列数
              m_FlexGrid.SetRows(6);                 //设置行数

3. 设置固定行数和固定列数
              m_FlexGrid.SetFixedRows(1);         //设置固定行数为1行
              m_FlexGrid.SetFixedCols(0);          //设置固定列数为0列

4. 设置列宽、行高
             m_FlexGrid.SetColWidth(1,1500);        //设置列号为1的列宽为1500
             m_FlexGrid.SetRowHeight(2,300);       //设置行号为2的行高为300

5. 设置文本对齐方式
             m_FlexGrid.SetColAlignment(1,4);        //设置列号为1的文本对齐方式

    参数对应描述为:
             0   单元内容顶部左对齐。
             1   单元内容中间左对齐。对字符串的缺省设置值。
             2   单元内容底部左对齐。
             3   单元内容顶部居中。
             4   单元内容中间居中。
             5   单元内容底部居中。
             6   单元内容顶部右对齐。
             7   单元内容中间右对齐。对数字的缺省设置值。
             8   单元内容底部右对齐。
              9   单元内容一般对齐方式。对字符串中间左对齐而对数值中间右对齐。

6. 设置表头和单元格内容
             m_FlexGrid.SetTextArray(0,"Head1");                 //设置表头(第1列)内容为“Head1”
             m_FlexGrid.SetTextMatrix(1,1,"Text");                //设置单元格(行号1列号1)内容为“Text”

7. 增加一行
             m_FlexGrid.AddItem("AddString",(COleVariant)(3L));           //在行号为3的位置增加一行

8. 合并单元格
             for(i=0;i<4;i++)
                     m_FlexGrid.SetTextMatrix(5,i,"合计");        //将行号为5、列号从0到4的单元格内容设为相同“合计”
             m_FlexGrid.SetMergeCells(2);                           //设置单元格合并方式(下述)
             m_FlexGrid.SetMergeRow(5,TRUE);                 //设置要合并的行

      SetMergeCells函数的参数值如下:
             0: flexMergeNever                              默认的选项,就是不合并
             1: flexMergeFree                                不规则的合并
             2: flexMergeRestrictRows                   行合并
             3: flexMergeRestrictColumns              列合并
             4: flexMergeRestrictBoth                     行,列都合并

     注意只有单元格内容相同时才能进行合并操作。

9. 制作可编辑的FlexGrid控件

    放置一个Edit控件,绑定成员变量为m_Edit,并设置初始状态为不可见。

    响应FlexGrid控件的Click事件:

 void CBlackBKDlg::OnClickMsflexgrid() {<span style="white-space:pre"></span>long lCol=m_FlexGrid.GetColSel();         //获取点击的行号<span style="white-space:pre"></span>long lRow=m_FlexGrid.GetRowSel();      //获取点击的列号<span style="white-space:pre"></span>if(lRow>m_FlexGrid.GetRows() || lRow==0)              //判断点击是否有效<span style="white-space:pre"></span>return;<span style="white-space:pre"></span>CRect rect;<span style="white-space:pre"></span>m_FlexGrid.GetWindowRect(&rect);                 //获取FlexGrid控件的窗口矩形<span style="white-space:pre"></span>ScreenToClient(&rect);                                    //转换为客户区矩形<span style="white-space:pre"></span>CDC* pDC=GetDC();<span style="white-space:pre"></span>//MSFlexGrid 控件的函数的长度单位是“缇(twips)”,需要将其转化为像素,1440 缇 = 1 英寸<span style="white-space:pre"></span>//计算象素点和缇的转换比例<span style="white-space:pre"></span>int nTwipsPerDotX=1440/pDC->GetDeviceCaps(LOGPIXELSX);<span style="white-space:pre"></span>int nTwipsPerDotY=1440/pDC->GetDeviceCaps(LOGPIXELSY);<span style="white-space:pre"></span>//计算选中格的左上角的坐标(象素为单位)<span style="white-space:pre"></span>long y = m_FlexGrid.GetRowPos(lRow)/nTwipsPerDotY;<span style="white-space:pre"></span>long x = m_FlexGrid.GetColPos(lCol)/nTwipsPerDotX;<span style="white-space:pre"></span>//计算选中格的尺寸(象素为单位)。加1是实际调试中,发现加1后效果更好<span style="white-space:pre"></span>long width = m_FlexGrid.GetColWidth(lCol)/nTwipsPerDotX+1;<span style="white-space:pre"></span>long height = m_FlexGrid.GetRowHeight(lRow)/nTwipsPerDotY+1;<span style="white-space:pre"></span>//形成选中个所在的矩形区域<span style="white-space:pre"></span>CRect rc(x,y,x+width,y+height);<span style="white-space:pre"></span>//转换成相对对话框的坐标<span style="white-space:pre"></span>rc.OffsetRect(rect.left+1,rect.top+1);<span style="white-space:pre"></span>CString strValue=m_FlexGrid.GetTextMatrix(lRow,lCol);           //获取单元格内容<span style="white-space:pre"></span>m_Edit.ShowWindow(SW_SHOW);                         <span style="white-space:pre"></span>//显示控件<span style="white-space:pre"></span>m_Edit.MoveWindow(rc);                                        //改变大小并移到选中格位置<span style="white-space:pre"></span>m_Edit.SetWindowText(strValue);                           //显示文本<span style="white-space:pre"></span>m_Edit.SetFocus();                                                 //获取焦点}

编辑完成释放焦点之后,将数据写回单元格。响应Edit控件的EN_KILLFOCUS事件:

           void CBlackBKDlg::OnKillfocusEditChange()             {                     CString strInput;                    GetDlgItemText(IDC_EDIT_CHANGE,strInput);      //获取输入框内容                     m_FlexGrid.SetText(strInput);                   //设置单元格内容                    m_Edit.ShowWindow(SW_HIDE);                           //隐藏输入框             }

  

0 0