GridCtrl使用详解
来源:互联网 发布:淘宝浏览单平台app 编辑:程序博客网 时间:2024/05/22 15:52
正在学习中,所以就转来了,省的总去找了。算是保存~
在单文档中的使用方法
步骤一 初始化
在CView类的.h头文件中包含文件:
#include "Gridctrl.h"
并且手写加入如下的成员函数:
CGridCtrl * m_pGridCtrl;
步骤二 构造与析构
构造函数中:
m_pGridCtrl = NULL;
析构函数中:
if(m_pGridCtrl)
delete m_pGridCtrl;
步骤三 如果需要打印功能的话添加同名打印函数代码
在CView类的OnBeginPrinting()函数中添加如下代码:
if(m_pGridCtrl)
m_pGridCtrl->OnBeginPrinting(pDC,pInfo); //简单吧,这就是类的好处
其它两个打印函数也一样的做法.
步骤四 在OnInitaUpdate()函数中或者你自己添加的要显示Grid的消息函数中如下初始化:
//创建非模式对话框
CDlg *dlg;
dlg=new CDlg();
dlg->Create(IDD_Dlg,this);
//初始化GridCtrl控件
if(m_pGridCtrl!=NULL)
{
delete m_pGridCtrl;
m_pGridCtrl=NULL;
}
if (m_pGridCtrl == NULL)
{
// Create the Gridctrl object
m_pGridCtrl = new CGridCtrl;
if (!m_pGridCtrl) return 0;
// Create the Gridctrl window
CRect rect;
GetClientRect(rect);
m_pGridCtrl->Create(rect, this, 100);
// fill it up with stuff
m_pGridCtrl->SetEditable(false);
m_pGridCtrl->SetTextBkColor(RGB(0xFF, 0xFF, 0xE0)); //黄色背景
m_pGridCtrl->EnableDragAndDrop(false);
try {
m_pGridCtrl->SetRowCount(k); //设置行数为k行
m_pGridCtrl->SetColumnCount(4); //k列
m_pGridCtrl->SetFixedRowCount(1); //标题行为一行
m_pGridCtrl->SetFixedColumnCount(1); //同上
}
catch (CMemoryException* e)
{
e->ReportError();
e->Delete();
return 0;
}
//填充列标题
int row=0;
for(int col=0;col<4;col++)
{
GV_ITEM Item;
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = row;
Item.col = col;
if(col==0){
Item.nFormat = DT_CENTER|DT_WORDBREAK;
Item.strText.Format(_T("【类别】"),col);
}
else if(col==1){
Item.nFormat = DT_LEFT|DT_WORDBREAK;
Item.strText.Format(_T("第一列"),col);
}
else if(col==2){
Item.nFormat = DT_LEFT|DT_WORDBREAK;
Item.strText.Format(_T("第二列"),col);
}
m_pGridCtrl->SetItem(&Item);
}
// fill rows/cols with text
for (row = 1; row < k; row++)
for (col = 0; col < h; col++)
{
GV_ITEM Item;
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = row;
Item.col = col;
if (col < 1) { //行标题头
Item.nFormat = DT_CENTER|DT_VCENTER
|DT_SINGLELINE|DT_END_ELLIPSIS
|DT_NOPREFIX;
Item.strText.Format(_T("%d"),row);
}
else if(col==1){ //第一列的值
Item.nFormat = DT_CENTER|DT_VCENTER
|DT_SINGLELINE|DT_END_ELLIPSIS
|DT_NOPREFIX;
str="aa";
Item.strText.Format(_T("%s"),str);
}else if(col==2){ //第二列第值
Item.nFormat = DT_CENTER|DT_VCENTER
|DT_SINGLELINE|DT_END_ELLIPSIS
|DT_NOPREFIX;
CString str;
str="bb";
Item.strText.Format(_T("%s"),str);
}
m_pGridCtrl->SetItem(&Item);
}
m_pGridCtrl->AutoSize();
//--------------设置行列距------------------
for(int a=1;a<m;a++)
m_pGridCtrl->SetRowHeight(a,21); //设置各行高
m_pGridCtrl->SetRowHeight(0,24); //设置0行高
m_pGridCtrl->SetColumnWidth(1,110); //设置2列宽
m_pGridCtrl->SetColumnWidth(2,160); //设置3列宽
m_pGridCtrl->SetColumnWidth(3,100); //设置4列宽
}
上例取自实际工程,稍有修改!
部分注释:
void SetVirtualMode(TRUE) //设为虚模式
BOOL SetRowCount(int nRows) //设置总的行数。
BOOL SetFixedRowCount(int nFixedRows = 1)//设置固定的行数据
BOOL SetColumnCount(int nCols) //设置列数
BOOL SetFixedColumnCount(int nFixedCols = 1)//设置固定的列数
步骤五: 添加WM_SIZE消息,调整控件的界面占屏幕大小
if(m_pGridCtrl->GetSafeHWnd())
{
CRect rect;
GetClientRect(rect);
m_pGridCtrl->MoveWindow(rect);
}
在对话框中的使用方法
步骤一 创建数据显示表格对话框
在资源管理器中新创建一个对话框,假设为CDlgTestReportBox。 从工具箱中加入Custom Control,就是人头像的那个,将其区域拉伸至要显示数据表格的大小,充满整个对话框。
在CDlgTestReportBox类的头文件中:
#include "GridCtrl.h"
再定义成员变量:
CGridCtrl* m_pGrid;
添加OnShowWindow()消息处理函数如下:
void CDlgTestReportBox::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
// TODO: Add your message handler code here
if(m_pGrid!=NULL)
{
delete m_pGrid;
m_pGrid=NULL;
}
if(m_pGrid==NULL)
{
m_pGrid=new CGridCtrl;
CRect rect;
GetDlgItem(IDC_ReportAera)->GetWindowRect(rect); //得到显示区域
ScreenToClient(&rect);
m_pGrid->Create(rect,this,100);
m_pGrid->SetEditable(false);
m_pGrid->SetTextBkColor(RGB(0xFF, 0xFF, 0xE0)); //黄色背景
try
{
m_pGrid->SetRowCount(10); //初始为10行
m_pGrid->SetColumnCount(11); //初始化为11列
m_pGrid->SetFixedRowCount(1); //表头为一行
m_pGrid->SetFixedColumnCount(1); //表头为一列
}
catch (CMemoryException* e)
{
e->ReportError();
e->Delete();
// return FALSE;
}
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==0&&col==0) //第(0,0)格
{
Item.nFormat = DT_CENTER|DT_WORDBREAK;
Item.szText.Format(_T("报表显示"),col);
}
else if (row < 1) //设置0行表头显示
{
Item.nFormat = DT_CENTER|DT_WORDBREAK;
Item.szText.Format(_T(" 项目%d"),col);
}
else if (col < 1) //设置0列表头显示
{
if(row< m_pGrid->GetRowCount()-4)
{
Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
Item.szText.Format(_T("第%d次"),row);
}
}
else
{
Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
Item.szText.Format(_T(""),2);
}
m_pGrid->SetItem(&Item);
}
m_pGrid->Invalidate();
}
//--------------设置行列距------------------
for(int a=0;aGetRowCount();a++)
m_pGrid->SetRowHeight(a,16); //设置各行高
m_pGrid->SetColumnWidth(0,58); //设置0列宽
for(int b=1;bGetColumnCount();b++)
m_pGrid->SetColumnWidth(b,59); //设置各列宽
}
步骤二 嵌入上面的对话框 显示数据
在你需要显示数据的对话框上的头文件中,假设为CDlgTest,加入
#include "GridCtrl.h"
CDlgTestReportBox* m_pTestReportBox;
将数据显示对话框放入你的对话框相应位置上,在CDlgTest::OnInitDialog() 中:
if(!m_pTestReportBox)
{
m_pTestReportBox=new CDlgTestReportBox(this);
}
m_pTestReportBox->Create(IDD_DlgTestReportBox,this);
//定义区域变量
CRect rectDraw;
GetDlgItem(IDC_AeraReport)->GetWindowRect(rectDraw);
ScreenToClient(&rectDraw); //动态测试数据显示区域rectDraw
//将对应的对话框放到指定区域
m_pTestReportBox->MoveWindow(rectDraw);
m_pTestReportBox->ShowWindow(SW_SHOW);
自定义填充数据的函数:CDlgTest::FillGrid() 如下:
CGridCtrl* pGrid=m_pTestReportBox->m_pGrid;
for (int row = pGrid->GetRowCount()-1; row >= pGrid->GetRowCount()-3; row--)
{
for (int col = 1; col <= pGrid->GetColumnCount(); col++)
{
GV_ITEM Item;
Item.mask = GVIF_TEXT|GVIF_FORMAT;
Item.row = row;
Item.col = col;
if(row==pGrid->GetRowCount()-3&&col>0) //平均值
{
if(col==10){
Item.nFormat = DT_CENTER|DT_WORDBREAK;
Item.szText.Format(_T(" %6.2f "),avjch);
}
else{
Item.nFormat = DT_CENTER|DT_WORDBREAK;
Item.szText.Format(_T(" %6.2f "),av[col-1]);
}
}
pGrid->SetItem(&Item); //提交数据
if(row==0||col==0)
{
COLORREF clr = RGB(0, 0, 0);
pGrid->SetItemBkColour(row, col, clr);
pGrid->SetItemFgColour(row, col, RGB(255,0,0));
}
}//循环结束
pGrid->Invalidate();
}
转载地址http://www.cppblog.com/xbgs/archive/2006/06/23/8936.html
- GridCtrl使用详解
- GridCtrl使用详解 收藏
- GridCtrl使用详解(转)
- GridCtrl使用详解(转)
- GridCtrl使用详解
- GridCtrl使用详解
- GridCtrl使用详解
- GridCtrl使用详解
- GridCtrl控件的使用
- 关于使用gridctrl
- GridCtrl 的使用
- GridCtrl成员函数、变量详解
- GridCtrl 在 VS2010中使用
- GRIDCTRL在对话框中的使用
- GridCtrl 控件在VS2005 中使用 日志。
- GridCtrl 使用指南
- 在MFC GridCtrl中使用CGridCellCheck类根据GetCheck()状态做不同处理的实现
- 在MFC GridCtrl中使用CGridCellCheck类根据GetCheck()状态做不同处理的实现
- 使用VC2005编译真正的静态Qt程序
- qrcode二维码学习笔记之一
- 关于性能测试中一些计算方法(throughput, active user...)
- Visual Studio 2005的版本情况和新特征详细介绍
- 说明
- GridCtrl使用详解
- dwr 实现页面数据加载等待状态
- dsfdsaf
- ArcGis For Silverlight API,地图显示Gis,绘制点,线,绘制图等(四)--动态绘制点
- Qt 类中 用QSettings类读配置文件
- makefile configure(转)
- drawRGB在N7370上的问题
- qlServer2005Winform中的数据库连接字符串一般放入到AppConfig文件中以便于管理
- ArcGIS 9.3文件破解方法