在MFC GridCtrl中使用CGridCellCheck类根据GetCheck()状态做不同处理的实现

在项目中大量应用了MFC GridCtrl类做表格

在一个表格中给定了一个CGridCellCheck,需要根据选中不选中的状态,动态改变相关联的文本,经过摸索,使用两种消息组合来确定其状态改变。

 

1.声明一个bool型变量 m_checkflag,并初始化

2.对GridCtrl手动添加两个消息事件

      .h 中

             afx_msg void OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult);
             afx_msg void OnClick(NMHDR* pNMHDR, LRESULT* pResult);

      .cpp 中

             ON_NOTIFY(GVN_SELCHANGED, IDC_GRID, OnSelChanged)
             ON_NOTIFY(NM_CLICK, IDC_GRID, OnClick)

3.为消息添加代码

  #define CHECKCELL_CHECKFLAG 1 //gridcellcheck 网格使用SetItemData保存的ItemData void CGridCheckDemo::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult) { //NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNMHDR; //if(pItem->iRow <= 0 || pItem->iColumn <= 0) return; GV_DISPINFO *pgvDispInfo = (GV_DISPINFO *)pNMHDR; GV_ITEM *pgvItem = &pgvDispInfo->item; NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNMHDR; CGridCtrl *pGrid = m_pGridCtrl; if (pGrid == NULL || pGrid->GetSafeHwnd() == NULL) return; CGridCell *pCell = (CGridCell*)pGrid->GetCell(pItem->iRow, pItem->iColumn); if (pCell == NULL) return; LPARAM lParam = pGrid->GetItemData(pItem->iRow, pItem->iColumn); WPARAM wParam = 0; switch (lParam) { case CHECKCELL_CHECKFLAG: if (pCell != NULL && pCell->IsKindOf(RUNTIME_CLASS(CGridCellCheck))) { m_checkflag = ((CGridCellCheck*)pCell)->GetCheck(); } break; default: break; } } void CGridCheckDemo::OnClick(NMHDR* pNMHDR, LRESULT* pResult) { //NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNMHDR; //if(pItem->iRow <= 0 || pItem->iColumn <= 0) return; GV_DISPINFO *pgvDispInfo = (GV_DISPINFO *)pNMHDR; GV_ITEM *pgvItem = &pgvDispInfo->item; NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNMHDR; CGridCtrl *pGrid = m_pGridCtrl; if (pGrid == NULL || pGrid->GetSafeHwnd() == NULL) return; CGridCell *pCell = (CGridCell*)pGrid->GetCell(pItem->iRow, pItem->iColumn); if (pCell == NULL) return; LPARAM lParam = pGrid->GetItemData(pItem->iRow, pItem->iColumn); switch (lParam) { case CHECKCELL_CHECKFLAG: if (pCell != NULL && pCell->IsKindOf(RUNTIME_CLASS(CGridCellCheck))) { if (((CGridCellCheck*)pCell)->GetCheck() != m_checkflag) { m_checkflag= !m_checkflag; if (m_checkflag) { //... do something } else { //... do something } pGrid->Refresh(); } } break; default: break; } }

你可能感兴趣的:(在MFC GridCtrl中使用CGridCellCheck类根据GetCheck()状态做不同处理的实现)