⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 3 页
字号:
ball is a different color.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 18.3.</B></A> <I><BR>Bitmaps used for the ListEx image lists.</I></P><P>The properties for the two bitmaps are provided in Table 18.4.<H4><FONT COLOR="#000077">Table 18.4. Properties for the ListEx image list bitmaps.</FONT></H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><B>Resource ID</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Width</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Height</B></TD>		<TD ALIGN="LEFT" VALIGN="TOP"><B>Background</B></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDB_BALLS</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP">64</TD>		<TD ALIGN="LEFT" VALIGN="TOP">32</TD>		<TD ALIGN="LEFT" VALIGN="TOP">White</TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDB_SM_BALLS</TT></TD>		<TD ALIGN="LEFT" VALIGN="TOP">32</TD>		<TD ALIGN="LEFT" VALIGN="TOP">16</TD>		<TD ALIGN="LEFT" VALIGN="TOP">White</TD>	</TR></TABLE><H3><FONT COLOR="#000077"><B>Adding Items to a List View Control</B></FONT></H3><P>The <TT>LV_ITEM</TT> structure is used to represent an item in a list view control.This structure is used when adding, modifying, or fetching list view items. The datamembers for the <TT>LV_ITEM</TT> structure include the following:<UL>	<LI><TT>mask</TT> is used to indicate which members are being used for the current	function call. Possible values for this member are given later in this section.<BR>	<BR>		<LI><TT>item</TT> contains the List View index of the item referred to by this structure.	The first item contained in the list view control has an index of zero, the next	has an index of one, and so on.<BR>	<BR>		<LI><TT>iSubItem</TT> contains the index of the current subitem. A <I>subitem</I>	is a string that is displayed in a column to the right of an item's icon and label	in the report view. The first subitem has an index of one. The zero index is used	by the actual list view item.<BR>	<BR>		<LI><TT>state</TT> and <TT>stateMask</TT> contain the current state of the item and	the valid states of the item.<BR>	<BR>		<LI><TT>pszText</TT> contains the address of a string that is used as the item's	label. This member must be assigned the <TT>LPSTR_TEXTCALLBACK</TT> value if a callback	function is used to set the item's text.<BR>	<BR>		<LI><TT>cchTextMax</TT> specifies the size of the buffer provided in the <TT>pszText</TT>	member if the structure is receiving item attributes. Otherwise, this member is not	used.<BR>	<BR>		<LI><TT>iImage</TT> contains the image list index for this item.</UL><P>The <TT>LV_ITEM</TT> structure's <TT>mask</TT> member is used to indicate whichparts of the structure are valid or should be filled in. It can be one or more ofthe following values:<UL>	<LI><TT>LVIF_TEXT</TT> indicates that the <TT>pszText</TT> member is valid.<BR>	<BR>		<LI><TT>LVIF_IMAGE</TT> indicates that the <TT>iImage</TT> member is valid.<BR>	<BR>		<LI><TT>LVIF_PARAM</TT> indicates that the <TT>lParam</TT> member is valid.<BR>	<BR>		<LI><TT>LVIF_STATE</TT> indicates that the <TT>state</TT> member is valid.</UL><BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>When more than one	value is needed, combine them using the C++ <TT>OR</TT> <TT>|</TT> operator:</P>	<P><FONT COLOR="#0066FF"><TT>listItem.mask = LVIF_TEXT | LVIF_IMAGE;</TT></FONT>	<HR></BLOCKQUOTE><PRE></PRE><H4><FONT COLOR="#000077">Inserting a List View Item</FONT></H4><P>The <TT>InsertItem</TT> function is used to add an item to a list view control:</P><PRE><FONT COLOR="#0066FF"><TT>m_listItem.InsertItem( &amp;listItem );</TT></FONT></PRE><P>A pointer to an <TT>LV_ITEM</TT> structure is passed as the parameter to <TT>InsertItem</TT>.<TT>LV_ITEM</TT> data members are filled with data for the new item before it isinserted.</P><PRE><FONT COLOR="#0066FF"><TT>listItem.mask = LVIF_TEXT;</TT><TT>listItem.iItem = 0;</TT><TT>listItem.pszText = szText;</TT><TT>m_listCtrl.InsertItem( &amp;listItem );</TT></FONT></PRE><H4><FONT COLOR="#000077">Adding Column Information for the Report View</FONT></H4><P>Unlike the other three list view styles, the report view displays additional informationfor each item contained in the list. The extra items are subitems that are arrangedin columns. Each list view item must have the same number of subitems. For example,in the Windows Explorer, the subitems are used for file information such as filesize, file type, and modified date.</P><P>Columns for subitems are added to a list view control in two steps: first, the<TT>LV_COLUMN</TT> data structure is initialized and then the columns are added.List view columns are inserted using <TT>LV_COLUMN</TT> structures and the <TT>InsertColumn</TT>function. The <TT>LV_COLUMN</TT> structure has the following members:<UL>	<LI><TT>mask</TT> indicates the member variables that are used for the current function	call. Values for the mask member variable are discussed at the end of this section.<BR>	<BR>		<LI><TT>fmt</TT> specifies the alignment used for the column. There are three possible	values: <TT>LVCFMT_LEFT</TT>, <TT>LVCFMT_RIGHT</TT>, and <TT>LVCFMT_CENTER</TT>.	The first column must use the <TT>LVCFMT_LEFT</TT> value.<BR>	<BR>		<LI><TT>cx</TT> specifies the width of the column in pixels.<BR>	<BR>		<LI><TT>pszText</TT> points to a string containing the column text. If the structure	is used to fetch information, this member holds the address of the buffer that contains	the column heading text.<BR>	<BR>		<LI><TT>cchTextMax</TT> stores the size of the buffer that is pointed to by <TT>pszText</TT>.	This member is used only when receiving data.<BR>	<BR>		<LI><TT>iSubItem</TT> specifies the column number.</UL><P>The mask member variable is used to specify which member values are valid. Possiblevalues include the following:<UL>	<LI><TT>LVCF_FMT</TT> indicates that the <TT>fmt</TT> member is valid.<BR>	<BR>		<LI><TT>LVCF_SUBITEM</TT> indicates that the <TT>iSubItem</TT> member is valid.<BR>	<BR>		<LI><TT>LVCF_TEXT</TT> indicates that the <TT>pszText</TT> member is valid.<BR>	<BR>		<LI><TT>LVCF_WIDTH</TT> indicates that the <TT>cx</TT> member is valid.</UL><P>After you fill in the values for an <TT>LV_COLUMN</TT> structure, the column isadded to the list view control using the <TT>InsertColumn</TT> function:</P><PRE><FONT COLOR="#0066FF"><TT>m_listCtrl.InsertColumn( nColumn, &amp;listColumn );</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Determining Which Items Are Selected</B></FONT></H3><P>Unlike a list box control, no single message or function exists to determine whichitems are selected in a list view control. Instead, you must use the <TT>CListCtrl::GetNextItem</TT>function, as in this example:</P><PRE><FONT COLOR="#0066FF"><TT>int nSel = m_listCtrl.GetNextItem( -1, LVNI_SELECTED );</TT></FONT></PRE><P>This code returns the index of the first selected item in the list view control.<TT>GetNextItem</TT> has two parameters: the start item and a search flag. If thestart item is <TT>-1</TT>, the search starts with the first item. The flag variablecan include one geometric value and one state value. The following are the geometricvalues:<UL>	<LI><TT>LVNI_ABOVE</TT>: Searches for an item above the start item.<BR>	<BR>		<LI><TT>LVNI_ALL</TT>: Searches for the next indexed item. This is the default value.<BR>	<BR>		<LI><TT>LVNI_BELOW</TT>: Searches for an item below the start item.<BR>	<BR>		<LI><TT>LVNI_TOLEFT</TT>: Searches for an item to the left of the start item.<BR>	<BR>		<LI><TT>LVNI_TORIGHT</TT>: Searches for an item to the right of the start item.</UL><P>The following are the possible state values:<UL>	<LI><TT>LVNI_DROPHILITED</TT>: Searches for an item that has the <TT>LVIS_DROPHILITED</TT>	state flag set.<BR>	<BR>		<LI><TT>LVNI_FOCUSED</TT>: Searches for an item that has the <TT>LVIS_FOCUSED</TT>	state flag set.<BR>	<BR>		<LI><TT>LVNI_HIDDEN</TT>: Searches for an item that has the <TT>LVIS_HIDDEN</TT>	state flag set.<BR>	<BR>		<LI><TT>LVNI_MARKED</TT>: Searches for an item that has the <TT>LVIS_MARKED</TT>	state flag set.<BR>	<BR>		<LI><TT>LVNI_SELECTED</TT>: Searches for an item that has the <TT>LVIS_SELECTED</TT>	state flag set.</UL><P>If no item can be found that matches the search parameters, <TT>-1</TT> is returned.Otherwise, the index of the first list item that satisfies the criteria is returned.<H3><FONT COLOR="#000077"><B>Modifications to the <TT>CListExDlg</TT> Class</B></FONT></H3><P>The <TT>CListExDlg</TT> class must have two small modifications made to its classdeclaration:<UL>	<LI>You must add two <TT>CImageList</TT> member variables for the list view control.<BR>	<BR>		<LI>You must add a <TT>SetListView</TT> member function. This function handles switching	between different list view styles.</UL><P>Add the source code provided in Listing 18.1 to the implementation section ofthe <TT>CListExDlg</TT> class declaration.<H4><FONT COLOR="#000077">TYPE: Listing 18.1. Changes to the CListExDlg class declaration.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// Implementation</TT><TT>protected:</TT><TT>    void SetListView( DWORD dwView );</TT><TT>    CImageList  m_imageLarge;</TT><TT>    CImageList  m_imageSmall;</TT></FONT></PRE><P>The <TT>CListExDlg::OnInitDialog</TT> member function is called when the maindialog box is initialized. Add the source code in Listing 18.2 to the <TT>OnInitDialog</TT>function, just after the <TT>// TODO</TT> comment provided by AppWizard.<H4><FONT COLOR="#000077">TYPE: Listing 18.2. Changes to the CListExDlg class implementation.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// TODO: Add extra initialization here</TT><TT>// Create and set image lists</TT><TT>m_imageLarge.Create( IDB_BALLS, 32, 1, RGB(255,255,255) );</TT><TT>m_imageSmall.Create( IDB_SM_BALLS, 16, 1, RGB(255,255,255) );</TT><TT>m_listCtrl.SetImageList( &amp;m_imageLarge, LVSIL_NORMAL );</TT><TT>m_listCtrl.SetImageList( &amp;m_imageSmall, LVSIL_SMALL );</TT><TT>// Create list view columns</TT><TT>LV_COLUMN   listColumn;</TT><TT>LV_ITEM     listItem;</TT><TT>char*       arColumns[3] = { &quot;City&quot;, &quot;Football&quot;, &quot;Baseball&quot; };</TT><TT>listColumn.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;</TT><TT>listColumn.fmt = LVCFMT_LEFT;</TT><TT>listColumn.cx = 60;</TT><TT>for( int nColumn = 0; nColumn &lt; 3; nColumn++ )</TT><TT>{</TT><TT>    listColumn.iSubItem = nColumn;</TT><TT>    listColumn.pszText = arColumns[nColumn];</TT><TT>    m_listCtrl.InsertColumn( nColumn, &amp;listColumn );</TT><TT>}</TT><TT>// Add list items</TT><TT>listItem.mask = LVIF_TEXT | LVIF_IMAGE;</TT><TT>listItem.iSubItem = 0;</TT><TT>char* arCity[3]      = { &quot;Oakland&quot;, &quot;San Diego&quot;, &quot;Seattle&quot; };</TT><TT>char* arFootball[3]  = { &quot;Raiders&quot;, &quot;Chargers&quot;, &quot;Seahawks&quot; };</TT><TT>char* arBaseball[3]  = { &quot;Athletics&quot;, &quot;Padres&quot;, &quot;Mariners&quot; };</TT><TT>for( int nItem = 0; nItem &lt; 3; nItem++ )</TT><TT>{</TT><TT>    listItem.iItem = nItem;</TT><TT>    listItem.pszText = arCity[nItem];</TT><TT>    listItem.iImage = nItem % 2;</TT><TT>    m_listCtrl.InsertItem( &amp;listItem );</TT><TT>    m_listCtrl.SetItemText( nItem, 1, arFootball[nItem] );</TT><TT>    m_listCtrl.SetItemText( nItem, 2, arBaseball[nItem] );</TT><TT>}</TT></FONT></PRE><P>The source code provided in Listing 18.2 creates two image lists for the listview control and then creates the list view's column headers. After the columns arecreated, the three list items are inserted into the list view. The <TT>SetItemText</TT>function is used to add subitem text strings to each list item--in this case, thename of the professional football and baseball teams for each city.<H3><FONT COLOR="#000077"><B>Changing the Current View for a List View Control</B></FONT></H3><P>Switching views in a list view control requires just a few lines of code. Thecurrent view style is stored in a structure maintained by Windows. This informationcan be retrieved using the <TT>GetWindowLong</TT> function:</P><PRE><FONT COLOR="#0066FF"><TT>DWORD dwOldStyle = GetWindowLong( hWndList, GWL_STYLE );</TT></FONT></PRE><P>The <TT>GetWindowLong</TT> function has two parameters:<UL>	<LI>A window handle to the list view control	<LI>A <TT>GWL</TT> constant that specifies the type of information requested--in	this case, <TT>GWL_STYLE</TT></UL><P>The return value from <TT>GetWindowLong</TT> contains all the Windows style informationfor the list view control. If you are interested in the current view, the unnecessaryinformation should be masked off using <TT>LVS_TYPEMASK</TT>.</P><PRE><FONT COLOR="#0066FF"><TT>dwOldStyle &amp;= ~LVS_TYPEMASK; // Mask off extra style info</TT></FONT></PRE><P>After the mask has been applied, the style information is one of the followingvalues:<UL>	<LI><TT>LVS_ICON</TT>	<LI><TT>LVS_SMALLICON</TT>	<LI><TT>LVS_LIST</TT>	<LI><TT>LVS_REPORT</TT></UL><P>To change to another view, you use the <TT>SetWindowLong</TT> function. When applyinga new list view style, you must make sure that the style bits that are not associated

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -