Home Index  | Back

New Page 1
Lesson 1 -Building Your First Windows Application

1.1 : Creating a Windows Application

1.2 : Creating a Dialog-Based Application

1.3 : Creating DLLs, Console Applications, and More

1.4 : Changing Your AppWizard Decisions
 1.5 : Understanding AppWizard’s Code
 1.6 : Understanding a MDI Application
 1.7 : Understanding the Components of a Dialog-Based Application

Lesson 2 - Dialogs and Controls

2.1 : Understanding Dialog Boxes

2.2 : Creating a Dialog Box Resource

 2.3 : Writing a Dialog Box Class
 2.4 : Using the Dialog Box Class
Lesson 3 - Messages and Commands

3.1 : Understanding Message Routing

3.2 : Understanding Message Loops

 3.3 : Reading Message Maps
 3.4 : Learning How ClassWizard Helps You Catch Messages
 3.5 : Recognizing Messages
 3.6 : Understanding Commands
 3.7 : Understanding Command Updates
 3.8 : Learning How ClassWizard Helps You Catch Commands and Command Updates

Lesson 4 - Documents and Views

4.1 : Understanding the Document Class

4.2 : Understanding the View Class

4.3 : Creating the Rectangles Application

 4.4 : Other View Classes

4.5 : Document Templates, Views, and Frame Windows

Lesson 5 - Drawing on the Screen

5.1 :Understanding Device Contexts

 5.2 : Introducing the Paint1 Application
 5.3 : Building the Paint1 Application
 5.4 : Scrolling Windows
 5.5 : Building the Scroll Application
Lesson 6 - Building a Complete Application: ShowString

6.1 : Building an Application That Displays a String

 6.2 : Building the ShowString Menus
 6.3 : Building the ShowString Dialog Boxes
 6.4 : Making the Menu Work
 6.5 : Making the Dialog Box Work
 6.6 : Adding Appearance Options to the Options Dialog Box
Lesson 7 -  Status Bars and Toolbars

7.1 : Working with Toolbars

 7.2 : Working with Status Bars
Lesson 8 - Common Controls

8.1 : The Progress Bar Control

 8.2 : The Up-Down Control
 8.3 : The Image List Control
 8.4 : The List View Control
 8.5 : The Tree View Control
 8.6 : The Rich Edit Control
 8.7 : The Date Picker Control
 8.8 : Month Calendar Control
 8.9 : Scrolling the View
Lesson 9 - Property Pages and Sheets

9.1 : Introducing Property Sheets

 9.2 : Creating the Property Sheet Demo Application
 9.3 : Running the Property Sheet Demo Application
Lesson 10 - ActiveX Concepts

10.1 : The Purpose of ActiveX

10.2 : Object Linking

10.3 : Object Embedding

 10.4 : Containers and Servers
 10.5 : Toward a More Intuitive User Interface
 10.6 : The Component Object Model  

10.7 : Automation

 10.8 : ActiveX Controls

Lesson 11 -  Building an ActiveX Control

11.1 : Creating a Rolling-Die Control

11.2 : Displaying the Current Value

11.3 : Reacting to a Mouse Click and Rolling the Die 

 11.4 : Creating a Better User Interface
 11.5 : Generating Property Sheets
Lesson 12 - Database Access

12.1 : Understanding Database Concepts

12.2 : Creating an ODBC Database Program

 12.3 : Choosing Between ODBC and DAO
 12.4 : OLE DB

Lesson -8:Common Controls

8.4-The List View Control

A list view control simplifies the job of building an application that works with lists of objects and organizes those objects in such a way that the program’s user can easily determine each object’s attributes. For example, consider a group of files on a disk. Each file is a separate object associated with a number of attributes, including the file’s name, size, and the most recent modification date. When you explore a folder, you see files either as icons in a window or as a table of entries, each entry showing the attributes associated with the files. You have full control over the way that the file objects are displayed, including which attributes are shown and which are unlisted. The common controls include something called a list view control, so you can organize lists in exactly the same way. If you’d like to see an example of a full-fledged list view control, open the Windows Explorer (see Figure 8.3). The right side of the window shows how the list view control can organize objects in a window. (The left side of the window contains a tree view control, which you will learn about later in this unit in the section titled "The Tree View Control.") In the figure, the list view is currently set to the report view, in which each object in the list receives its own line, showing not only the object’s name but also the attributes associated with that object.


FIG. 8.3 Windows Explorer uses a list view control to organize file information.

The user can change the way objects are organized in a list view control. Figure 8.4, for example, shows the list view portion of the Explorer set to the large-icon setting, and Figure 8.5 shows the small-icon setting, which enables the user to see more objects (in this case, files) in the window. With a list view control, the user can edit the names of objects in the list and in the report view can sort objects, based on data displayed in a particular column.


