How to display selected cells with a user-defined text and background color or use the highlight text and background color from the system settings

By default Objective Grid is only inverting selected cells with CDC::InvertRect, but by overriding the following two methods, you will be able to change the appearance of selected cells.

Please note that in earlier versions of this FAQ we mentioned to set m_bCacheCurrentCell = FALSE. This was wrong. Instead you should set m_bRefreshOnSetCurrentCell = TRUE; This also solves the problem that this solution did not work with listbox mode.

Example:

In your OnInitialUpdate routine, you should change the following attribute:

   // Force that current cell is redrawn when user 
// navigates through the grid
   m_bRefreshOnSetCurrentCell = TRUE;

Next, you should override the following two methods:

BOOL CGridSampleView::GetStyleRowCol(ROWCOL nRow, ROWCOL nCol, CGXStyle& style, GXModifyType mt, int nType)
{
    // Load stored style information of the cell
    BOOL bRet = CGXGridCore::GetStyleRowCol(nRow, nCol, style, mt, nType);
 
    // Check invert state for individual cell
    if (nType == 0 && GetInvertStateRowCol(nRow, nCol, GetParam()->GetRangeList()))
    {   
       style.SetInterior(::GetSysColor(COLOR_HIGHLIGHT));
       style.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
       bRet = TRUE;
    }
 
    return bRet;
}
 
void CGridSampleView::DrawInvertCell(CDC* /*pDC*/, ROWCOL nRow, ROWCOL nCol, CRect rectItem)
{
    // If DrawInvertCell has been called from OnDrawTopLeftBottomRight
    // m_nNestedDraw is greater 0. There is no invalidation of the rectange
    // necessary because the cell has already been drawn.
 
    if (m_nNestedDraw == 0)
    {
       // m_nNestedDraw equal to 0 means that PrepareChangeSelection,
       // PrepareClearSelection or UpdateSelectRange did call this method.
 
       CGXRange range;
       if (GetCoveredCellsRowCol(nRow, nCol, range))
          rectItem = CalcRectFromRowCol(range.top, range.left, range.bottom, range.right);
 
       InvalidateRect(&rectItem);
    }
}