Without a special note,the listctrl has the default view style of report.

1. CListCtrl style

       LVS_ICON: for each item displayed great icon

       LVS_SMALLICON: for each item displayed on the icon

       LVS_LIST: a show with a small icon on the item

       LVS_REPORT: Show item details

       You can inspect the Windows Explorer, just like “View” tab with “icon, small icon, list details”

2. CListctrl style and expand style

LONG lStyle;
lStyle = GetWindowLong (m_list.m_hWnd, GWL_STYLE); // Get the current window style
lStyle &= ~ LVS_TYPEMASK; // Clear display
lStyle |= LVS_REPORT; // set style
SetWindowLong (m_list.m_hWnd, GWL_STYLE, lStyle); // set style
DWORD dwStyle = m_list.GetExtendedStyle ();
dwStyle |= LVS_EX_FULLROWSELECT; // highlight the selected full row
dwStyle |= LVS_EX_GRIDLINES; // grid lines (with the style of the report)
dwStyle |= LVS_EX_CHECKBOXES; // checkbox controls
m_list.SetExtendedStyle (dwStyle); // set expand style

Note: Please check MSDN for style details.

3. Insert data

m_list.InsertColumn (0, “ID”, LVCFMT_LEFT, 40); // insert column
m_list.InsertColumn (1, “NAME”, LVCFMT_LEFT, 50);
int nRow = m_list.InsertItem (0, “11 “);// Row
m_list.SetItemText (nRow, 1, “jacky “);// set of data

4. Always selected item
Check the style of Show selection always, or in the section 2  above set LVS_SHOWSELALWAYS style.

5. Check and uncheck a row

int nIndex = 0;
// Check
m_list.SetItemState (nIndex, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
// Uncheck
m_list.SetItemState (nIndex, 0, LVIS_SELECTED | LVIS_FOCUSED);

6. Get the checkbox status of all the rows

m_list.SetExtendedStyle(LVS_EX_CHECKBOXES);
CString str;
for(int i=0; i<nRowCount;i++)
{
   if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED || m_list.GetCheck(i))
   {
        str.Format(_T(“checkbox checked in row %d”), i);
        AfxMessageBox(str);
   }
}

7. Get the selected row index

Method 1:

CString str;
for(int i=0; i<nRowCount;i++)
{
    if( m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED )
    {
        str.Format(_T(“ROW %d selected.”), i);
        AfxMessageBox(str);
    }
}

Method 2:

POSITION pos = m_list.GetFirstSelectedItemPosition();
if (pos == NULL)
TRACE0(“No items were selected!\n”);
else
{
    while (pos)
    {
        int nItem = m_list.GetNextSelectedItem(pos);
        TRACE1(“Item %d was selected!\n”, nItem);
        // you could do your own processing on nItem here
    }
}

Tags: ,