LISTVIEW ON A DIALOG

 

-   Start Visual C++ if you didn't yet.
1/      Let's create the preliminary display
a)  Press Ctrl+N (to invoke File->New ).
b) In the New property sheets, from the Projects tab, click MFC AppWizard (exe) (normally, this (small) project would have been better written as a Win32 application to save space, but...)
In the Project name, type a name, like: List. Check/specify the directory in the Location edit box.
Press Enter.
c)   In the MFC AppWizard - Step 1, choose Dialog based and press Enter.
d)  In the MFC AppWizard - Step 2 of 4, in the title box, type: A CListCtrl Dialog Program. Press Enter.
e) In the MFC AppWizard - Step 3 of 4, refuse the comments and press Enter.
d)  In the MFC AppWizard - Step 4 of 4, press Enter (there is nothing significant to change there).
f)  In the New Project Information dialog, press Enter.
2/  Now in the Visual Studio.
 a) When you get in the Visual Studio, you find that AppWiard has provided a default static line "TODO: blah blah blah" for you. Click that line and press Del on the keyboard. Also, click the Cancel button on the dialog and press Del from the keyboard (for this exercise, we will not need that particular button, since this is just a demonstration app, we will dismiss our dialog with the OK button, which we could even have deleted also.
   b)  From the Controls toolbox, choose the List Control and drop it in your
	dialog.

    c)  Right-click anywhere in the new new list control on the dialog and
 	choose Properties.
	In the Properties dialog, change the identifier of the list control to
	IDC_LIST.
	In the Styles tab, from the View combo box, specify the default view as
 	Report. Press Enter.

  3/    Press Ctrl+W (to invoke ClassWizard).

    a)  From the Member Variables, make sure the Class name is specified as 
	CListDlg.
	Double-click IDC_LIST to associate a member variable with that control.
	Specify the Member variable name as m_List. Press Enter 2 times.

    b)  Let's create the headers for our app.
	In the Workspace, click the ClassView. Expand the List classes project
	if it's not expanded yet.
	Expand the CListDlg class and double-click the OnInitDialog() function.
	Under the // TODO: Add extra initialization here, create each header.
        Remember that we already have an object (m_List) of kind CListCtrl. 
	When we create a particular header, we will specify the following
	attributes:

	// TODO: Add extra initialization here
	m_List.InsertColumn(	// Ask Mfc to create/insert a column
		0,		// This is the rank/order of this 
				//		particular item
		"Name",		// The caption we want for this header
		LVCFMT_LEFT,	// The relative position we want the 
				// items under this header to have
		100);		// The width we want for the items under 
				// this header
	m_List.InsertColumn(1, "Profession", LVCFMT_CENTER, 80);
	m_List.InsertColumn(2, "Fav. Sport", LVCFMT_LEFT, 100);
	m_List.InsertColumn(3, "Hobby", LVCFMT_LEFT, 80);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

    c)  If you execute your app now, you should have your headers ready.

    d)  Let's provide some items to display under the headers:

	int nItem;		// This integer will be used to identify the
				// header item we are dealing with.
	// Give a name/caption to an item to display under the first header
	nItem = m_List.InsertItem(0, "Sandra");
	// Create a caption for the corresponding headers.
	m_List.SetItemText(nItem, 1, "Singer");
	m_List.SetItemText(nItem, 2, "HandBall");
	m_List.SetItemText(nItem, 3, "Beach");

	return TRUE;  // return TRUE  unless you set the focus to a control
}
	In the same way, create the other items. When you finish, this section
	of the OnInitDialog function might look like this:

	int nItem;

	nItem = m_List.InsertItem(0, "Sandra C. Anschwitz");
	m_List.SetItemText(nItem, 1, "Singer");
	m_List.SetItemText(nItem, 2, "HandBall");
	m_List.SetItemText(nItem, 3, "Beach");

	nItem = m_List.InsertItem(0, "Roger A. Miller");
	m_List.SetItemText(nItem, 1, "FootBaller");
	m_List.SetItemText(nItem, 2, "Tennis");
	m_List.SetItemText(nItem, 3, "Teaching");

	nItem = m_List.InsertItem(0, "Marie-Julie W. Gross");
	m_List.SetItemText(nItem, 1, "Student");
	m_List.SetItemText(nItem, 2, "Boxing");
	m_List.SetItemText(nItem, 3, "Programming");

	nItem = m_List.InsertItem(0, "Ella Pius Roger");
	m_List.SetItemText(nItem, 1, "Architect");
	m_List.SetItemText(nItem, 2, "Ping-Pong");
	m_List.SetItemText(nItem, 3, "Songo");

	return TRUE;  // return TRUE  unless you set the focus to a control
}
  4/	Let's provide the ability to display items in different styles

    a)  Create four buttons on your dialog, identified and captioned as follows:
Button ID:
Caption
IDC_LARGEICON Lar&ge Icons
IDC_SMALLICON Sm&all Icons
IDC_LISTBTN &List
IDC_DETAILS &Details

        b)   We need two functions (or just one if you want) to set/control the view styles  
              imposed  to the buttons. In the ListDlg class, declare two functions as follows, in
              the protected implementation section.

DWORD GetViewType();
BOOL SetViewType(DWORD dwViewType);

Then implemented those functions as follows:

DWORD CListDlg::GetViewType()
{
   
     return (GetStyle() & LVS_TYPEMASK);
}

