﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Tauruser-随笔分类-MFC</title><link>http://www.cppblog.com/Tauruser/category/3682.html</link><description>Enjoy Every Day</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 21:04:01 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 21:04:01 GMT</pubDate><ttl>60</ttl><item><title>CListCtrl For Beginners(转载)</title><link>http://www.cppblog.com/Tauruser/archive/2007/03/07/19381.html</link><dc:creator>Tauruser</dc:creator><author>Tauruser</author><pubDate>Wed, 07 Mar 2007 13:23:00 GMT</pubDate><guid>http://www.cppblog.com/Tauruser/archive/2007/03/07/19381.html</guid><wfw:comment>http://www.cppblog.com/Tauruser/comments/19381.html</wfw:comment><comments>http://www.cppblog.com/Tauruser/archive/2007/03/07/19381.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Tauruser/comments/commentRss/19381.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Tauruser/services/trackbacks/19381.html</trackback:ping><description><![CDATA[
		<p>The List Control (MFC <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListCtrl.asp">CListCtrl</a> class) is the type of control that the Windows Explorer uses for it's right-side pane, but figuring out how to use the control can be a different kind of pain. The control is usually used only in "report view", which provides columns for each item. The following shows the basics of adding data to a List Control:<br /></p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #000000">    </span>
				<span style="COLOR: #0000ff">int</span>
				<span style="COLOR: #000000"> Index;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> Insert columns</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">ListCtrl.InsertColumn(</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">One</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, LVCFMT_LEFT, </span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />ListCtrl.InsertColumn(</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">Two</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, LVCFMT_LEFT, </span>
				<span style="COLOR: #000000">-</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> Insert first row</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">Index </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> ListCtrl.InsertItem(LVIF_TEXT, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">One one</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, NULL);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />ListCtrl.SetItem(Index, </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, LVIF_TEXT, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">One two</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, NULL);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> Insert second row</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">Index </span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000"> ListCtrl.InsertItem(LVIF_TEXT, </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">Two one</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, NULL);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />ListCtrl.SetItem(Index, </span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, LVIF_TEXT, </span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">Two two</span>
				<span style="COLOR: #000000">"</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, </span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, NULL);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">//</span>
				<span style="COLOR: #008000"> Set column widths (an optional nice touch)</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">ListCtrl.SetColumnWidth(</span>
				<span style="COLOR: #000000">0</span>
				<span style="COLOR: #000000">, LVSCW_AUTOSIZE);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />ListCtrl.SetColumnWidth(</span>
				<span style="COLOR: #000000">1</span>
				<span style="COLOR: #000000">, LVSCW_AUTOSIZE);</span>
		</div>
		<br />If you are using CListView, then you will need to use something such as the following to access the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListCtrl.asp">CListCtrl</a>:<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #000000">CListCtrl </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">ListCtrl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> GetListCtrl();</span></div><p>Note that for these to work, the control must have the "LVS_REPORT" style. For list controls in dialogs, including dialogs for <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CFormView.asp">CFormView</a>, this style can be set in the dialog editor. If you are using <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListView.asp">CListView</a> or you are creating list controls using Create, the style can be set in a PreCreateWindow override, as in:<br /><br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Codehighlighter1_50_115_Open_Image" onclick="this.style.display='none'; Codehighlighter1_50_115_Open_Text.style.display='none'; Codehighlighter1_50_115_Closed_Image.style.display='inline'; Codehighlighter1_50_115_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_50_115_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_50_115_Closed_Text.style.display='none'; Codehighlighter1_50_115_Open_Image.style.display='inline'; Codehighlighter1_50_115_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="COLOR: #000000">BOOL CDumpView::PreCreateWindow(CREATESTRUCT</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> cs) </span><span id="Codehighlighter1_50_115_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_50_115_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />cs.style </span><span style="COLOR: #000000">|=</span><span style="COLOR: #000000"> LVS_REPORT;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> CListView::PreCreateWindow(cs);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div><p align="left"><br /></p><p align="left"><font size="2"><strong><font size="5">Getting a Count of Columns<br /></font><br /></strong></font></p><font size="3">The solution for determining the number of columns is not obvious. You must get the header control from the list control and get the number of items (rows) in the header control. So I suggest putting the following in your header:</font><p></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Codehighlighter1_35_71_Open_Image" onclick="this.style.display='none'; Codehighlighter1_35_71_Open_Text.style.display='none'; Codehighlighter1_35_71_Closed_Image.style.display='inline'; Codehighlighter1_35_71_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_35_71_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_35_71_Closed_Text.style.display='none'; Codehighlighter1_35_71_Open_Image.style.display='inline'; Codehighlighter1_35_71_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="COLOR: #000000">CHeaderCtrl </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">GetHeaderCtrl() </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span id="Codehighlighter1_35_71_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_35_71_Open_Text"><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (CHeaderCtrl</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)GetDlgItem(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);}</span></span><span style="COLOR: #000000">;<br /><img id="Codehighlighter1_102_142_Open_Image" onclick="this.style.display='none'; Codehighlighter1_102_142_Open_Text.style.display='none'; Codehighlighter1_102_142_Closed_Image.style.display='inline'; Codehighlighter1_102_142_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_102_142_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_102_142_Closed_Text.style.display='none'; Codehighlighter1_102_142_Open_Image.style.display='inline'; Codehighlighter1_102_142_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> GetColumnsCount() </span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> </span><span id="Codehighlighter1_102_142_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_102_142_Open_Text"><span style="COLOR: #000000">{</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> GetHeaderCtrl()</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">GetItemCount();}</span></span><span style="COLOR: #000000">;</span></div><strong><font size="5"><p align="left"><br />Deleting All Columns</p></font></strong><p>Using GetColumnsCount() from above, it is easy to delete all columns.<br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Codehighlighter1_38_112_Open_Image" onclick="this.style.display='none'; Codehighlighter1_38_112_Open_Text.style.display='none'; Codehighlighter1_38_112_Closed_Image.style.display='inline'; Codehighlighter1_38_112_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_38_112_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_38_112_Closed_Text.style.display='none'; Codehighlighter1_38_112_Open_Image.style.display='inline'; Codehighlighter1_38_112_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> CListControl::DeleteAllColumns() </span><span id="Codehighlighter1_38_112_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><img src="http://www.cppblog.com/images/dot.gif" /></span><span id="Codehighlighter1_38_112_Open_Text"><span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> i, n;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />n </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> GetColumnsCount();<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> (i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">; i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">n; </span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">i)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    DeleteColumn(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div><h2 style="MARGIN-TOP: 40px">Confusing Terminology</h2><p><br /></p><p>Microsoft has made things very confusing because the Platform SDK name for a listbox is "ListBox Control" but MFC programmers usually use "list control" to refer to an instance of the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListCtrl.asp">CListCtrl</a> class, which is confusing. Also, the MFC class name for a "List View Control" is <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListCtrl.asp">CListCtrl</a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListView.asp">CListView</a> is the view class for it. The MFC class name for a "ListBox Control" is <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListBox.asp">CListBox</a> so we would expect the view class for it to also be CListView except there is not a view class. The following summarizes this. 
</p><p style="MARGIN-LEFT: 40px; TEXT-INDENT: -20px"><b>Platform SDK name:</b><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/listboxes/listboxes.asp">List Boxes</a><br /><b>Window class name:</b> ListBox (See under "Predefined Controls" in <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/Common/user.asp">User Controls</a> and the documentation for <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/CreateWindow.asp">CreateWindow</a> and <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/CreateWindowEx.asp">CreateWindowEx</a>)<br /><b>MFC class name:</b><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListBox.asp">CListBox</a><br /><b>MFC view class name:</b> none</p><p style="MARGIN-LEFT: 40px; TEXT-INDENT: -20px"><b>Platform SDK name:</b><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/ListView/ListView.asp">ListView Control </a><br /><b>Window class name:</b> SysListView32 (use WC_LISTVIEW in source code instead, which is defined as "SysListView32")<br /><b>MFC class name:</b><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListCtrl.asp">CListCtrl</a><br /><b>MFC view class name:</b><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_CListView.asp">CListView</a></p><p><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></p><img src ="http://www.cppblog.com/Tauruser/aggbug/19381.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Tauruser/" target="_blank">Tauruser</a> 2007-03-07 21:23 <a href="http://www.cppblog.com/Tauruser/archive/2007/03/07/19381.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MFC ComboBox 使用方法（转载）</title><link>http://www.cppblog.com/Tauruser/archive/2007/02/28/19038.html</link><dc:creator>Tauruser</dc:creator><author>Tauruser</author><pubDate>Wed, 28 Feb 2007 02:29:00 GMT</pubDate><guid>http://www.cppblog.com/Tauruser/archive/2007/02/28/19038.html</guid><wfw:comment>http://www.cppblog.com/Tauruser/comments/19038.html</wfw:comment><comments>http://www.cppblog.com/Tauruser/archive/2007/02/28/19038.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/Tauruser/comments/commentRss/19038.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Tauruser/services/trackbacks/19038.html</trackback:ping><description><![CDATA[
		<h1 align="center">MFC ComboBox 使用方法（转载）<br /></h1>
		<br />Combo box controls are space savers. Wherever there is no need for a multi-select from a list of items, combo box is a good choice in such places. This article " CComboBox Example" explains how to use the MFC CComboBox class for manipulation of a list of strings. 
