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

📄 ch07.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<BR>
	
	<LI>Want Key Input: Indicates that the list box owner should receive <TT>WM_VKEYTOITEM</TT>
	or <TT>WM_CHARTOITEM</TT> messages when keys are pressed while the list box has the
	input focus. This option is normally cleared.<BR>
	<BR>
	
	<LI>Disable No Scroll: Displays a vertical scrollbar even if it's not needed. This
	option is normally cleared.<BR>
	<BR>
	
	<LI>No Integral Height: Indicates that Windows should display the list box exactly
	as specified in the resource description, displaying partial items if needed. This
	option is normally selected.
</UL>

<H3><FONT COLOR="#000077"><B>Using the <TT>CListBox</TT> Class</B></FONT></H3>
<P>Like control classes used in previous hours, the MFC <TT>CListBox</TT> class makes
your life much easier by providing a C++ class that hides control messages and provides
an easy-to-use interface. To attach a <TT>CListBox</TT> object to a list box control,
use ClassWizard as you have for controls in previous hours:

<DL>
	<DD>1. Open ClassWizard.<BR>
	<BR>
	2. Select the <TT>CDialog</TT>-derived class that manages the dialog box; in this
	case, <TT>CListBoxDlg</TT>.<BR>
	<BR>
	3. Select the Member Variables tab.<BR>
	<BR>
	4. Select the control ID representing the control associated with the new member
	variable; in this case, <TT>IDC_LIST</TT>.<BR>
	<BR>
	5. Click the button labeled Add Variable. An Add Member Variable dialog box appears.
	Enter the control's name, category, and variable type; then click OK. For this example,
	use the values from Table 7.1.
</DL>

<H4><FONT COLOR="#000077">Table 7.1. Values used to add a CListBox member variable
for CListBoxDlg.</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_listBox</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Control</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CListBox</TT></TD>
	</TR>
</TABLE>

<H3><FONT COLOR="#000077"><B>Adding an Item to a List Box</B></FONT></H3>
<P>There are two ways to add a text string to a list box:

<UL>
	<LI>To add a string to a list box, the <TT>AddString</TT> member function can be
	called:
</UL>



