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

📄 ch03.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<pre><font color="#008000">    m_listView.SetItemText(1, 1, &quot;Sub Item 1.1&quot;);</font></pre>

<pre><font color="#008000">    m_listView.SetItemText(1, 2, &quot;Sub Item 1.2&quot;);</font></pre>

<pre><font color="#008000">    lvItem.iItem = 2;</font></pre>

<pre><font color="#008000">    lvItem.iSubItem = 0;</font></pre>

<pre><font color="#008000">    lvItem.pszText = &quot;Item 2&quot;;</font></pre>

<pre><font color="#008000">    m_listView.InsertItem(&amp;lvItem);</font></pre>

<pre><font color="#008000">    m_listView.SetItemText(2, 1, &quot;Sub Item 2.1&quot;);</font></pre>

<pre><font color="#008000">    m_listView.SetItemText(2, 2, &quot;Sub Item 2.2&quot;);</font></pre>

<pre><font color="#008000">    // Create the view-control buttons.</font></pre>

<pre><font color="#008000">    m_smallButton.Create(&quot;Small&quot;, WS_VISIBLE | WS_CHILD | WS_BORDER,</font></pre>

<pre><font color="#008000">        CRect(400, 120, 450, 140), this, IDC_LISTVIEW_SMALL);</font></pre>

<pre><font color="#008000">    m_largeButton.Create(&quot;Large&quot;, WS_VISIBLE | WS_CHILD | WS_BORDER,</font></pre>

<pre><font color="#008000">        CRect(400, 145, 450, 165), this, IDC_LISTVIEW_LARGE);</font></pre>

<pre><font color="#008000">    m_listButton.Create(&quot;List&quot;, WS_VISIBLE | WS_CHILD | WS_BORDER,</font></pre>

<pre><font color="#008000">        CRect(400, 170, 450, 190), this, IDC_LISTVIEW_LIST);</font></pre>

<pre><font color="#008000">    m_reportButton.Create(&quot;Report&quot;, WS_VISIBLE | WS_CHILD | WS_BORDER,</font></pre>

<pre><font color="#008000">        CRect(400, 195, 450, 215), this, IDC_LISTVIEW_REPORT);</font></pre>

<pre><font color="#008000">}</font></pre>

<p><font color="#008000">CreateListView()</font> first creates two image lists: one holds the small icon for the list view, and the other holds the large icon. (In this case, the list includes only one icon. In other applications, you might have a list of 
large icons for folders, text files, and so on, plus another list of small icons for the same purposes.)</P>

<P>After creating the image lists, <font color="#008000">CreateListView()</font> goes on to create the list view control, by calling the class's <font color="#008000">Create()</font> member function as usual. The <font color="#008000">CListCtrl</font> 
class, of which <font color="#008000">m_listView</font> is an object, defines special styles to be used with list view controls. Table 3.7 lists these special styles and their descriptions.</P>

<P><I>Table 3.7&#151;List View Styles</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Style</B></P>

<TD>

<P><b>Description</b></P>

<TR>

<TD>

<pre><font color="#008000">LVS_ALIGNLEFT</font></pre>

<TD>

<P>Left aligns items in the large-icon and small-icon views</P>

<TR>

<TD>

<pre><font color="#008000">LVS_ALIGNTOP</font></pre>

<TD>

<P>Top aligns items in the large-icon and small-icon views</P>

<TR>

<TD>

<pre><font color="#008000">LVS_AUTOARRANGE</font></pre>

<TD>

<P>Automatically arranges items in the large-icon and small-icon views</P>

<TR>

<TD>

<pre><font color="#008000">LVS_EDITLABELS</font></pre>

<TD>

<P>Enables the user to edit item labels</P>

<TR>

<TD>

<pre><font color="#008000">LVS_ICON</font></pre>

<TD>

<P>Sets the control to the large-icon view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_LIST</font></pre>

<TD>

<P>Sets the control to the list view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_NOCOLUMNHEADER</font></pre>

<TD>

<P>Shows no column headers in report view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_NOITEMDATA</font></pre>

<TD>

<P>Stores only the state of each item</P>

<TR>

<TD>

<pre><font color="#008000">LVS_NOLABELWRAP</font></pre>

<TD>

<P>Disallows multiple-line item labels</P>

<TR>

<TD>

<pre><font color="#008000">LVS_NOSCROLL</font></pre>

<TD>

<P>Turns off scrolling</P>

<TR>

<TD>

<pre><font color="#008000">LVS_NOSORTHEADER</font></pre>

<TD>

<P>Turns off the button appearance of column headers</P>

<TR>

<TD>