<h2 sid92="0" sdqik="0">CComboBox Example - Initializing a Combo Box:</h2><p sid92="0" sdqik="0">   It is assumed that the readers of the sample have already created a <a class="kLink" oncontextmenu="return false;" id="KonaLink0" onmouseover="adlinkMouseOver(event,this,0);" style="POSITION: static; TEXT-DECORATION: underline! important" onclick="adlinkMouseClick(event,this,0);" onmouseout="adlinkMouseOut(event,this,0);" href="http://www.codersource.net/mfc_ccombobox_example.html#" target="_top"><font style="FONT-WEIGHT: 400; COLOR: blue! important; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: static" color="blue" size="2"><span class="kLink" style="FONT-WEIGHT: 400; COLOR: blue! important; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative">dialog </span><span class="kLink" style="FONT-WEIGHT: 400; COLOR: blue! important; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: relative">box</span></font></a> (either in a dialog based application or SDI/MDI application) and placed a combo box control from the controls toolbox on the Resource Editor. </p><p sid92="6" sdqik="0">   After placing the combo box control on the dialog box, open the class wizard by pressing <i sid92="0" sdqik="0">Ctrl + W </i>keys or <i sid92="0" sdqik="0">Menu --&gt; View --&gt; ClassWizard</i>. In the <i sid92="0" sdqik="0">Member Variables</i> tab, Add a Variable for the CComboBox class. This CComboBox example assumes that the variable name is, </p><p sid92="0" sdqik="0">      CComboBox  m_cbExample; </p><p sid92="0" sdqik="0">   This m_cbExample will be used further in our CComboBox example MFC code. </p><p><!-- Ad Link Place Holder here --></p><div align="center"><p><adlinks></adlinks></p></div><p></p><h2 sid92="0" sdqik="0">CComboBox Example - Adding Items to a Combo Box:</h2><p sid92="2" sdqik="0">   The function AddString is used for adding items to a combo box. If there is a constant set of data, these values can also be added in the Resource Editor itself. The Combo Box control properties dialog has a tab for adding <i sid92="0" sdqik="0">data</i>. Otherwise the data can be added as follows. </p><p sid92="4" sdqik="0">    m_cbExample.AddString("StringData1");<br />    m_cbExample.AddString("StringData2");<br />    m_cbExample.AddString("StringData3"); </p><h2 sid92="0" sdqik="0">CComboBox Example - Retrieving Items from a Combo Box:</h2><p sid92="2" sdqik="0">   Usually <endl;><br />a requirement for retrieving items from the combo box will arise from selecting the data. This article also assumes the same. Now the data selected in a combo box needs to be retrieved. 
<p sid92="2" sdqik="0">   To do this, the first step is to find out the index of the selected item inside the combo box control.<endl;><br />Then the item at the corresponding position needs to be pulled out as follows. 
<p sid92="8" sdqik="0"><endl;><br />    int nIndex = m_cbExample.GetCurSel();<br />    CString strCBText;<br /><endl;><br />    m_cbExample.GetLBText(<endl;><br />nIndex, strCBText); 
<p sid92="1" sdqik="0"><endl;><br />   In the above CComboBox example code, the value will be retrieved and stored in strCBText variable. There is another overloaded version for GetLBText. But the version which uses CString is the easiest one. 
<p><endl;><br /><h3 sid92="0" sdqik="0">CComboBox Example - Finding Items inside a Combo Box:</h3><p sid92="1" sdqik="0"><endl;><br />   This kind of Find operations on a Combo box will most probably be useful in programs that dynamically modify the values in a combo box. The function FindStringExact is used to find the exact string match inside a combo box. 
<p sid92="1" sdqik="0"><endl;><br />    int nIndex = m_cbExample.FindStringExact(0, "Value to be found");<endl;><br /><p sid92="0" sdqik="0">   The string position inside the combo box control is the return value. It returns CB_ERR if it was unsuccessful in finding the string. </p><p><endl;><br /><h3 sid92="0" sdqik="0">CComboBox Example - Deleting Items from a Combo Box:</h3><p sid92="1" sdqik="0"><endl;><br />    This operation can be done by using the CCombobox member function DeleteString. This function needs the index of the item inside the combo box. 
<p sid92="3" sdqik="0"><endl;><br />     m_cbExample<endl;><br />.DeleteString(nIndex);</endl;></endl;></p></endl;></p><p></p></endl;></p><p></p></endl;></endl;></p><p></p></endl;></p><p></p></endl;></p><p></p></endl;></p><p></p></endl;></endl;></endl;></p><p></p></endl;></p><p></p></endl;></p><p></p><img src ="http://www.cppblog.com/Tauruser/aggbug/19038.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Tauruser/" target="_blank">Tauruser</a> 2007-02-28 10:29 <a href="http://www.cppblog.com/Tauruser/archive/2007/02/28/19038.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>