?? ch18.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1"> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 18 -- List View Controls</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch17/ch17.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch19/ch19.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 18 -<BR>List View Controls</FONT></H1><P>List views are extremely flexible controls that allow information to be displayedin a variety of ways. In this hour, you will learn<UL> <LI>How to use image lists with the list view control<BR> <BR> <LI>How to switch between different display styles in a list view control<BR> <BR> <LI>How to allow the user to edit individual list items</UL><H2><FONT COLOR="#000077"><B>What Is a List View Control?</B></FONT></H2><P>List view controls are one of the new common controls first released with Windows95. A list view control is used to display information and an associated icon inone of four different formats:<UL> <LI>Icon view displays rows of 32x32 pixel icons.<BR> <BR> <LI>Small icon view displays rows of smaller 16x16 pixel icons.<BR> <BR> <LI>List view displays small icons and list items arranged in a column.<BR> <BR> <LI>Report view displays items and their associated icons, along with subitems that are arranged in columns.</UL><P>When you use a list view control, you can provide a menu or other method to enablethe user to switch between the different viewing modes.</P><P>The Windows Explorer uses a list view control and offers all four view styles.The Explorer is shown in Figure 18.1 with the contents of the <TT>C:\</TT> directorycontained in a large icon view.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 18.1.</B></A> <BR><I>The Windows Explorer uses a list view control.</I></P><P>List views are very popular with users because they offer several different waysto display information. When you allow the user to switch between view styles, thelist view control puts the user in charge of how information is displayed.</P><P>List view controls can be used to associate different icons with items storedin the list view control, as the Explorer does for filenames. The user is free toselect between different sized icons, or even the report view, which can displayextra information about each item. List view controls also support drag-and-dropoperations, which enable the user to easily move items to or from a list view control.<H2><FONT COLOR="#000077"><B>List View Control Properties</B></FONT></H2><P>The list view control's properties are set using the Properties dialog box inthe same way other controls are. Some of the properties available for list view controlsare also available for list boxes. The list view control property options includethe following:<UL> <LI><I>ID</I> is used for the list view control resource ID. A default resource ID, such as <TT>IDC_LIST1</TT>, is supplied by Developer Studio.<BR> <BR> <LI><I>Visible</I><B> </B>is used to indicate that the control is initially visible. This check box is usually checked.<BR> <BR> <LI><I>Disabled</I><B> </B>is used to indicate that the list should be initially disabled. This check box is usually cleared.<BR> <BR> <LI><I>Group</I><B> </B>is used to mark the first control in a group. This check box is usually cleared.<BR> <BR> <LI><I>Tab Stop</I><B> </B>indicates that this control can be reached by pressing Tab on the keyboard. This check box is usually checked.<BR> <BR> <LI><I>Help ID</I><B> </B>indicates that a context-sensitive help ID should be created for this control. This check box is normally cleared.<BR> <BR> <LI><I>View</I><B> </B>specifies the initial view used by the list view control. Possible values are Icon, Small Icon, List, or Report.<BR> <BR> <LI><I>Align</I> specifies whether the items are aligned to the top or left sides of the control. This property applies only in the icon or small icon views.<BR> <BR> <LI><I>Sort</I> enables items to be sorted based on their labels as they are entered into the list view control.<BR> <BR> <LI><I>Auto Arrange</I> specifies that items should be kept arranged when viewed in the icon or small icon views.<BR> <BR> <LI><I>Single Selection</I><B> </B>enables a single list view item to be selected.<BR> <BR> <LI><I>No Label Wrap</I><B> </B>specifies that each item label must be displayed on a single line rather than wrapped, as is the standard behavior.<BR> <BR> <LI><I>Edit Labels</I><B> </B>enables labels to be edited by the user. If this property is enabled, you must handle edit notification messages sent by the control.<BR> <BR> <LI><I>Owner Draw Fixed</I><B> </B>indicates that the owner of the control, instead of Windows, is responsible for drawing the control.<BR> <BR> <LI><I>No Scroll</I><B> </B>disables scrolling.<BR> <BR> <LI><I>No Column Header</I> removes the header control that is usually included in the report view.<BR> <BR> <LI><I>No Sort Header</I> disables the sorting function that is available through the header control.<BR> <BR> <LI><I>Share Image List</I> indicates that the image list used by the list view control is shared with other image lists. You must destroy the image list manually after the last list view control has been destroyed.<BR> <BR> <LI><I>Show Selection Always</I> indicates that a selected item is always highlighted, even if the list view control doesn't have the focus.<BR> <BR> <LI><I>Border</I><B> </B>indicates that a border should be drawn around the control.</UL><H3><FONT COLOR="#000077"><B>A List View Control Example</B></FONT></H3><P>As an example of how the basic properties of list view controls are used, createa dialog box-based application. This program displays a list view control containingthree items. You can use radio buttons to switch between the different view styles.</P><P>Use AppWizard to create a dialog box-based application named ListEx. Feel freeto accept any of the options offered by AppWizard; this example works with any AppWizardparameters.<H3><FONT COLOR="#000077"><B>Adding Controls to the ListEx Dialog Box</B></FONT></H3><P>You must add a total of five controls to the ListEx main dialog box: four radiobuttons and one list view control. Add the controls to the dialog box as shown inFigure 18.2.</P><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 18.2.</B></A> <BR><I>The ListEx main dialog box in the dialog editor.</I></P><P>Properties for the list view and radio button controls are listed in Table 18.1.All properties that aren't listed should be set to the default values.<H4><FONT COLOR="#000077">Table 18.1. Property values for controls in the ListExmain dialog box.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Resource ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Caption</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Icon view radio</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_ICON</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">&Icon</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Small radio</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_SMALL</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">&Small</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">List radio</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_LIST</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">&List</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Report radio</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_REPORT</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">&Report</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">List view control</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_LIST</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">none</TD> </TR></TABLE></P><P>Use ClassWizard to associate a <TT>CListCtrl</TT> member variable with <TT>IDC_LIST</TT>,using the values from Table 18.2.<H4><FONT COLOR="#000077">Table 18.2. Values for a new CListCtrl member variablein CListExDlg.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_LIST</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_listCtrl</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>Control</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListCtrl</TT></TD> </TR></TABLE></P><P>Next, use ClassWizard to create message-handling functions that are called whenthe radio buttons are selected. Add a total of four member functions to the <TT>CListExDlg</TT>class, using the values from Table 18.3.<H4><FONT COLOR="#000077">Table 18.3. Values for new CListCtrl member functions inCListExDlg.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Object ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Class Name</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_ICON</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListExDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>BN_CLICKED</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRadioIcon</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_SMALL</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListExDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>BN_CLICKED</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRadioSmall</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_LIST</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListExDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>BN_CLICKED</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRadioList</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_RADIO_REPORT</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListExDlg</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>BN_CLICKED</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRadioReport</TT></TD> </TR></TABLE><H3><FONT COLOR="#000077"><B>Associating Image Lists with a List Control</B></FONT></H3><P>The images displayed in the list view next to each item are stored in image liststhat are associated with the list view control. Constructing and managing image listswas discussed in Hour 17, "Using Image Lists and Bitmaps." An image listis added to a list view control with the <TT>SetImageList</TT> function:</P><PRE><FONT COLOR="#0066FF"><TT>m_listCtrl.SetImageList( &m_imageSmall, LVSIL_SMALL );</TT></FONT></PRE><P>Two parameters are passed to the list view control: the address of the image listand a style parameter that indicates the type of images stored in the image list.There are three image list types.<UL> <LI><TT>LVSIL_NORMAL</TT> is used for the image list used in the icon view.<BR> <BR> <LI><TT>LVSIL_SMALL</TT> is used for the image list used in the small icon view.<BR> <BR> <LI><TT>LVSIL_STATE</TT> is used for optional state images, such as check marks.</UL><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>After the image list control has been added to the list view control, the list view control takes responsibility for destroying the image list. <HR></P> <P><HR><B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>If the Share image list property has been selected, the list view control will not destroy the image list. You must destroy it yourself after the list view has been destroyed.<BR> If you don't destroy the image list, you will create a memory leak. If you destroy the image list too early, the list view control will behave unpredictably. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Creating the Image Lists</B></FONT></H3><P>You must create two bitmaps for the ListEx application. One bitmap is used forthe large icon bitmap and one for the small icon bitmap. The two bitmaps are shownin Figure 18.3. Each of the bitmaps contains two balls of the same size, and each
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -