First of all, you should use a CExtPropertyGridCtrl
-derived class:
class CMyPropertyGridCtrl : public CExtPropertyGridCtrl
{
public:
virtual bool OnPgcCreateGrids()
{
ASSERT_VALID( this );
try
{
/////////////////////////////////////////////////////////
// 1) CREATE CATEGORIZED GRID WINDOW
CMyCategorizedTreeGrid * pGridCategorized =
new CMyCategorizedTreeGrid( this );
pGridCategorized->m_bAutoDeleteWindow = true;
if( ! pGridCategorized->Create(
this,
__EXT_MFC_ID_PROPERTY_GRID_CATEGORIZED,
true
)
)
{
ASSERT( FALSE );
throw __EXT_MFC_ID_PROPERTY_GRID_CATEGORIZED;
}
/////////////////////////////////////////////////////////
// 2) INIT ONE ICON INSIDE THE CATEGORIZED GRID WINDOW
HICON hIcon = (HICON)::LoadImage(
::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON, 16, 16, 0 );
CExtCmdIcon icon;
icon.AssignFromHICON( hIcon, false );
pGridCategorized->m_nIconIndex =
pGridCategorized->GridIconInsert( &icon );
/////////////////////////////////////////////////////////
// 3) CREATE SORTED GRID WINDOW
CExtPropertyGridWndSorted * pGridSorted =
new CExtPropertyGridWndSorted( this );
pGridSorted->m_bAutoDeleteWindow = true;
if( ! pGridSorted->Create(
this,
__EXT_MFC_ID_PROPERTY_GRID_SORTED
)
)
{
ASSERT( FALSE );
throw __EXT_MFC_ID_PROPERTY_GRID_SORTED;
}
}
catch( ... )
{
return false;
}
return true;
}
};
The overridden
OnPgcCreateGrids()
virtual method is similar to the original one, but it creates the
CMyCategorizedTreeGrid
window instead of that created by the default
CExtPropertyGridWndCategorized
window. Besides, the method loads and inserts the icon into the categorized grid window. The
CMyCategorizedTreeGrid
window is required for adding icons to property category rows. Here is the implementation of this class:
class CMyCategorizedTreeGrid : public CExtPropertyGridWndCategorized
{
public:
INT m_nIconIndex;
CMyCategorizedTreeGrid()
: m_nIconIndex( -1 )
{
}
virtual HTREEITEM PropertyItemInsert(
CExtPropertyItem * pPropertyItem,
LONG nIndex = -1,
HTREEITEM htiParent = TVI_ROOT,
bool bRedraw = true
)
{
ASSERT_VALID( this );
HTREEITEM hTreeItem =
CExtPropertyGridWnd::PropertyItemInsert(
pPropertyItem, nIndex, htiParent, false );
if( hTreeItem != NULL && m_nIconIndex >= 0
&& pPropertyItem->IsKindOf( RUNTIME_CLASS( CExtPropertyCategory ) )
// && ANALYZE WHETHER THIS CATEGORY SHOULD HAVE AN ICON
)
{
CExtGridCell * pCell = ItemGetCell( 0, hTreeItem );
ASSERT_VALID( pCell );
pCell->IconIndexSet( m_nIconIndex );
}
if( bRedraw && hTreeItem != NULL )
{
LONG nRowNo = pGrid->ItemGetVisibleIndexOf( hTreeItem );
if( nRowNo >= 0 )
{
CRect rcInvalidate;
if( GridCellRectsGet( 0, nRowNo, 0, 0, NULL, &rcInvalidate ) )
{
CRect rcClient;
pGrid->GetClientRect( &rcClient );
rcInvalidate.left = rcClient.left;
rcInvalidate.right = rcClient.right;
InvalidateRect( &rcInvalidate );
}
}
}
return hTreeItem;
}
};