抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

靖待的技术博客

小清新IT旅程 | 为中华之崛起而读书





  一个小demo——CBCGPGridCtrl网格控件增加行、删除行、上下移动行。


代码

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
BOOL CtestDlg::OnInitDialog()
{
CBCGPDialog::OnInitDialog();

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
CRect rect(0, 0, 400, 400);
m_pGridCtrl = new CBCGPGridCtrl;
m_pGridCtrl->Create(WS_CHILD, rect, this, 1);
m_pGridCtrl->ShowWindow(SW_SHOW);

m_pGridCtrl->InsertColumn(0, _T("name"), 40);
m_pGridCtrl->InsertColumn(1, _T("sex"), 40);

CBCGPGridRow* pRow = new CBCGPGridRow;
m_pGridCtrl->AddRow(pRow);
CBCGPGridItem* pNameItem = pRow->CreateItem(0, 0);
pRow->AddItem(pNameItem);
pNameItem->SetValue("Nancy");

CBCGPGridItem* pSexItem = pRow->CreateItem(0, 1);
pRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue("男");//默认值为“男”

return TRUE; // return TRUE unless you set the focus to a control
}

增加行

1
2
3
4
5
6
7
8
9
10
11
12
13
void CtestDlg::OnBnClickedBtnAdd()
{
CBCGPGridRow* pRow = new CBCGPGridRow;
m_pGridCtrl->AddRow(pRow);
int iRow = pRow->GetRowId();
CBCGPGridItem* pNameItem = pRow->CreateItem(iRow, 0);
pRow->AddItem(pNameItem);
CBCGPGridItem* pSexItem = pRow->CreateItem(iRow, 1);
pRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue("男");
}

删除行

1
2
3
4
5
6
7
8
9
10
11
12
13
void CtestDlg::OnBnClickedBtnDelete()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();
m_pGridCtrl->RemoveRow(iRow);
CBCGPGridRow* pPreRow = m_pGridCtrl->GetRow(iRow - 1);
m_pGridCtrl->AdjustLayout();
m_pGridCtrl->SetCurSel(pPreRow);
}

上移行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void CtestDlg::OnBnClickedBtnMoveUp()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();
CBCGPGridRow* pNewRow = new CBCGPGridRow;
m_pGridCtrl->InsertRowBefore(iRow - 1, pNewRow);
CBCGPGridItem* pNameItem = pNewRow->CreateItem(0, 0);
pNewRow->AddItem(pNameItem);
pNameItem->SetValue(pRow->GetItem(0)->GetValue());

CBCGPGridItem* pSexItem = pNewRow->CreateItem(0, 1);
pNewRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue(pRow->GetItem(1)->GetValue());

m_pGridCtrl->RemoveRow(iRow + 1);
m_pGridCtrl->AdjustLayout();

m_pGridCtrl->SetCurSel(iRow - 1);
}

下移行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void CtestDlg::OnBnClickedBtnMoveDown()
{
CBCGPGridRow* pRow = m_pGridCtrl->GetCurSel();
if (!pRow)
{
return;
}
int iRow = pRow->GetRowId();

CBCGPGridRow* pNewRow = new CBCGPGridRow;
m_pGridCtrl->InsertRowAfter(iRow+1, pNewRow);
CBCGPGridItem* pNameItem = pNewRow->CreateItem(0, 0);
pNewRow->AddItem(pNameItem);
pNameItem->SetValue(pRow->GetItem(0)->GetValue());

CBCGPGridItem* pSexItem = pNewRow->CreateItem(0, 1);
pNewRow->AddItem(pSexItem);
pSexItem->AddOption("男");
pSexItem->AddOption("女");
pSexItem->SetValue(pRow->GetItem(1)->GetValue());

m_pGridCtrl->RemoveRow(iRow);

m_pGridCtrl->AdjustLayout();

m_pGridCtrl->SetCurSel(iRow+1);
}

注意

  1. pRow可以有多种方式创建,可以先new CBCGPGridRow后再AddRow到网格控件对象m_pGridCtrl中。也可以直接 m_pGridCtrl->CreateRow();。不过CBCGPGridItem不可以直接new,只能通过 CBCGPGridItem* pNameItem = pRow->CreateItem(iRow, 0)来得到。

  2. m_pGridCtrl->RemoveRow(iRow);操作后若想继续选中现有某行,需要 m_pGridCtrl->AdjustLayout();来调整布局刷新网格,再调用SetCurSel实现选中操作。

  3. InsertRowBeforeInsertRowAfter的两个参数是:被插入行(在该行前或后插入新行),新插入行数据指针CBCGPGridRow* pRow

评论