Set focus on a cell

I wrote a small piece of code that would help select individual cells in a
CListCtrl. This is not supported currently by the MFC.

When the user clicks on the cell in the CListCtrl it draws a rect. around
that cell indicating that is the cell selected by the user. This might not
be the best solution but a work around if anyone would like to use this.

//Individual cell selection in CLIstCtrl
CListCtrl::OnClick(...)
{
    int column;
    CRect m_rect;
    //the function below is provided in CListCtrl inPlace editing
    int index = GetRowColumnIndex(point, &column);
    if(index == -1)return;
    int offset = 0;
    for(int i = 0; i < column; i++)
      offset += GetColumnWidth(i);
    //Get the rectangle of the label and the icon
    GetItemRect(index, &m_rect, LVIR_BOUNDS);
    m_rect.left += offset + 4;
    //Get the columnWidth of the selected column
    m_rect.right = m_rect.left + GetColumnWidth(column);
    Update(index);
    CClientDC dc(this);    //this is the pointer of the current view
    dc.DrawFocusRect(m_rect);
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read