BOOL CListDlg::SetViewType(DWORD dwViewType)
{
   
 
   DWORD dwCurType;
   
     HWND hWnd;
   
    
hWnd = m_List;
       
GetSafeHwnd();
   
    
dwCurType = ::GetWindowLong(hWnd, GWL_STYLE);
   
    
dwCurType &= ~LVS_TYPEMASK;
   
    
dwViewType |= dwCurType;
   
    
::SetWindowLong(hWnd, GWL_STYLE, dwViewType);
   
    
return TRUE;
}

        c) Press Ctrl+W to invoke ClassWizard. In the Message Maps, make sure the Class name is specified as CListDlg. In the Object IDs list box, choose IDC_LARGEICON, in the Messages list box, double-click BN_CLICKED and accept the suggested name for the function. Do the same thing for the other three buttons.

        d) To implement these four functions, except for the large icon, you first check/validate the current view. if the current view doesn't correspond to the view associated with this button, change it accordingly.
        When you finish, this section of your code should look like:

void CListDlg::OnLargeicon()
{
        SetViewType(LVS_ICON);
}

void CListDlg::OnSmalllicon()
{
        if( GetViewType() != LVS_SMALLICON) SetViewType(LVS_SMALLICON); }

void CListDlg::OnListbtn()
{
        if( GetViewType() != LVS_LIST) SetViewType(LVS_LIST);
}

void CListDlg::OnDetail()
{
        if( GetViewType() != LVS_REPORT) SetViewType(LVS_REPORT);
}

    5/ The only things missing now are the pictures to associate with the different
        displays. I know you are wondering why I left them for the end, I think it's because I
        wanted to go step by step and show you the difference and independence of  
       different parts of the program, in case you don't need images. So, by now, you know
       you don't need images to make a ListView functionnal. They only make the app look 
       cute. You can use 1, two, or n images, it is completely up to you.  
       CListCtrl/CListView and its sister CTreeCtrl/CTreeView are very flexible classes. 
        a) Create two bitmaps, one bitmap will be used for small displays, the other for large
            displays. The image(s) that will be used for the small displays should have a height
            of 16 pixels, the other a height of 32 pixels. The dimensions we are going to
            specify here are valid for this exo. i.Press Ctrl+R to invoke the Resource dialog.
            Click Bitmap and press Enter. Press Alt+Enter to access the Properties dialog.
            Change the identifier of the bitmap as IDB_LARGEIMG and specify its Width
           160 and its Height as 32.
 

                                                        
Design your bitmap as you want, just remember to make each image with a width of 31. 
Create a second bitmap identified as IDB_SMALLIMG with a width of 80 and a height of 16. And design it to your tast.
          b) Create two CImageList objects in your class dialog (you can create them in your
              protected implementation):  

CImageList m_SmallImg; 
CImageList m_LargeImg;   

          c) Create/associate these CImageList for your bitmaps, in the OnInitDialog() 
              function. Then use them for the display controlled by the CListCtrl:  

// TODO: Add extra initialization here
        m_SmallImg.Create(IDB_SMALLIMG, 16, 1, RGB(255, 255, 255));         
        m_LargeImg.Create(IDB_LARGEIMG, 32, 1, RGB(255, 255, 245)); 
        m_List.SetImageList(&m_SmallImg, LVSIL_SMALL); 
        m_List.SetImageList(&m_LargeImg, LVSIL_NORMAL);

        d) Finally, set an image for each item displayed, remember that the images are
            counted as: 0, 1, etc. For example, change the first like this(you only specify the
            number of the image when you create the item): 
            nItem = m_List.InsertItem(0, "Sandra C. Anschwitz", 0);
           
Do the same for the other items. When you finish, you might have a section that 
            looks like this:

            // TODO: Add extra initialization here
            m_SmallImg.Create(IDB_SMALLIMG, 16, 1, RGB(255, 255, 255));    
            m_LargeImg.Create(IDB_LARGEIMG, 32, 1, RGB(255, 255, 245));

            m_List.SetImageList(&m_SmallImg, LVSIL_SMALL);
            m_List.SetImageList(&m_LargeImg, LVSIL_NORMAL); 

            m_List.InsertColumn( 0, "Name", LVCFMT_LEFT, 120);
            m_List.InsertColumn(1, "Profession", LVCFMT_CENTER, 100);

            m_List.InsertColumn(2, "Fav. Sport", LVCFMT_LEFT, 100);
            m_List.InsertColumn(3, "Hobby", LVCFMT_LEFT, 85);

            int nItem; nItem = m_List.InsertItem(0, "Sandra C. Anschwitz", 0);
            m_List.SetItemText(nItem, 1, "Singer"); 
            m_List.SetItemText(nItem, 2, "HandBall");
            m_List.SetItemText(nItem, 3, "Beach");

            nItem = m_List.InsertItem(0, "Roger A. Miller", 1);
            m_List.SetItemText(nItem, 1, "FootBaller");
            m_List.SetItemText(nItem, 2, "Tennis");
            m_List.SetItemText(nItem, 3, "Teaching");

            nItem = m_List.InsertItem(0, "Marie-Julie W. Gros", 2);
            m_List.SetItemText(nItem, 1, "Student");
            m_List.SetItemText(nItem, 2, "Boxing");
            m_List.SetItemText(nItem, 3, "Programming");

            nItem = m_List.InsertItem(0, "Ella Pius Roger", 3);
   
         m_List.SetItemText(nItem, 1, "Architect");
            m_List.SetItemText(nItem, 2, "Ping-Pong");
            m_List.SetItemText(nItem, 3, "Songo");

            nItem = m_List.InsertItem(0, "Eduardo Santana", 4);
            m_List.SetItemText(nItem, 1, "Sower");
            m_List.SetItemText(nItem, 2, "Chess");
            m_List.SetItemText(nItem, 3, "Setting Traps");

            return TRUE; // return TRUE unless you set the focus to a control 
}

 

  Copyright (c) 1999 FunctionX