﻿<?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++博客-Late Not Bad-文章分类-MFC</title><link>http://www.cppblog.com/lateCpp/category/16065.html</link><description>不带感情的学习技术</description><language>zh-cn</language><lastBuildDate>Thu, 25 Aug 2011 05:29:14 GMT</lastBuildDate><pubDate>Thu, 25 Aug 2011 05:29:14 GMT</pubDate><ttl>60</ttl><item><title>char []　到　LPWSTR转换的一个具体应用</title><link>http://www.cppblog.com/lateCpp/articles/153358.html</link><dc:creator>lateCpp</dc:creator><author>lateCpp</author><pubDate>Sun, 14 Aug 2011 09:17:00 GMT</pubDate><guid>http://www.cppblog.com/lateCpp/articles/153358.html</guid><wfw:comment>http://www.cppblog.com/lateCpp/comments/153358.html</wfw:comment><comments>http://www.cppblog.com/lateCpp/articles/153358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lateCpp/comments/commentRss/153358.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lateCpp/services/trackbacks/153358.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-size: 14pt; "><strong>背景：</strong></span><div><span style="font-size: 12px;">1. &nbsp; &nbsp; &nbsp; Unicode环境</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">2.<span style="white-space:pre">	</span>使用了CTreeCtrl控件 TV_ITEM类型(item) item.pszText变量　<span style="white-space:pre">		</span>&nbsp;</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 14pt; "><strong>问题：</strong></span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">定义了一个char</span></div><div><span style="font-size: 12px;">text[31]数组，用来接收选中的TV_ITEM　item的item.pszText。然后将该字符数组的值显示给一个static</span></div><div><span style="font-size: 12px;">text 控件。即，显示当前选中的树形目录的item的标题。在执行语句：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">item.pszText&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;text</span></div></span></div><div><span style="font-size: 12px;">时，报错：&#8220;无法将char[] 转换为　LPWSTR&#8221;。</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 14pt; "><strong>解决：</strong></span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">1.思路一：</span></div><div><span style="font-size: 12px;"><span style="white-space:pre">	</span>使用CA2W字符转换宏（ATL and MFC String Conversion Macros</span></div><div><span style="font-size: 12px;"><span style="white-space:pre">	</span>）。根据MSDN描述，这个宏用于将ANSI转换为Wide Character（UNICODE）</span></div><div><span style="font-size: 12px;">代码如下：</span></div><div><div style="background-color: #eeeeee; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><span class="Apple-style-span" style="font-size: 13px; "><span style="color: #008080; ">1</span>&nbsp;<span style="color: #000000; ">LPWSTR&nbsp;&nbsp;&nbsp;aaa&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;CA2W(text);</span></span></div><div style="padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; background-color: #eeeeee; font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; width: 997px; word-break: break-all; "><span style="color: #008080; ">2</span>&nbsp;<span style="color: #000000; ">item.pszText&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;aaa;</span></div><span class="Apple-style-span" style="font-size: 12px; ">2.思路二：</span></div><div><span style="font-size: 12px;"><span style="white-space:pre">	</span>使用int MultiByteToWideChar（）函数。根据MSDN描述，这个方法：This</span></div><div><span style="font-size: 12px;"><span style="white-space:pre">	</span>function maps a character string to a wide-character (Unicode)</span></div><div><span style="font-size: 12px;"><span style="white-space:pre">	</span>string。</span></div><div><span style="font-size: 12px;">代码如下：<br /><br /></span><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">1</span>&nbsp;<span style="color: #000000; ">TCHAR&nbsp;aaa[</span><span style="color: #000000; ">31</span><span style="color: #000000; ">];<br /></span><span style="color: #008080; ">2</span>&nbsp;<span style="color: #000000; ">MultiByteToWideChar(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,text,</span><span style="color: #000000; ">31</span><span style="color: #000000; ">,aaa,</span><span style="color: #000000; ">62</span><span style="color: #000000; ">);</span></div></div><div><span style="font-size: 14pt; "><strong>总结：</strong></span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">　使用第一种方法，直接用类型LPWSTR（错误信息里要求）来定义一个变量，然后接收转换过后字符串。简单明了，参数也少。</span></div><div><span style="font-size: 12px;">　第二种方法，MultiByteToWideChar()函数，首先得用TCHAR来定义一个数组，其次参数太多，引入新的理解问题。</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">　另外：网上有另外推荐将项目的unicode环境关掉，就可避免这个问题。不推荐，这实际上是回避问题。现在已经是Unicode的应用已经越来越广，程序不支持Unicode环境，走的了一时，走不了一世。<br /><br /><strong>该函数的完整代码：</strong><br /><div style="font-size: 13px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; background-color: #eeeeee; "><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080; ">&nbsp;1</span>&nbsp;<span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;CDialog_TREE::OnTvnSelchangedTree1(NMHDR&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pNMHDR,&nbsp;LRESULT&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pResult)<br /></span><span style="color: #008080; ">&nbsp;2</span>&nbsp;<span style="color: #000000; ">{<br /></span><span style="color: #008080; ">&nbsp;3</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;LPNMTREEVIEW&nbsp;pNMTreeView&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;reinterpret_cast</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">LPNMTREEVIEW</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">(pNMHDR);<br /></span><span style="color: #008080; ">&nbsp;4</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;TODO:&nbsp;在此添加控件通知处理程序代码</span><span style="color: #008000; "><br /></span><span style="color: #008080; ">&nbsp;5</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;CTreeCtrl</span><span style="color: #000000; ">*</span><span style="color: #000000; ">&nbsp;pTree&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;(CTreeCtrl</span><span style="color: #000000; ">*</span><span style="color: #000000; ">)GetDlgItem(IDC_TREE1);<br /></span><span style="color: #008080; ">&nbsp;6</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;HTREEITEM&nbsp;hSelected&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;pNMTreeView</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">itemNew.hItem;<br /></span><span style="color: #008080; ">&nbsp;7</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">&nbsp;(hSelected&nbsp;</span><span style="color: #000000; ">!=</span><span style="color: #000000; ">&nbsp;NULL)<br /></span><span style="color: #008080; ">&nbsp;8</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;{<br /></span><span style="color: #008080; ">&nbsp;9</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><strong><span style="color: #0000FF; ">char</span><span style="color: #000000; ">&nbsp;text[</span><span style="color: #000000; ">31</span></strong><span style="color: #000000; "><strong>];</strong><br /></span><span style="color: #008080; ">10</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TV_ITEM&nbsp;item;<br /></span><span style="color: #008080; ">11</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.mask&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;TVIF_HANDLE&nbsp;</span><span style="color: #000000; ">|</span><span style="color: #000000; ">&nbsp;TVIF_TEXT;<br /></span><span style="color: #008080; ">12</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.hItem&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;hSelected;<br /></span><span style="color: #008080; ">13</span>&nbsp;<span style="color: red; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: red; "><strong>LPWSTR&nbsp;&nbsp;&nbsp;aaa&nbsp;&nbsp;&nbsp;</strong></span><span style="font-weight: bold; color: red; ">=</span><span style="font-weight: bold; color: red; ">&nbsp;&nbsp;&nbsp;CA2W(text);</span><span style="font-weight: bold; color: #000000; "><br /></span><span style="color: #008080; ">14</span><strong>&nbsp;</strong><span style="font-weight: bold; color: #000000; ">&nbsp; &nbsp; &nbsp; &nbsp; 　</span><span style="font-weight: bold; color: red; ">item.pszText&nbsp;</span><span style="font-weight: bold; color: red; ">=</span><span style="color: red; "><strong>&nbsp;aaa;</strong></span><span style="color: #000000; "><br /></span><span style="color: #008080; ">15</span>&nbsp;<span style="color: #000000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>TCHAR&nbsp;aaa[31];</strong><br /></span><span style="color: #008080; ">16</span>&nbsp;<span style="color: #008000; "></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>&nbsp;MultiByteToWideChar(0,0,text,31,aaa,62);</strong></span><span style="color: #008000; "><br /></span><span style="color: #008080; ">17</span>&nbsp;<span style="color: #008000; "></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item.cchTextMax&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">30</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">18</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VERIFY(pTree</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">GetItem(</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">item));<br /></span><span style="color: #008080; ">19</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetDlgItemText(IDC_STATIC_SELECTED,</span><span style="color: red; "><strong>aaa</strong></span><span style="color: #000000; ">);<br /></span><span style="color: #008080; ">20</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;}<br /></span><span style="color: #008080; ">21</span>&nbsp;<span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pResult&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br /></span><span style="color: #008080; ">22</span>&nbsp;<span style="color: #000000; ">}</span></div><br />注意，第１９行SetDlgItemText()的时候，要使用转换过的字符串，即aaa。不然仍然会报错。<br /><br /><br /></span></div><div style="color: black; font-family: Tahoma; font-size: 11px; "></div><img src ="http://www.cppblog.com/lateCpp/aggbug/153358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lateCpp/" target="_blank">lateCpp</a> 2011-08-14 17:17 <a href="http://www.cppblog.com/lateCpp/articles/153358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VS2005因错误设置目录引起的错误</title><link>http://www.cppblog.com/lateCpp/articles/144259.html</link><dc:creator>lateCpp</dc:creator><author>lateCpp</author><pubDate>Thu, 14 Apr 2011 14:43:00 GMT</pubDate><guid>http://www.cppblog.com/lateCpp/articles/144259.html</guid><wfw:comment>http://www.cppblog.com/lateCpp/comments/144259.html</wfw:comment><comments>http://www.cppblog.com/lateCpp/articles/144259.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lateCpp/comments/commentRss/144259.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lateCpp/services/trackbacks/144259.html</trackback:ping><description><![CDATA[<p>来源自：<span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; FONT: medium 微软雅黑; WHITE-SPACE: normal; ORPHANS: 2; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="TEXT-ALIGN: left; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, sans-serif; FONT-SIZE: 15px" class=Apple-style-span>：<a style="COLOR: rgb(51,102,153); TEXT-DECORATION: none" href="http://blog.csdn.net/li_007/archive/2007/06/29/1671563.aspx">http://blog.csdn.net/li_007/archive/2007/06/29/1671563.aspx</a><br><br>今天从<a href="http://www.opencv.org.cn/index.php/%E4%BD%BF%E7%94%A8DirectShow%E9%87%87%E9%9B%86%E5%9B%BE%E5%83%8F#.E4.BE.8B.E7.A8.8B"><u><font color=#0066cc size=3>http://www.opencv.org.cn/index.php/%E4%BD%BF%E7%94%A8DirectShow%E9%87%87%E9%9B%86%E5%9B%BE%E5%83%8F#.E4.BE.8B.E7.A8.8B</font></u></a>下载于世琪老师的Directshow类，用来检测自己机子上的USB摄像头（微点公司MVCII-1M）到能否连上。<br><br>照着教程，一步一步来，却有如下编译错误：<br><br><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; FONT: medium 微软雅黑; WHITE-SPACE: normal; ORPHANS: 2; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="TEXT-ALIGN: left; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, sans-serif; FONT-SIZE: 15px" class=Apple-style-span><strong>C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(222) : error C2146: 语法错误: 缺少&#8220;;&#8221;(在标识符&#8220;PVOID64&#8221;的前面)<br>C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(222) : error C4430: 缺少类型说明符- 假定为int。注意: C++ 不支持默认int<br>C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(5940) : error C2146: 语法错误: 缺少&#8220;;&#8221;(在标识符&#8220;Buffer&#8221;的前面)<br>C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(5940) : error C4430: 缺少类型说明符- 假定为int。注意: C++ 不支持默认int<br>C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\winnt.h(5940) : error C4430: 缺少类型说明符- 假定为int。注意: C++ 不支持默认int<br><br></strong>令我很是不解。<br><br>最后在网上查找一下，发现是包含目录顺序的关系：<br><br>在包含于老师的DirectShow/Inlude时，放在了：<span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; FONT: medium 微软雅黑; WHITE-SPACE: normal; ORPHANS: 2; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="LINE-HEIGHT: 21px; FONT-FAMILY: Tahoma, 'Lucida Grande', Verdana, Helvetica, Arial, sans-serif; FONT-SIZE: 14px" class=Apple-style-span></p>
<pre style="BORDER-BOTTOM: rgb(47,111,171) 1px dashed; BORDER-LEFT: rgb(47,111,171) 1px dashed; PADDING-BOTTOM: 1em; LINE-HEIGHT: 1.1em; BACKGROUND-COLOR: rgb(249,249,249); PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; COLOR: black; BORDER-TOP: rgb(47,111,171) 1px dashed; BORDER-RIGHT: rgb(47,111,171) 1px dashed; PADDING-TOP: 1em" class=c><span style="FONT-STYLE: italic; COLOR: rgb(102,102,102)">$(VCInstallDir)PlatformSDK\include和$(FrameworkSDKDir)include</span></pre>
两个目录上面。<br>调整&#8220;<strong>C:\OpenCV2.0\vs2005\DirectShow\Include</strong>&#8221;到包含目录的最底下一行，再编译，还是没通过。<br><br>再查，发现自己画蛇添足。<br>在：项目——xx工程属性——Ｃ/Ｃ++——常规——附加包含目录里也加上了<font size=3 face=Verdana>DirectShow/Inlude。<br>当然，在linker里的也加上了相应的库。<br><br>将这两项都删掉。<br><br>再编译，顺利通过。<br><br>回头再看于老师的使用说明：<br><br><span style="WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; FONT: medium 微软雅黑; WHITE-SPACE: normal; ORPHANS: 2; COLOR: rgb(0,0,0); WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class=Apple-style-span><span style="LINE-HEIGHT: 21px; FONT-FAMILY: Tahoma, 'Lucida Grande', Verdana, Helvetica, Arial, sans-serif; FONT-SIZE: 14px" class=Apple-style-span>
<pre style="BORDER-BOTTOM: rgb(47,111,171) 1px dashed; BORDER-LEFT: rgb(47,111,171) 1px dashed; PADDING-BOTTOM: 1em; LINE-HEIGHT: 1.1em; BACKGROUND-COLOR: rgb(249,249,249); PADDING-LEFT: 1em; PADDING-RIGHT: 1em; FONT-FAMILY: monospace; COLOR: black; BORDER-TOP: rgb(47,111,171) 1px dashed; BORDER-RIGHT: rgb(47,111,171) 1px dashed; PADDING-TOP: 1em" class=c>在vc++2005开发环境下的使用说明：
//    1.将CameraDS.h CameraDS.cpp复制到你的项目中
//    2.将DirectShow复制到你的opencv根目录下，菜单 工具-&gt;选项-&gt;项目和解决方案-&gt;vc++目录，把..(你的opencv安装目录)/DirectShow/Include添加到
//   &#8220;引用文件&#8221;中</font><span style="COLOR: red">$(VCInstallDir)PlatformSDK\include和$(FrameworkSDKDir)include下面任意位置</span>
//    3.菜单 工具-&gt;选项-&gt;项目和解决方案-&gt;vc++目录，把..(你的opencv安装目录)/DirectShow/Lib添加到&#8220;库文件&#8221;下面。也可参考使用说明3。</pre>
怪自己不细心啊。<br><br>＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝<br>遗憾的是，这次检测结果证明，还是不能调用微视公司的这个摄像头。——看来得想办法找到该摄像头的DirectShow驱动了。</span></span></span></span></span></span></span></span>
<img src ="http://www.cppblog.com/lateCpp/aggbug/144259.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lateCpp/" target="_blank">lateCpp</a> 2011-04-14 22:43 <a href="http://www.cppblog.com/lateCpp/articles/144259.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cstring 和 const char* , unicode和ANSI编码 的一个具体应用</title><link>http://www.cppblog.com/lateCpp/articles/142060.html</link><dc:creator>lateCpp</dc:creator><author>lateCpp</author><pubDate>Thu, 17 Mar 2011 08:57:00 GMT</pubDate><guid>http://www.cppblog.com/lateCpp/articles/142060.html</guid><wfw:comment>http://www.cppblog.com/lateCpp/comments/142060.html</wfw:comment><comments>http://www.cppblog.com/lateCpp/articles/142060.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lateCpp/comments/commentRss/142060.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lateCpp/services/trackbacks/142060.html</trackback:ping><description><![CDATA[<p style="FONT-SIZE: 12pt">&nbsp; </p>
<h2><span>参考文章如下：</span></h2>
<p align=left><span><span>l&nbsp;</span></span><span>网上流传甚广的一个大牛写的</span><span>&#8220;</span><span>关于</span><span>CString</span><span>的技巧</span><span>&#8221;</span><span>，没能找到原文，转载地址如下<strong>：</strong></span><span><a href="http://blog.csdn.net/coolstar14/archive/2004/07/15/41885.aspx">http://blog.csdn.net/coolstar14/archive/2004/07/15/41885.aspx</a><strong></strong></span></p>
<p align=left><span><span>l&nbsp;</span></span><span>CodeProject</span><span>上一篇</span><span>&#8220;CString Managment&#8221;</span><span>地址：</span><strong><span> &nbsp; &nbsp;</span></strong><span><a href="http://www.codeproject.com/KB/string/cstringmgmt.aspx">http://www.codeproject.com/KB/string/cstringmgmt.aspx</a><strong></strong></span></p>
<p><span><span>l&nbsp;</span></span><span>CodeProject</span><span>上的</span><span>FAQ</span><span>里的一些解答：</span><span><a href="http://www.codeproject.com/KB/cpp/cppforumfaq.aspx#mfc_cstrtopchar">http://www.codeproject.com/KB/cpp/cppforumfaq.aspx#mfc_cstrtopchar</a></span></p>
<p><span><span>l&nbsp;</span></span><span>MSDN</span><span>里关于</span><span>WideCharToMultiByte()</span><span>函数的说明及例子。</span></p>
<h2><span>背景：</span></h2>
<p><span><span>1.<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span>Unicode</span><span>环境</span></p>
<p><span><span>2.<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span>CSmtp</span><span>类是从网上下载的一个类，实现了利用</span><span>smtp</span><span>协议发送邮件。具有很多方法，可以设置帐户名、密码等，对应的方法为</span><span>CSmtp::SetLogin(const char*)</span><span>、</span><span>CSmtp::SetPassword(const char*)</span><span>等。因为是别人的作品，我并无能力改写这些函数的参数类型。</span></p>
<h2><span>问题</span></h2>
<p align=left><span><span>1.<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span>CString</span><span>——</span><span>&gt;const char*:</span><span>将</span><span>Edit</span><span>控件里用户输入的字符串，保存到</span><span>Cstring</span><span>类型的控件变量里。经过<strong>处理，</strong>然后将此字符串作为函数</span><span>m_CSmtpMail.SetLogin(const char*)</span><span>的参数输入。</span></p>
<p align=left><span><span>2.<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span>const char*</span><span>——</span><span>&gt;LPCTSTR:</span><span>将捕捉异常函数</span><span>e.GetErrorText().c_str()</span><span>的结果转换为</span><span>LPCTSTR</span><span>，作为</span><span>MessageBox</span><span>（</span><span>LPCTSTR</span><span>）函数的参数。</span></p>
<h2><span>解决：</span></h2>
<p><span>对于第一个问题：</span></p>
<p><span>1</span><span>：先将</span><span>Cstring</span><span>变量转换为</span><span>Unicode </span><span>的字符指针，即</span><span>TCHAR*</span></p>
<p><span>2</span><span>：再利用</span><span>WideCharToMultiByte()</span><span>函数将</span><span>unicode</span><span>字符串转换为</span><span>ANSI</span><span>字符串</span></p>
<p><span><span>3.<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></span><span>将转换后的结果字符串传递给</span><span>SetLogin(const char*)</span><span>函数，</span><span>MFC</span><span>根据参数类型要求，<strong>自动将</strong></span><strong><span>char * </span></strong><strong><span>转换成</span><span>const char*</span></strong></p>
<p><span>代码如下：</span></p>
<p>&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 12pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">m_EditAccount为控件变量<br></span><span style="COLOR: #008080">2</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">m_CSmtpMail为CSmtp对象</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">3</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;tempAct&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;m_EditAccount.GetBuffer();<br></span><span style="COLOR: #008080">4</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">&nbsp;mbTempAct[</span><span style="COLOR: #000000">256</span><span style="COLOR: #000000">];<br></span><span style="COLOR: #008080">5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">WideCharToMultiByte(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,tempAct,</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,mbTempAct,</span><span style="COLOR: #000000">100</span><span style="COLOR: #000000">,Null,Null);<br></span><span style="COLOR: #008080">6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">m_CSmtpMail.SetLogin(mbtempAct);</span></div>
<p>&nbsp;</p>
<p><span>对于第二个问题：</span></p>
<p><span>1. e.GetErrorText().c_str()</span><span>的结果实际上是一个字符数组的首地址指针，一个</span><span>const char*</span><span>类型。利用</span><span>Cstring</span><span>的构造函数，可以很容易的将一个字符指针转换为</span><span>Cstring</span><span>类型。</span></p>
<p><span>2.</span><span>将</span><span>Cstring</span><span>类型传递给</span><span>MessageBox()。</span><span>MFC</span><span>根据参数类型要求，利用LPCTSTR操作符，<strong>自动将</strong></span><strong><span>Cstring</span></strong><strong><span>转换为</span><span>LPCTSTR</span></strong><strong><span>类型</span></strong><span>。LPCTSTR与CString的转换就是这么简单，这一点在很多讲解的</span><span>Cstring</span><span>的文章里都可以找到。</span></p>
<p><span>3.</span><span>相比第一个问题，这里少掉了</span><span>ANSI</span><span>编码到</span><span>unicode</span><span>编码相互转换的过程。这应该是</span><span>Cstring</span><span>类封装完善的缘故，在构造函数</span><span>Cstring str4(e.GetErrorText().c_str())</span><span>这一步自动完成了。</span></p>
<p><span>代码如下：</span></p>
<p>&nbsp;</p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px">1<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="FONT-SIZE: 12pt">Cstring&nbsp;cstr4(e.GetErrorText().c_str());<br>2<img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif">MessageBox(cstr4);</span></div>
<img src ="http://www.cppblog.com/lateCpp/aggbug/142060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lateCpp/" target="_blank">lateCpp</a> 2011-03-17 16:57 <a href="http://www.cppblog.com/lateCpp/articles/142060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Attach函数的讲解</title><link>http://www.cppblog.com/lateCpp/articles/140631.html</link><dc:creator>lateCpp</dc:creator><author>lateCpp</author><pubDate>Fri, 25 Feb 2011 02:18:00 GMT</pubDate><guid>http://www.cppblog.com/lateCpp/articles/140631.html</guid><wfw:comment>http://www.cppblog.com/lateCpp/comments/140631.html</wfw:comment><comments>http://www.cppblog.com/lateCpp/articles/140631.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lateCpp/comments/commentRss/140631.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lateCpp/services/trackbacks/140631.html</trackback:ping><description><![CDATA[<div style="PADDING-BOTTOM: 1pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; PADDING-TOP: 0cm">
<p style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0cm; TEXT-INDENT: 28pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; BORDER-RIGHT: medium none; PADDING-TOP: 0cm; mso-border-bottom-alt: solid windowtext .75pt; mso-padding-alt: 0cm 0cm 1.0pt 0cm" class=MsoNormal><span lang=EN-US>MSDN</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">上的函数说明：</span></p>
</div>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>CWnd</span><span><span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: black; FONT-SIZE: 13pt; mso-fareast-font-family: 微软雅黑" lang=EN-US>::</span></span><span lang=EN-US>Attach</span><span><span style="FONT-FAMILY: 'Verdana','sans-serif'; COLOR: black; FONT-SIZE: 13pt; mso-fareast-font-family: 微软雅黑" lang=EN-US><o:p></o:p></span></span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>Attaches a Windows window to a&nbsp;CWnd&nbsp;object.</span></p>
<p style="TEXT-INDENT: 28.1pt" class=MsoNormal><strong><span style="COLOR: black; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi" lang=EN-US>BOOL</span></strong><span lang=EN-US> <strong><span style="COLOR: black; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Attach(</span></strong></span></p>
<p style="TEXT-INDENT: 28.1pt" class=MsoNormal><strong><span style="COLOR: black; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi" lang=EN-US><span style="mso-spacerun: yes">&nbsp;&nbsp; </span>HWND</span></strong><span lang=EN-US> <span class=parameter><span style="COLOR: black">hWndNew</span></span> </span></p>
<p style="TEXT-INDENT: 28.1pt" class=MsoNormal><strong><span style="COLOR: black; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi" lang=EN-US>);</span></strong><span lang=EN-US><o:p></o:p></span></p>
<p style="LINE-HEIGHT: normal; TEXT-INDENT: 28.1pt" class=MsoNormal><strong><span lang=EN-US></span>Parameters<o:p></o:p></strong></p>
<p style="LINE-HEIGHT: normal; TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>hWndNew</span></p>
<div style="PADDING-BOTTOM: 1pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; PADDING-TOP: 0cm">
<p style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0cm; LINE-HEIGHT: normal; TEXT-INDENT: 28pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; BORDER-RIGHT: medium none; PADDING-TOP: 0cm; mso-border-bottom-alt: solid windowtext .75pt; mso-padding-alt: 0cm 0cm 1.0pt 0cm" class=MsoNormal><span lang=EN-US>Specifies a handle to a Windows window.</span></p>
</div>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">一个概念：</span><strong style="mso-bidi-font-weight: normal"><span lang=EN-US>C++</span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象和</span><span lang=EN-US>Windows</span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象的区别</span><span lang=EN-US>(</span></strong><strong style="mso-bidi-font-weight: normal"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">来自</span><span lang=EN-US>MSDN)<o:p></o:p></span></strong></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>The window <em>object</em> is an object of the C++ <strong>CWnd</strong> class (or a derived class) that your program creates directly. It comes and goes in response to your program's constructor and destructor calls. The Windows <em>window</em>, on the other hand, is an opaque handle to an internal Windows data structure that corresponds to a window and consumes system resources when present. A Windows window is identified by a "window handle" (<strong>HWND</strong>) and is created after the <strong>CWnd</strong> object is created by a call to the <strong>Create</strong> member function of class <strong>CWnd</strong>. The window may be destroyed either by a program call or by a user's action. The window handle is stored in the window object's <strong>m_hWnd</strong> member variable. The following figure shows the relationship between the C++ window object and the Windows window. Creating windows is discussed in <span style="COLOR: red">Creating Windows</span>. Destroying windows is discussed in <span style="COLOR: red">Destroying Window Objects</span></span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US><o:p>&nbsp;<img border=0 alt="" src="http://www.cppblog.com/images/cppblog_com/latecpp/Windowswindow.png" width=451 height=143></o:p></span></p>
<div style="PADDING-BOTTOM: 1pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; PADDING-TOP: 0cm">
<p style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0cm; TEXT-INDENT: 28pt; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; BORDER-TOP: medium none; BORDER-RIGHT: medium none; PADDING-TOP: 0cm; mso-border-bottom-alt: solid windowtext .75pt; mso-padding-alt: 0cm 0cm 1.0pt 0cm" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">一段讲解：</span></p>
</div>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US><o:p>&nbsp;</o:p></span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">首先，你要明白</span><span lang=EN-US>Windows</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象和</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象的区别。</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象（即</span><span lang=EN-US>MFC</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">类</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">）实际上并没有把整个</span><span lang=EN-US>Windows</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象都包装在其中，它只是有一个窗口句柄。（<span style="COLOR: red">在</span></span><span style="COLOR: red" lang=EN-US>MSDN</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">上查看</span><span style="COLOR: red" lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">类的成员变量，确实只有一个</span><span style="COLOR: red" lang=EN-US>HWND hWnd</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">，成员函数有</span><span style="COLOR: red" lang=EN-US>Attach()</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">和</span><span style="COLOR: red" lang=EN-US>Dettach()</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">、</span><span style="COLOR: red" lang=EN-US>Create()</span><span style="FONT-FAMILY: 宋体; COLOR: red; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">等</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">）。这个窗口句柄如果指向一个实际存在的窗口对象，那么这个</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象就是有效的，否则这个</span><span lang=EN-US>MFC</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象是空的。</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">如果你还不明白，请回忆一下，当我们使用</span><span lang=EN-US>MFC</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">创建一个窗口时，是分两步进行的：</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">第一步，</span><span lang=EN-US>new</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">一个</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象，这一步是创建</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象，但是其中的</span><span lang=EN-US>HWND</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">还是非法的，因为对应的</span><span lang=EN-US>Windows</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象还没有被创建出来；</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">第二步，调用</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的成员函数</span><span lang=EN-US>Create</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">创建真正的</span><span lang=EN-US>Windows</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象，同时，把先前创建的</span><span lang=EN-US>MFC</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象的</span><span lang=EN-US>HWND</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">成员指向该窗口，这样才算创建完毕一个窗口。</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">而如果你是用</span><span lang=EN-US>SDK</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">方式，那么只要创建一个</span><span lang=EN-US>WNDCLASS</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">结构，然后调用</span><span lang=EN-US>Create</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">或者</span><span lang=EN-US>CreateEx</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">就创建了一</span> <span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">个窗口。</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">好，现在回答你的问题，你可以假设，现在你已经有了一个<strong>有效窗口句柄（<span style="COLOR: red">指向（并标识）了一个具体的</span></strong></span><strong><span style="COLOR: red" lang=EN-US>Windows window</span></strong><strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">）</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">，那么你想把这个窗口和一个</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象关联起来怎么办？很简单，用</span><span lang=EN-US>Attach</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">：其实就是让一个</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象——</span><strong><span lang=EN-US>CWnd</span></strong><strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象的</span><span lang=EN-US>HWND</span></strong><strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">成员</span></strong><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">等于<strong>这个窗口句柄</strong>。这就是</span><span lang=EN-US>Attach</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">主要完成的任务。</span><span lang=EN-US>&nbsp;</span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">第二个，关于</span><span lang=EN-US>Detach</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">。如前所述，</span><span lang=EN-US>WNDCLASS</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">其实和</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">根本没有什么关系。它们之间只是通过</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的成员</span><span lang=EN-US>HWND</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">联系起来的。如果把</span><span lang=EN-US> Attach</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">看做</span><span lang=EN-US>&#8220;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">联姻</span><span lang=EN-US>&#8221;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的话，那么</span><span lang=EN-US>Detach</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">就是</span><span lang=EN-US>&#8220;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">离婚</span><span lang=EN-US>&#8221;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">了，通俗地说，就是切断一个</span><span lang=EN-US>CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象和一个有效窗口的脐带。为什么要切断呢？因为</span><span lang=EN-US> CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">是</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的对象，</span><span lang=EN-US>C++</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">的对象有一个生存期的概念，脱离了该对象的作用域，这个对象就要被销毁，但是</span><span lang=EN-US>Windows</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象没有这个特点，当销毁</span><span lang=EN-US> CWnd</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">对象的时候，我们不一定希望</span><span lang=EN-US>WNDCLASS</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">一起被销毁，那么在此之前，我们就先要把这个</span><span lang=EN-US>&#8220;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">脐带</span><span lang=EN-US>&#8221;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">剪断，以免</span><span lang=EN-US>&#8220;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">城门失火，殃及池鱼</span><span lang=EN-US>&#8221;</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: 'Times New Roman'">。</span></p>
<p style="TEXT-INDENT: 26pt" class=MsoNormal><span><span style="FONT-FAMILY: '微软雅黑','sans-serif'; COLOR: black; FONT-SIZE: 13pt; mso-ascii-font-family: Verdana; mso-hansi-font-family: Verdana">一个代码示例:<br></span></span></p>
<p style="TEXT-INDENT: 28pt" class=MsoNormal><span lang=EN-US><o:p></p>
<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008080">&nbsp;1</span><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;CMyCapView::OnLButtonUp(UINT&nbsp;nFlags,&nbsp;CPoint&nbsp;point)<br></span><span style="COLOR: #008080">&nbsp;2</span><span style="COLOR: #000000"><img id=Codehighlighter1_56_687_Open_Image onclick="this.style.display='none'; Codehighlighter1_56_687_Open_Text.style.display='none'; Codehighlighter1_56_687_Closed_Image.style.display='inline'; Codehighlighter1_56_687_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_56_687_Closed_Image onclick="this.style.display='none'; Codehighlighter1_56_687_Closed_Text.style.display='none'; Codehighlighter1_56_687_Open_Image.style.display='inline'; Codehighlighter1_56_687_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif"></span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_56_687_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_56_687_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;3</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;TODO:&nbsp;在此添加消息处理程序代码和/或调用默认值</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">&nbsp;4</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br></span><span style="COLOR: #008080">&nbsp;5</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;CMyCapDoc</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pDoc</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(CMyCapDoc</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)GetDocument();<br></span><span style="COLOR: #008080">&nbsp;6</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">&nbsp;(m_nStatus</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">&nbsp;7</span><span style="COLOR: #000000"><img id=Codehighlighter1_155_649_Open_Image onclick="this.style.display='none'; Codehighlighter1_155_649_Open_Text.style.display='none'; Codehighlighter1_155_649_Closed_Image.style.display='inline'; Codehighlighter1_155_649_Closed_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="DISPLAY: none" id=Codehighlighter1_155_649_Closed_Image onclick="this.style.display='none'; Codehighlighter1_155_649_Closed_Text.style.display='none'; Codehighlighter1_155_649_Open_Image.style.display='inline'; Codehighlighter1_155_649_Open_Text.style.display='inline';" align=top src="http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="BORDER-BOTTOM: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BACKGROUND-COLOR: #ffffff; DISPLAY: none; BORDER-TOP: #808080 1px solid; BORDER-RIGHT: #808080 1px solid" id=Codehighlighter1_155_649_Closed_Text><img src="http://www.cppblog.com/Images/dot.gif"></span><span id=Codehighlighter1_155_649_Open_Text><span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;8</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CWnd&nbsp;srcWnd;<br></span><span style="COLOR: #008080">&nbsp;9</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">将一个窗口attach到一个CWnd对象上。原因如下句注释；具体见CSDN笔记<br></span><span style="COLOR: #008080">10</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">MSDN原文：Attaches&nbsp;a&nbsp;Windows&nbsp;window&nbsp;to&nbsp;a&nbsp;CWnd&nbsp;object.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">11</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srcWnd.Attach(hwndCapture);<br></span><span style="COLOR: #008080">12</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">CaptureDIB函数参数要求是CWnd类的指针，所以不能直接用hwndCapture,而要将hwndCapture跟一个<br></span><span style="COLOR: #008080">13</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">CWnd对象结合起来，使这个CWnd对象就是hwndCapture指向的窗口。</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pDoc</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">m_dib.CaptureDIB(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">srcWnd,rectCapture);<br></span><span style="COLOR: #008080">15</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">终止鼠标捕获</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">16</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ReleaseCapture();<br></span><span style="COLOR: #008080">17</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">恢复窗口显示模式</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">18</span><span style="COLOR: #008000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CMyCapApp</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;theApp</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">(CMyCapApp</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">)AfxGetApp();<br></span><span style="COLOR: #008080">19</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theApp</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">m_pMainWnd</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">ShowWindow(SW_NORMAL);<br></span><span style="COLOR: #008080">20</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStatus</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">21</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srcWnd.Detach();<br></span><span style="COLOR: #008080">22</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Invalidate(</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">23</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">24</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;CView::OnLButtonUp(nFlags,&nbsp;point);<br></span><span style="COLOR: #008080">25</span><span style="COLOR: #000000"><img align=top src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif">}</span></span></div>
<p style="TEXT-INDENT: 28pt" class=MsoNormal>&nbsp;<br>总结：因为Detach()函数是CWnd对象来调用的，所以可以理解为一个内部空虚的C++对象在呼唤一个有模有样的Windows窗口。</o:p></span></p>
<img src ="http://www.cppblog.com/lateCpp/aggbug/140631.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lateCpp/" target="_blank">lateCpp</a> 2011-02-25 10:18 <a href="http://www.cppblog.com/lateCpp/articles/140631.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>