FIG. 8.4 Here’s Explorer’s list view control set to large icons.


FIG. 8.5 Here’s Explorer’s list view control set to small icons.

Common will also sport a list view control, although not as fancy as Explorer’s. You will add a list view and some buttons to switch between the small-icon, large-icon, list, and report views.

8.4.1 Creating the List View

How does all this happen? Well, it does require more work than the progress bar, or up-down controls (it could hardly take less). You will write the rest of CreateListView(), which performs the following tasks:

1. Creates and fills the image list controls

2. Creates the list view control itself

3. Associates the image lists with the list view

4. Creates the columns

5. Sets up the columns

6. Creates the items

7. Sets up the items

8. Creates the buttons

After creating the image lists, CreateListView() goes on to create the list view control by calling the class’s Create() member function, as usual. Add these lines to CreateListView():

// Create the List View control.

m_listView.Create(WS_VISIBLE | WS_CHILD | WS_BORDER |

LVS_REPORT | LVS_NOSORTHEADER | LVS_EDITLABELS,

CRect(160, 120, 394, 220), this, IDC_LISTVIEW);

The CListCtrl class, of which m_listView is an object, defines special styles to be used with list view controls. Table 8.2 lists these special styles and their descriptions.

Table 8.2 List View Styles

Style

Description

LVS_ALIGNLEFT Left-

aligns items in the large-icon and small-icon views

LVS_ALIGNTOP

Top-aligns items in the large-icon and small-icon views

LVS_AUTOARRANGE Automatically arranges items in the large-icon and small-icon views
LVS_EDITLABELS Enables the user to edit item labels
LVS_ICON Sets the control to the large-icon view
LVS_LIST Sets the control to the list view
LVS_NOCOLUMNHEADER Shows no column headers in report view
LVS_NOITEMDATA Stores only the state of each item
LVS_NOLABELWRAP Disallows multiple-line item labels
LVS_NOSCROLL Turns off scrolling
LVS_NOSORTHEADER Turns off the button appearance of column headers
LVS_OWNERDRAWFIXED Enables owner-drawn items in report view
LVS_REPORT Sets the control to the report view
LVS_SHAREIMAGELISTS Prevents the control from destroying its image lists when the control no longer needs them
LVS_SINGLESEL Disallows multiple selection of items
LVS_SMALLICON Sets the control to the small-icon view
LVS_SORTASCENDING Sorts items in ascending order
LVS_SORTDESCENDING

Sorts items in descending order

The third task in CreateListView() is to associate the control with its image lists with two calls to SetImageList(). Add these lines to CreateListView():

m_listView.SetImageList(&m_smallImageList, LVSIL_SMALL);

m_listView.SetImageList(&m_largeImageList, LVSIL_NORMAL);

This function takes two parameters: a pointer to the image list and a flag indicating how the list is to be used. Three constants are defined for this flag: LVSIL_SMALL (which indicates that the list contains small icons), LVSIL_NORMAL (large icons), and LVSIL_STATE (state images). The SetImageList() function returns a pointer to the previously set image list, if any.

8.4.2 Creating the List View’s Columns

The fourth task is to create the columns for the control’s report view. You need one main column for the item itself and one column for each sub-item associated with an item. For example, in Explorer’s list view, the main column holds file and folder names. Each additional column holds the sub-items for each item, such as the file’s size, type, and modification date. To create a column, you must first declare a LV_COLUMN structure. You use this structure to pass information to and from the system. After you add the column to the control with InsertColumn(), you can use the structure to create and insert another column. Listing 8.4 shows the LV_COLUMN structure.

Listing 8.4 The LV_COLUMN Structure, Defined by MFC

typedef struct _LV_COLUMN

{

UINT mask; // Flags indicating valid fields

int fmt; // Column alignment

int cx; // Column width

LPSTR pszText; // Address of string buffer

int cchTextMax; // Size of the buffer

int iSubItem; // Subitem index for this column

} LV_COLUMN;

The mask member of the structure tells the system which members of the structure to use and which to ignore. The flags you can use are

· LVCF_FMT fmt is valid.

· LVCF_SUBITEM iSubItem is valid.

· LVCF_TEXT pszText is valid.

· LVCF_WIDTH cx is valid.

The fmt member denotes the column’s alignment and can be LVCFMT_CENTER, LVCFMT_LEFT, or LVCFMT_RIGHT. The alignment determines how the column’s label and items are positioned in the column.

NOTE: The first column, which contains the main items, is always aligned to the left. The other columns in the report view can be aligned however you like.