<BLOCKQUOTE>
	<PRE><FONT COLOR="#0066FF"><TT>m_listBox.AddString( &quot;Rene'&quot; );</TT></FONT></PRE>

</BLOCKQUOTE>


<UL>
	<LI>Any strings added to a sorted list box are sorted as they are added. If the list
	box is not sorted, the item is added after the last item in the list.<BR>
	<BR>
	
	<LI>To add an item at a specified position in a list box, use the <TT>InsertString</TT>
	member function:
</UL>



<BLOCKQUOTE>
	<PRE><FONT COLOR="#0066FF"><TT>m_listBox.InsertString( 0, &quot;Alex&quot; );</TT></FONT></PRE>

</BLOCKQUOTE>


<UL>
	<LI>All positions in a list box are numbered beginning with zero. Any existing list
	box items are shifted down, if needed, to make room for the new item.
</UL>

<P>Both the <TT>InsertString</TT> and <TT>AddString</TT> functions return the position
of the new item. If an error occurs when adding an item, <TT>LB_ERR</TT> is returned
from the <TT>AddString</TT> or <TT>InsertString</TT> functions. If the list box is
full, <TT>LB_ERRSPACE</TT> is returned. Using the source code from Listing 7.4, add
three strings to the <TT>IDC_LIST</TT> list box during the <TT>CListBoxDlg::OnInitDialog</TT>
member function. There already are several lines of code in <TT>CListBoxDlg::OnInitDialog</TT>;
add the three <TT>AddString</TT> statements after the <TT>//TODO</TT> comment supplied
by AppWizard.
<H4><FONT COLOR="#000077">TYPE: Listing 7.4. Using AddString to add strings to a
list box.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>// TODO: Add extra initialization here</TT>
<TT>m_listBox.AddString( &quot;Foo&quot; );</TT>
<TT>m_listBox.AddString( &quot;Bar&quot; );</TT>
<TT>m_listBox.AddString( &quot;Baz&quot; );</TT>
</FONT></PRE>
<P>To determine the number of items currently in a list box, use the <TT>GetCount</TT>
member function:</P>
<PRE><FONT COLOR="#0066FF"><TT>nItems = listBox.GetCount();</TT>
</FONT></PRE>


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>The <TT>GetCount</TT>
	function returns the total number of items in a list box, not the value of the last
	valid index. If a list box contains five items, <TT>GetCount</TT> returns five, but
	the last valid index is four. 
<HR>


</BLOCKQUOTE>

<H3><FONT COLOR="#000077"><B>Removing Items from a List Box</B></FONT></H3>
<P>To remove items from a list box, specify the item position to be removed in a
call to the <TT>DeleteString</TT> member function:</P>
<PRE><FONT COLOR="#0066FF"><TT>listBox.DeleteString(8);</TT>
</FONT></PRE>
<P>This line removes the item in the ninth position of the list box. Remember, all
list box position indexes start from zero. The return value from the <TT>DeleteString</TT>
member function is the number of items remaining in the list box, or <TT>LB_ERR</TT>
if any errors occur. The return value can be used like this:</P>
<PRE><FONT COLOR="#0066FF"><TT>int nItems = listBox.GetCount();</TT>
<TT>while(nItems &gt; 3 &amp;&amp; nItems != LB_ERR )</TT>
<TT>    nItems = listBox.DeleteString(nItems - 1);</TT>
</FONT></PRE>
<P>This code removes the contents of a list box, except for the first three items.
To clear a list box completely, use the <TT>ResetContent</TT> function:</P>
<PRE><FONT COLOR="#0066FF"><TT>listBox.ResetContent();</TT>
</FONT></PRE>
<P>The <TT>ResetContent</TT> function returns <TT>void</TT>.
<H3><FONT COLOR="#000077"><B>Receiving List Box Messages</B></FONT></H3>
<P>Several messages are sent to the parent of a list box for notification purposes
when certain events occur. All these messages are prefixed with <TT>LBN_</TT>, for
List Box Notification. For these messages to be sent, the list box must have the
<TT>Notify</TT> property enabled. The following messages are sent from the list box
to its parent:

<UL>
	<LI><TT>LBN_DBLCLK</TT> is sent when a user double-clicks a list-box item.<BR>
	<BR>
	
	<LI><TT>LBN_ERRSPACE</TT> indicates that an action could not be performed due to
	a lack of memory.<BR>
	<BR>
	
	<LI><TT>LBN_KILLFOCUS</TT> is sent just before the list box loses the input focus.<BR>
	<BR>
	
	<LI><TT>LBN_SELCANCEL</TT> is sent when a user cancels a list box selection.<BR>
	<BR>
	
	<LI><TT>LBN_SELCHANGE</TT> is sent when the selection state in a list box is about
	to change.<BR>
	<BR>
	
	<LI><TT>LBN_SETFOCUS</TT> is sent when a list box receives the input focus.
</UL>

<P>The <TT>LBN_DBLCLK</TT> message is the most frequently used notification message.
Most users expect some sort of default action to take place when a list box item
is double-clicked. For example, when a list of filenames is displayed, double-clicking
a particular filename might be expected to open that file for editing.</P>
<P>The steps to add message-handling functions for any of the controls used in Windows
are very similar. To create a message-handling function for the <TT>LBN_DBLCLK</TT>
notification message, follow these steps:

<DL>
	<DD>1. Open ClassWizard and click the Message Maps tab.<BR>
	<BR>
	2. Select the <TT>CListBoxDlg</TT> class and the <TT>IDC_LIST</TT> Object ID.<BR>
	<BR>
	3. Select <TT>LBN_DBLCLK</TT>, and click the Add Function button.<BR>
	<BR>
	4. Accept the suggested function name <TT>CListBoxDlg::OnDblclkList</TT>.<BR>
	<BR>
	5. Click the button labeled Edit Code.<BR>
	<BR>
	6. Add the source code from Listing 7.5 to the <TT>CListBoxDlg::OnDblclkList</TT>
	function.
</DL>

<H4><FONT COLOR="#000077">TYPE: Listing 7.5. Handling a list box notification message.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CListBoxDlg::OnDblclkList()</TT>
<TT>{</TT>
<TT>    int nSelection = m_listBox.GetCurSel();</TT>
<TT>    if( nSelection != LB_ERR )</TT>
<TT>    {</TT>
<TT>        CString szSelection;</TT>
<TT>        m_listBox.GetText( nSelection, szSelection );</TT>

<TT>        AfxMessageBox( szSelection );</TT>
<TT>    }</TT>
<TT>}</TT>
</FONT></PRE>
<P>Compile and run the ListBox project and then double-click any of the list box
items. The <TT>LBN_DBLCLK</TT> message is sent to the <TT>CListBoxDlg::OnDblclkList</TT>
function, and a message box is displayed with information about the selected item.</P>
<P>You can determine the currently selected item in the list box by using the <TT>CListbox::GetCurSel</TT>
member function, as shown in Listing 7.5. The <TT>GetCurSel</TT> member function
returns the position of the currently selected item, with the first item position
starting at zero. If no item is selected, or if the list box has the multiple-selection
property, <TT>LB_ERR</TT> is returned.
<H2><FONT COLOR="#000077"><B>What Are Combo Boxes?</B></FONT></H2>
<P>A combo box control is a single control that combines an edit control with a list
box. A combo box enables a user to enter data either by entering text like an edit
control or by selecting an item from several choices like a list box.</P>
<P>Combo boxes are quite useful when a user is not limited to selecting only the
items presented in a list box. The list box portion of the combo box can be used
to display recent selections, giving the user the freedom to enter a new selection
in the edit control.</P>
<P>There are three types of combo boxes:

<UL>
	<LI>Simple combo boxes display an edit control and list box. Unlike the other combo
	box types, the list box is always visible. When the list box contains more items
	than can be displayed, a scrollbar is used to scroll through the list box.<BR>
	<BR>
	
	<LI>Drop-down combo boxes hide the list box until the user opens it. With this type
	of combo box, the list uses much less room in a dialog box than that used by the
	simple combo box.<BR>
	<BR>
	
	<LI>Drop-down list boxes are similar to drop-down combo boxes in that they display
	the list box only when opened by the user. However, a static-text control is used
	to display the selection instead of an edit control. Therefore, the user is limited
	to selecting items from the list box.
</UL>



<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Combo boxes also
	are used when space in a dialog box is at a premium. A large number of choices in
	a combo box can be hidden until the combo box is opened, enabling more controls to
	be placed in a smaller area than that required for a list box. 
<HR>


</BLOCKQUOTE>

<H3><FONT COLOR="#000077"><B>MFC Support for Combo Boxes</B></FONT></H3>
<P>Just like list boxes and other controls, you normally add combo boxes to dialog
box resources using the Developer Studio dialog box editor. After you add the control,
use ClassWizard to add message-handling functions and associate the control with
a <TT>CComboBox</TT> object.</P>
<P>You use the MFC <TT>CComboBox</TT> class to manage and interact with the combo
box control, and it contains many of the member functions that are available in the
<TT>CListBox</TT> and <TT>CEdit</TT> classes. For example, you can use <TT>GetCurSel</TT>
to get the currently selected item from the list box part of a combo box.
<H3><FONT COLOR="#000077"><B>Combo Box Properties</B></FONT></H3>
<P>A combo box has a large number of properties because it combines an edit control
and a list box. Most edit-control and list-box styles have similar properties that
can be applied to combo boxes. These combo box properties are identical to the list
box properties discussed earlier:

<UL>
	<LI>ID
	<LI>Visible

⌨️ 快捷键说明

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