To create a CListView:

I   -   The raw stuff:

   1.   Start Visual Studio if you didn't yet.
    a)  Press Ctrl+N to invoke the File->New dialog.

    b)  From the New dialog, in the Files tab, choose MFC AppWizard(exe).
	Type the File name as ListViewExo.
	In the Location box, specify the directory where you want to create
	your exo.
	After checking that the platform is specified as Win 32 (which is by
	default), press OK.

    c)  In MFC AppWizard Step 1 of 1, choose Single Document (SDI).

    d)  Accept all other defaults in Steps 2, 3, 4.

    e)  In MFC AppWizard Step 5, refuse the Comments.

    f)  In MFC AppWizard Step 6, base your CListViewExo on the CListView class.
	Press Enter.

    g)  The New Project Information presents a preamble to you to examine and
	accept or modify.
	Press Enter to accept.

   2.   In the Visual Studio interface, from the main menu, 
	click Build->Set Active Configuration...
	Double-click ListViewExo - Win32 Release.

   3.   Let's create the ListView resource.

    a)  From the main menu, click Insert->Resource...
	In the Resource dialog, double-click Bitmap.

    b)  From the WorkSpace, in the ResouceView tab, right-click on the
	IDB_BITMAP1 and choose Properties.
	Click the PushPin button on the Properties dialog to always have it on top.
	Change the identifier of the bitmap to IDB_LARGEIMAGE.
	Create a bitmap with dimensions X * 32; 32 represents the height
    	of each image, X = a * 32 where a represents the number of items or
    	columns that will be displayed, X represents the the total Width of
    	the bitmap including all the items.      
    	For this exercise, we will use a width of 320 and a height of 32.
	That gives us space for 10 images.

