Changing Views

The preceding code produces a list view control that initially appears in details view. As yet, you have no way of changing views. In the LISTVIEW sample, you can do this by choosing a view from the Options menu, shown in Figure 3-8.

Figure 3-8.

The Options menu in the LISTVIEW sample.

Clicking an item on this menu generates a WM_COMMAND message. In an MFC application, you can handle this by adding a message map entry for the command (IDM_LARGEICON for large icon view), and you can change the view by setting the window style. To check the current view of the control, use the LVS_TYPEMASK constant. The current view can be LVS_ICON, LVS_SMALLICON, LVS_LIST, or LVS_REPORT.

void CMfclistView::OnLargeicon () 
{
DWORD dwStyle;

dwStyle = GetWindowLong (m_ListCtl.m_hWnd, GWL_STYLE);

if ((dwStyle & LVS_TYPEMASK) != LVS_ICON)
SetWindowLong (m_ListCtl.m_hWnd, GWL_STYLE,
(dwStyle & ~LVS_TYPEMASK) | LVS_ICON);
}