<pre><font color="#008000">LVS_OWNERDRAWFIXED</font></pre>

<TD>

<P>Enables owner-drawn items in report view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_REPORT</font></pre>

<TD>

<P>Sets the control to the report view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_SHAREIMAGELISTS</font></pre>

<TD>

<P>Prevents the control from destroying its image lists when the control no longer needs them</P>

<TR>

<TD>

<pre><font color="#008000">LVS_SINGLESEL</font></pre>

<TD>

<P>Disallows multiple selection of items</P>

<TR>

<TD>

<pre><font color="#008000">LVS_SMALLICON</font></pre>

<TD>

<P>Sets the control to the small-icon view</P>

<TR>

<TD>

<pre><font color="#008000">LVS_SORTASCENDING</font></pre>

<TD>

<P>Sorts items in ascending order</P>

<TR>

<TD>

<pre><font color="#008000">LVS_SORTDESCENDING</font></pre>

<TD>

<P>Sorts items in descending order</P></TABLE>

<P>The third task in <font color="#008000">CreateListView()</font> is to associate the control with its image lists with two calls to <font color="#008000">SetImageList()</font>. This function takes two parameters: a pointer to the image list and a flag 
indicating how the list is to be used. There are three constants defined for this flag: <font color="#008000">LVSIL_SMALL</font> (which indicates that the list contains small icons), <font color="#008000">LVSIL_NORMAL</font> (large icons), and <font 
color="#008000">LVSIL_STATE</font> (state images). The <font color="#008000">SetImageList()</font> function returns a pointer to the previously set image list, if any.</P>

<P><B><I> Creating the List View's Columns</I></B></P>

<P>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, including the file's size, type, and modification date. To create a column, you must first declare a <font color="#008000">LV_COLUMN</font> structure. You use this structure to pass 
information to and from the system. After you add the column to the control with <font color="#008000">InsertColumn()</font>, you can use the structure to create and insert another column. The <font color="#008000">LV_COLUMN</font> structure is shown in 
Listing 3.7.</P>

<P><I>Listing 3.7&#151;The </I>LV_COLUMN<I> </I><I>Structure, defined by MFC</I></P>

<pre><font color="#008000">typedef struct _LV_COLUMN</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    UINT mask;       // Flags indicating valid fields</font></pre>

<pre><font color="#008000">    int fmt;         // Column alignment</font></pre>

<pre><font color="#008000">    int cx;          // Column width</font></pre>

<pre><font color="#008000">    LPSTR pszText;   // Address of string buffer</font></pre>

<pre><font color="#008000">    int cchTextMax;  // Size of the buffer</font></pre>

<pre><font color="#008000">    int iSubItem;    // Subitem index for this column</font></pre>

<pre><font color="#008000">} LV_COLUMN;</font></pre>

<P>The <font color="#008000">mask</font> member of the structure tells the system which members of the structure to use and which to ignore. The flags you can use are:</P>

<ul>

<li> <font color="#008000">LVCF_FMT</font>&#151;<font color="#008000">fmt</font> is valid.</P>

<li> <font color="#008000">LVCF_SUBITEM</font>&#151;<font color="#008000">iSubItem</font> is valid.</P>

<li> <font color="#008000">LVCF_TEXT</font>&#151;<font color="#008000">pszText</font> is valid.</P>

<li> <font color="#008000">LVCF_WIDTH</font>&#151;<font color="#008000">cx</font> is valid.</P>

</ul>

<P>The <font color="#008000">fmt</font> member denotes the column's alignment and can be <font color="#008000">LVCFMT_CENTER</font>, <font color="#008000">LVCFMT_LEFT</font>, or <font color="#008000">LVCFMT_RIGHT</font>. The alignment determines how the 
column's label and items are positioned in the column.</P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>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.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>The <font color="#008000">cx</font> field specifies the width of each column, whereas <font color="#008000">pszText</font> 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 <font color="#008000">cchTextMax</font> member denotes the size of the string buffer and is valid only when retrieving information about a column.</P>

<p><font color="#008000">CreateListView()</font> creates a temporary <font color="#008000">LV_COLUMN</font> structure, sets the elements, and then inserts it into the listview as column 0, the main column. This process is repeated for the other two 
columns.</P>

<P><B><I>Creating the List View's Items</I></B></P>

<P>The fifth task in <font color="#008000">CreateListView()</font> 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 <font color="#008000">LV_ITEM</font> and is defined, as shown in Listing 3.8.</P>

<P><I>Listing 3.8&#151;The </I>LV_ITEM<I> Structure, </I><I>defined by MFC</I></P>

<pre><font color="#008000">typedef struct _LV_ITEM</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    UINT   mask;         // Flags indicating valid fields</font></pre>