Large ListView Images
c) Create a small bitmap with dimensions X * 16 and call it
2. Let's create the resources. a) In your View class, declare two
CImageList items, one for each image type. Also declare a SetFunction to set the list view style at start up, and a GetFunction to seize the list view type when the user click a button on the toolbar. //}}AFX_MSG DECLARE_MESSAGE_MAP() protected: CImageList m_LargeImageList; CImageList m_SmallImageList; public: BOOL SetViewType(DWORD dwViewType); DWORD GetViewType(); b) In this application, we will use quite a few items, so let's define them first: #define NUM_COLS 8 #define NUM_ITEMS 10 static _TCHAR *_colLabel[NUM_COLS] = { _T("Country"), _T("Area sq. km"), _T("Population"), _T("Capital"), _T("Code"), _T("Budget $"), _T("Independance"), _T("Type") }; static int _colFmt[NUM_COLS] = { LVCFMT_LEFT, LVCFMT_RIGHT, LVCFMT_RIGHT, LVCFMT_LEFT, LVCFMT_CENTER, LVCFMT_CENTER, LVCFMT_CENTER, LVCFMT_CENTER }; static int _colWidth[NUM_COLS] = { 100, 80, 80, 80, 40, 100, 100, 90 }; static _TCHAR *_treeItem[NUM_ITEMS][NUM_COLS] = { _T("Netherlands"),_T("37330"), _T("15649729"), _T("Amsterdam"),_T("NL"),_T("107.2B"),_T("1579"), _T("Monarchy"), _T("U.S.A."), _T("9629091"),_T("267954764"),_T("Wash,D.C"), _T("US"),_T("1351T"), _T("04/07/1776"),_T("Fed. Rep"), _T("Cameroon"), _T("475440"), _T("14677510"), _T("Yaounde"), _T("CM"),_T("2.23B"), _T("01/01/1960"),_T("Republic"), _T("Senegal"), _T("196190"), _T("9403546"), _T("Dakar"), _T("SG"),_T("876M"), _T("04/04/1960"),_T("Republic"), _T("France"), _T("547440"), _T("58609285"), _T("Paris"), _T("FR"),_T("250B"), _T("486"), _T("Republic"), _T("Iraq"), _T("437072"), _T("22219289"), _T("Baghdad"), _T("IZ"),_T("N/A"), _T("30/10/1932"),_T("Republic"), _T("Thailand"), _T("514000"), _T("59450818"), _T("Bangkok"), _T("TH"),_T("28.4B"), _T("1238"), _T("Republic"), _T("Yemen"), _T("527970"), _T("13972477"), _T("Sanaa"), _T("YM"),_T("3B"), _T("22/05/1990"),_T("Republic"), _T("Bahamas"), _T("13940"), _T("275941"), _T("Nassau"), _T("BF"),_T("665M"), _T("10/07/1973"),_T("CommonWealth"), _T("Japan"), _T("377835"), _T("125732794"),_T("Tokyo"), _T("JA"),_T("528B"), _T("660BC"), _T("Const. Mon.") }; c) In your OnInitialUpdate() function, create the image lists that you had declared and define your items display: void CListVView::OnInitialUpdate() { CListView::OnInitialUpdate(); CListCtrl& ListCtrl = GetListCtrl(); m_LargeImageList.Create(IDB_LARGEIMAGES, 32, 1, RGB(192, 192, 192)); m_SmallImageList.Create(IDB_SMALLIMAGES, 16, 1, RGB(255, 255, 255)); ListCtrl.SetImageList(&m_LargeImageList, LVSIL_NORMAL); ListCtrl.SetImageList(&m_SmallImageList, LVSIL_SMALL); // insert columns int i, j; LV_COLUMN lvc; lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM; for(i = 0; i<NUM_COLS; i++) { lvc.iSubItem = i; lvc.pszText = _colLabel[i]; = _colWidth[i]; lvc.fmt = _colFmt[i]; ListCtrl.InsertColumn(i,&lvc); } // insert items LV_ITEM lvi; for(i = 0; i < NUM_ITEMS; i++) { lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE; lvi.iItem = i; lvi.iSubItem = 0; lvi.pszText = _treeItem[i][0]; lvi.iImage = i; lvi.stateMask = LVIS_STATEIMAGEMASK; lvi.state = INDEXTOSTATEIMAGEMASK(1); ListCtrl.InsertItem(&lvi); } // set item text for additional columns for(i = 0; i<NUM_ITEMS; i++) { for(j = 1; j<NUM_COLS; j++) { ListCtrl.SetItemText(i,j,_treeItem[i][j]); } } } d) Implement the functions you had defined earlier: ///////////////////////////////////////////////////////////////////////////// // CListVView message handlers BOOL CListVView::SetViewType(DWORD dwViewType) { return(ModifyStyle(LVS_TYPEMASK,dwViewType & LVS_TYPEMASK)); } DWORD CListVView::GetViewType() { return(GetStyle() & LVS_TYPEMASK); }
Isn't this image weird? Right now you have all the information you need about your items but you can't access them for a lack of means. Have you ever had a TV station where all the features can be set only with the remote control? That's why... 3. We need some buttoons to get different views/display of our items. a) Create four buttons on your toolbar as follows.
No View Buttons
Their identifiers, in order, are: ID_VIEW_LARGEICON, ID_VIEW_SMALLICON, ID_VIEW_LIST, ID_VIEW_DETAILS. b) Use ClassWizard to associate the
View and UpdateView functions for the toolbar buttons, and implement them like this: void CListViewExoView::OnViewSmallicon() { if (GetViewType() != LVS_SMALLICON) SetViewType(LVS_SMALLICON); } void CListViewExoView::OnViewLargeicon() { if (GetViewType() != LVS_ICON) SetViewType(LVS_ICON); } void CListViewExoView::OnViewList() { if (GetViewType() != LVS_LIST) SetViewType(LVS_LIST); } void CListViewExoView::OnViewDetails() { if ((GetViewType() != LVS_REPORT)) { if (GetViewType() != LVS_REPORT) SetViewType(LVS_REPORT); } } void CListViewExoView::OnUpdateViewSmallicon(CCmdUI* pCmdUI) { pCmdUI->SetCheck(GetViewType() == LVS_SMALLICON); } void CListViewExoView::OnUpdateViewLargeicon(CCmdUI* pCmdUI) { pCmdUI->SetCheck(GetViewType() == LVS_ICON); } void CListViewExoView::OnUpdateViewList(CCmdUI* pCmdUI) { pCmdUI->SetCheck(GetViewType() == LVS_LIST); } void CListViewExoView::OnUpdateViewDetails(CCmdUI* pCmdUI) { pCmdUI->SetCheck((GetViewType() == LVS_REPORT)/* && !GetFullRowSel()*/); }

Webmaster Copyright (c) 1999 FunctionX