The cx field specifies the width of each column, whereas pszText is the address of a string buffer. When you’re using the structure to create a column (you also can use this structure to obtain information about a column), this string buffer contains the column’s label. The cchTextMax member denotes the size of the string buffer and is valid only when retrieving information about a column.

CreateListView() creates a temporary LV_COLUMN structure, sets the elements, and then inserts it into the list view as column 0, the main column. This process is repeated for the other two columns. Add these lines to CreateListView():

// Create the columns.

LV_COLUMN lvColumn;

lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;

lvColumn.fmt = LVCFMT_CENTER;

lvColumn.cx = 75;

lvColumn.iSubItem = 0;

lvColumn.pszText = "Column 0";

m_listView.InsertColumn(0, &lvColumn);

lvColumn.iSubItem = 1;

lvColumn.pszText = "Column 1";

m_listView.InsertColumn(1, &lvColumn);

lvColumn.iSubItem = 2;

lvColumn.pszText = "Column 2";

m_listView.InsertColumn(1, &lvColumn);

8.4.3 Creating the List View’s Items

The fifth task in CreateListView() is to create the items that will be listed in the columns when the control is in its report view. Creating items is not unlike creating columns. As with columns, Visual C++ defines a structure that you must initialize and pass to the function that creates the items. This structure is called LV_ITEM and is defined as shown in Listing 8.5.

Listing 8.5 The LV_ITEM Structure, Defined by MFC

typedef struct _LV_ITEM

{

UINT mask; // Flags indicating valid fields

int iItem; // Item index

int iSubItem; // Sub-item index

UINT state; // Item’s current state

UINT stateMask; // Valid item states.

LPSTR pszText; // Address of string buffer

int cchTextMax; // Size of string buffer

int iImage; // Image index for this item

LPARAM lParam; // Additional information as a 32-bit value

} LV_ITEM;

In the LV_ITEM structure, the mask member specifies the other members of the structure that are valid. The flags you can use are

· LVIF_IMAGE iImage is valid.

· LVIF_PARAM lParam is valid.

· LVIF_STATE state is valid.

· LVIF_TEXT pszText is valid.

The iItem member is the index of the item, which you can think of as the row number in report view (although the items’ position can change when they’re sorted). Each item has a unique index. The iSubItem member is the index of the sub-item, if this structure is defining a sub-item. You can think of this value as the number of the column in which the item will appear. For example, if you’re defining the main item (the first column), this value should be 0.

The state and stateMask members hold the item’s current state and its valid states, which can be one or more of the following:

· LVIS_CUT The item is selected for cut and paste.

· LVIS_DROPHILITED The item is a highlighted drop target.

· LVIS_FOCUSED The item has the focus.

· LVIS_SELECTED The item is selected.

The pszText member is the address of a string buffer. When you use the LV_ITEM structure to create an item, the string buffer contains the item’s text. When you are obtaining information about the item, pszText is the buffer where the information will be stored, and cchTextMax is the size of the buffer. If pszText is set to LPSTR_TEXTCALLBACK, the item uses the callback mechanism. Finally, the iImage member is the index of the item’s icon in the small-icon and large-icon image lists. If set to I_IMAGECALLBACK, the iImage member indicates that the item uses the callback mechanism.

CreateListView() creates a temporary LV_ITEM structure, sets the elements, and then inserts it into the list view as item 0. Two calls to SetItemText() add sub-items to this item so that each column has some text in it, and the whole process is repeated for two other items. Add these lines:

// Create the items.

LV_ITEM lvItem;

lvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;

lvItem.state = 0;

lvItem.stateMask = 0;

lvItem.iImage = 0;

lvItem.iItem = 0;

lvItem.iSubItem = 0;

lvItem.pszText = "Item 0";

m_listView.InsertItem(&lvItem);

m_listView.SetItemText(0, 1, "Sub Item 0.1");

m_listView.SetItemText(0, 2, "Sub Item 0.2");

lvItem.iItem = 1;

lvItem.iSubItem = 0;

lvItem.pszText = "Item 1";

m_listView.InsertItem(&lvItem);

m_listView.SetItemText(1, 1, "Sub Item 1.1");

m_listView.SetItemText(1, 2, "Sub Item 1.2");

lvItem.iItem = 2;

lvItem.iSubItem = 0;

lvItem.pszText = "Item 2";

m_listView.InsertItem(&lvItem);

m_listView.SetItemText(2, 1, "Sub Item 2.1");

m_listView.SetItemText(2, 2, "Sub Item 2.2");

