FlexCell 是一款国产的网格、报表控件,其用法比较简单,特性也比较丰富。下面演示如何在一个单文档应用中使用 FlexCell。
1、新建工程
应用类型为 SDI(Single document),名称为 TF1。
2、添加 FlexCell 控件
选择菜单项“Project > Add to Project > Components and Controls”,弹出“Components and Controls Gallery”对话框。选择“Registered ActiveX Controls”文件夹。双击 FlexCell.Grid,弹出“Insert this components?”消息框。点【确定】按钮,弹出“Confirm Classes”对话框,点【OK】按钮。最后关闭“Components and Controls Gallery”对话框。
3、编程
3.1 包含头文件
打开 StdAfx.h,在 #endif // _AFX_NO_AFXCMN_SUPPORT 后添加:
#include "_grid.h"
#include "_cell.h"
3.2 在视图类中定义网格变量
打开 TF1View.h,在 // Attributes 后添加:
private:
C_Grid* m_pGrid;
3.3 构造与析构
CTF1View::CTF1View()
{
m_pGrid = NULL;
}
CTF1View::~CTF1View()
{
if (m_pGrid)
delete m_pGrid;
}
3.4 创建网格
如果视图类没有 OnInitialUpdate 函数,就需要覆盖这个虚函数。选择 Workspace 窗口中的 ClassView 页。右键单击 CTF1View,选择“Add Virtual Function”,弹出“New Virtual Override for class CTF1View”对话框。双击“New Virtual Functions”下的“OnInitialUpdate”,“OnInitialUpdate”会出现在右边的“Existing virtual function overrides”下。单击【Edit Existing】按钮,编辑代码。
void CTF1View::OnInitialUpdate()
{
CView::OnInitialUpdate();
if (m_pGrid == NULL)
{
// Create the Gridctrl object
m_pGrid = new C_Grid;
if (!m_pGrid) return;
// Create the Gridctrl window
CRect rect;
GetClientRect(rect);
if ( m_pGrid->Create(_T(""), WS_CHILD | WS_VISIBLE, rect, this, 120) )
{
// Register the FlexCell
m_pGrid->SetRegisterInformation(_T("***"), _T("***")); 注册码保密
m_pGrid->SetAutoRedraw(FALSE);
m_pGrid->SetRows(6);
m_pGrid->SetCols(8);
m_pGrid->SetDisplayFocusRect(FALSE);
//m_pGrid->SetExtendLastCol(TRUE);
// Column Headers
m_pGrid->Cell(0, 1).SetText(_T("Mon"));
m_pGrid->Cell(0, 2).SetText(_T("Tue"));
m_pGrid->Cell(0, 3).SetText(_T("Thu"));
m_pGrid->Cell(0, 4).SetText(_T("Wed"));
m_pGrid->Cell(0, 5).SetText(_T("Fri"));
m_pGrid->Cell(0, 6).SetText(_T("Sat"));
m_pGrid->Cell(0, 7).SetText(_T("Sun"));
// Populate items
for (int i = 0; i < 31; i++)
{
CString strCell;
strCell.Format(_T("%d"), i + 1);
m_pGrid->Cell(i / 7 + 1, i % 7 + 1).SetText(strCell);
}
m_pGrid->SetAutoRedraw(TRUE);
}
else
{
AfxMessageBox(_T("m_pGrid->Create() failed!"));
}
}
}
3.5 大小变化
如果视图类没有 OnSize 函数,就需要添加处理函数。在 ClassView 页中,右键单击 CTF1View,选择“Add Windows Message Handler”,弹出“New Windows Message and Event Handlers for class CTF1View”对话框。双击“New Windows messages/events”下的“WM_SIZE”,“WM_SIZE”会出现在右边的“Existing message/event handlers”下。单击【Edit Existing】按钮,编辑代码。
void CTF1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_pGrid->GetSafeHwnd())
{
CRect rect;
GetClientRect(rect);
m_pGrid->MoveWindow(rect);
m_pGrid->UpdateWindow();
m_pGrid->ShowWindow(SW_SHOW);
}
}
4、运行效果
1、新建工程
应用类型为 SDI(Single document),名称为 TF1。
2、添加 FlexCell 控件
选择菜单项“Project > Add to Project > Components and Controls”,弹出“Components and Controls Gallery”对话框。选择“Registered ActiveX Controls”文件夹。双击 FlexCell.Grid,弹出“Insert this components?”消息框。点【确定】按钮,弹出“Confirm Classes”对话框,点【OK】按钮。最后关闭“Components and Controls Gallery”对话框。
3、编程
3.1 包含头文件
打开 StdAfx.h,在 #endif // _AFX_NO_AFXCMN_SUPPORT 后添加:
#include "_grid.h"
#include "_cell.h"
3.2 在视图类中定义网格变量
打开 TF1View.h,在 // Attributes 后添加:
private:
C_Grid* m_pGrid;
3.3 构造与析构
CTF1View::CTF1View()
{
m_pGrid = NULL;
}
CTF1View::~CTF1View()
{
if (m_pGrid)
delete m_pGrid;
}
3.4 创建网格
如果视图类没有 OnInitialUpdate 函数,就需要覆盖这个虚函数。选择 Workspace 窗口中的 ClassView 页。右键单击 CTF1View,选择“Add Virtual Function”,弹出“New Virtual Override for class CTF1View”对话框。双击“New Virtual Functions”下的“OnInitialUpdate”,“OnInitialUpdate”会出现在右边的“Existing virtual function overrides”下。单击【Edit Existing】按钮,编辑代码。
void CTF1View::OnInitialUpdate()
{
CView::OnInitialUpdate();
if (m_pGrid == NULL)
{
// Create the Gridctrl object
m_pGrid = new C_Grid;
if (!m_pGrid) return;
// Create the Gridctrl window
CRect rect;
GetClientRect(rect);
if ( m_pGrid->Create(_T(""), WS_CHILD | WS_VISIBLE, rect, this, 120) )
{
// Register the FlexCell
m_pGrid->SetRegisterInformation(_T("***"), _T("***")); 注册码保密
m_pGrid->SetAutoRedraw(FALSE);
m_pGrid->SetRows(6);
m_pGrid->SetCols(8);
m_pGrid->SetDisplayFocusRect(FALSE);
//m_pGrid->SetExtendLastCol(TRUE);
// Column Headers
m_pGrid->Cell(0, 1).SetText(_T("Mon"));
m_pGrid->Cell(0, 2).SetText(_T("Tue"));
m_pGrid->Cell(0, 3).SetText(_T("Thu"));
m_pGrid->Cell(0, 4).SetText(_T("Wed"));
m_pGrid->Cell(0, 5).SetText(_T("Fri"));
m_pGrid->Cell(0, 6).SetText(_T("Sat"));
m_pGrid->Cell(0, 7).SetText(_T("Sun"));
// Populate items
for (int i = 0; i < 31; i++)
{
CString strCell;
strCell.Format(_T("%d"), i + 1);
m_pGrid->Cell(i / 7 + 1, i % 7 + 1).SetText(strCell);
}
m_pGrid->SetAutoRedraw(TRUE);
}
else
{
AfxMessageBox(_T("m_pGrid->Create() failed!"));
}
}
}
3.5 大小变化
如果视图类没有 OnSize 函数,就需要添加处理函数。在 ClassView 页中,右键单击 CTF1View,选择“Add Windows Message Handler”,弹出“New Windows Message and Event Handlers for class CTF1View”对话框。双击“New Windows messages/events”下的“WM_SIZE”,“WM_SIZE”会出现在右边的“Existing message/event handlers”下。单击【Edit Existing】按钮,编辑代码。
void CTF1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (m_pGrid->GetSafeHwnd())
{
CRect rect;
GetClientRect(rect);
m_pGrid->MoveWindow(rect);
m_pGrid->UpdateWindow();
m_pGrid->ShowWindow(SW_SHOW);
}
}
4、运行效果
“Ctrl + F5”运行程序,效果如图