<pre><font color="#008000">    int    iItem;        // Item index</font></pre>

<pre><font color="#008000">    int    iSubItem;     // Sub-item index</font></pre>

<pre><font color="#008000">    UINT   state;        // Item's current state</font></pre>

<pre><font color="#008000">    UINT   stateMask;    // Valid item states.</font></pre>

<pre><font color="#008000">    LPSTR  pszText;      // Address of string buffer</font></pre>

<pre><font color="#008000">    int    cchTextMax;   // Size of string buffer</font></pre>

<pre><font color="#008000">    int    iImage;       // Image index for this item</font></pre>

<pre><font color="#008000">    LPARAM lParam;       // Additional information as a 32-bit value</font></pre>

<pre><font color="#008000">} LV_ITEM;</font></pre>

<P>In the <font color="#008000">LV_ITEM</font> structure, the <font color="#008000">mask</font> member specifies the other members of the structure that are valid. The flags you can use are:</P>

<ul>

<li> <font color="#008000">LVIF_IMAGE</font>&#151;<font color="#008000">iImage</font> is valid.</P>

<li> <font color="#008000">LVIF_PARAM</font>&#151;<font color="#008000">lParam</font> is valid.</P>

<li> <font color="#008000">LVIF_STATE</font>&#151;state is valid.</P>

<li> <font color="#008000">LVIF_TEXT</font>&#151;<font color="#008000">pszText</font> is valid.</P>

</ul>

<P>The <font color="#008000">iItem</font> member is the index of the item, which you can think of as the row number in report view (although the position of the items can change when they're sorted). Each item has a unique index. The <font 
color="#008000">iSubItem</font> 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.</P>

<P>The <font color="#008000">state</font> and <font color="#008000">stateMask</font> members hold the item's current state and the item's valid states, which can be one or more of the following:</P>

<ul>

<li> <font color="#008000">LVIS_CUT</font>&#151;The item is selected for cut and paste</P>

<li> <font color="#008000">LVIS_DROPHILITED</font>&#151;The item is a highlighted drop target</P>

<li> <font color="#008000">LVIS_FOCUSED</font>&#151;The item has the focus</P>

<li> <font color="#008000">LVIS_SELECTED</font>&#151;The item is selected.</P>

</ul>

<P>The <font color="#008000">pszText</font> member is the address of a string buffer. When using the <font color="#008000">LV_ITEM</font> structure to create an item, the string buffer contains the item's text. When obtaining information about the item, 
<font color="#008000">pszText</font> is the buffer where the information will be stored, and <font color="#008000">cchTextMax</font> is the size of the buffer. If <font color="#008000">pszText</font> is set to <font 
color="#008000">LPSTR_TEXTCALLBACK</font>, the item uses the callback mechanism. Finally, the <font color="#008000">iImage</font> member is the index of the item's icon in the small icon and large icon image lists. If set to <font 
color="#008000">I_IMAGECALLBACK</font>, the <font color="#008000">iImage</font> member indicates that the item uses the callback mechanism.</P>

<p><font color="#008000">CreateListView()</font>creates a temporary <font color="#008000">LV_ITEM</font> structure, sets the elements, and then inserts it into the listview as item 0. Two calls to <font color="#008000">SetItemText()</font> add subitems to 
this item, so that each column has some text in it, and the whole process is repeated for two other items. Now you have created a list view with three columns and three items. Normally the values would not have been hard-coded as this was, but instead 
would have been filled in with values calculated by the program.</P>

<P><B><I> Manipulating the List View</I></B></P>

<P>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 <U>V</U>iew 
menu. Although Win95 Controls App doesn't have a snazzy toolbar like Explorer, it does include four buttons (labelled Small, Large, List, and Report) that you can click to change the view. Those buttons are created as the sixth step in <font 
color="#008000">CreateListView()</font>.</P>

<P>These buttons are associated (by hand-added entries in the view window's message map) with the message-response functions <font color="#008000">OnSmall()</font>, <font color="#008000">OnLarge()</font>, <font color="#008000">OnList()</font>, and <font 
color="#008000">OnReport()</font>. As will be explained in <A HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &#147;Messages and Commands,&#148; when the user clicks one of these buttons, its matching function is called, and the program changes the list 
view control to the requested view type. For example, when the user clicks the Small button, <font color="#008000">OnSmall()</font>, shown in Listing 3.9, changes the view to the small icon view. The other three functions are very similar.</P>

<P><I>Listing </I><I>3.9&#151;Win95View.cpp&#151;</I>CWin95View::OnSmall()</P>

⌨️ 快捷键说明

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