Now you have created a list view with three columns and three items. Normally the values wouldn’t be hard-coded, as this was, but instead would be filled in with values calculated by the program.

8.4.4 Manipulating the List View

You can set a list view control to four different types of views: small icon, large icon, list, and report. In Explorer, for example, the toolbar features buttons that you can click to change the view, or you can select the view from the View menu. Although Common doesn’t have a snazzy toolbar like Explorer, it will include four buttons (labeled Small, Large, List, and Report) that you can click to change the view. Those buttons are created as the sixth step in CreateListView(). Add these lines to complete the function:

// Create the view-control buttons.

m_smallButton.Create("Small", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 120, 450, 140), this, IDC_LISTVIEW_SMALL);

m_largeButton.Create("Large", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 145, 450, 165), this, IDC_LISTVIEW_LARGE);

m_listButton.Create("List", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 170, 450, 190), this, IDC_LISTVIEW_LIST);

m_reportButton.Create("Report", WS_VISIBLE | WS_CHILD | WS_BORDER,

CRect(400, 195, 450, 215), this, IDC_LISTVIEW_REPORT);

Edit the message map in CommonView.h to declare the handlers for each of these buttons so that it looks like this:

// Generated message map functions

protected:

//{{AFX_MSG(CCommonView)

afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg void OnDestroy();

afx_msg void OnTimer(UINT nIDEvent);

afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

//}}AFX_MSG

afx_msg void OnSmall();

afx_msg void OnLarge();

afx_msg void OnList();

afx_msg void OnReport();

DECLARE_MESSAGE_MAP()

};

Edit the message map in CommonView.cpp to associate the messages with the functions:

BEGIN_MESSAGE_MAP(CCommonView, CScrollView)

//{{AFX_MSG_MAP(CCommonView)

ON_WM_CREATE()

ON_WM_LBUTTONDOWN()

ON_WM_DESTROY()

ON_WM_TIMER()

ON_WM_HSCROLL()

//}}AFX_MSG_MAP

ON_COMMAND(IDC_LISTVIEW_SMALL, OnSmall)

ON_COMMAND(IDC_LISTVIEW_LARGE, OnLarge)

ON_COMMAND(IDC_LISTVIEW_LIST, OnList)

ON_COMMAND(IDC_LISTVIEW_REPORT, OnReport)

// Standard printing commands

ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)

ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)

END_MESSAGE_MAP()

Choose View, Resource Symbols and click New to add new IDs for each constant referred to in this new code:

· IDC_LISTVIEW

· IDC_LISTVIEW_SMALL

· IDC_LISTVIEW_LARGE

· IDC_LISTVIEW_LIST

· IDC_LISTVIEW_REPORT

The four handlers will each call SetWindowLong(), which sets a window’s attribute. Its arguments are the window’s handle, a flag that specifies the value to be changed, and the new value. For example, passing GWL_STYLE as the second value means that the window’s style should be changed to the style given in the third argument. Changing the list view control’s style (for example, to LVS_SMALLICON) changes the type of view that it displays. With that in mind, add the four handler functions to the bottom of CommonView.cpp:

void CCommonView::OnSmall()

{

SetWindowLong(m_listView.m_hWnd, GWL_STYLE,

WS_VISIBLE | WS_CHILD | WS_BORDER |

LVS_SMALLICON | LVS_EDITLABELS);

}

void CCommonView::OnLarge()

{

SetWindowLong(m_listView.m_hWnd, GWL_STYLE,

WS_VISIBLE | WS_CHILD | WS_BORDER |

LVS_ICON | LVS_EDITLABELS);

}

void CCommonView::OnList()

{

SetWindowLong(m_listView.m_hWnd, GWL_STYLE,

WS_VISIBLE | WS_CHILD | WS_BORDER |

LVS_LIST | LVS_EDITLABELS);

}

void CCommonView::OnReport()

{

SetWindowLong(m_listView.m_hWnd, GWL_STYLE,

WS_VISIBLE | WS_CHILD | WS_BORDER |

LVS_REPORT | LVS_EDITLABELS);

}

At this point you can build Common again and test it. Click each of the four buttons to change the view style. Also, try editing one of the labels in the first column of the list view.

Figure 8.1 already showed you the report view for this list view. Figure 8.6 shows the application’s list view control displaying small icons, and Figure 8.7 shows the large icons. (Some controls in these figures have yet to be covered in this unit.)

You can do a lot of other things with a list view control. A little time invested in exploring and experimenting can save you a lot of time writing your user interface.


FIG. 8.6 Here’s the sample application’s list view control set to small icons.


FIG. 8.7 Here’s the sample application’s list view control set to large icons.

 

Next>>
 
© Dewsoft Overseas