﻿<?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++博客-Small place-文章分类-DirectX</title><link>http://www.cppblog.com/fenglinuestc/category/8282.html</link><description>Things will not always be bad,enjoy it.</description><language>zh-cn</language><lastBuildDate>Thu, 04 Dec 2008 07:21:19 GMT</lastBuildDate><pubDate>Thu, 04 Dec 2008 07:21:19 GMT</pubDate><ttl>60</ttl><item><title>显示视频(转)</title><link>http://www.cppblog.com/fenglinuestc/articles/68525.html</link><dc:creator>Fagged</dc:creator><author>Fagged</author><pubDate>Wed, 03 Dec 2008 16:10:00 GMT</pubDate><guid>http://www.cppblog.com/fenglinuestc/articles/68525.html</guid><wfw:comment>http://www.cppblog.com/fenglinuestc/comments/68525.html</wfw:comment><comments>http://www.cppblog.com/fenglinuestc/articles/68525.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglinuestc/comments/commentRss/68525.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglinuestc/services/trackbacks/68525.html</trackback:ping><description><![CDATA[<h1><font face=宋体>（找不到原作者了，不过这篇文章对我来说来得太及时了）<br>显示视频
<p>&#160;</p>
</font></h1>
<p><font size=3><font face="Times New Roman">DirectShow </font>提供了如下过滤器来显示视频：</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3><font face="Times New Roman"><strong>Video Renderer</strong> </font>过滤器<font face="Times New Roman">. </font>该过滤器可用于所有的支持<font face="Times New Roman">DirectX</font>的平台，它对平台没有其它特殊的要求。可以是它，或<font face="Times New Roman">GDI</font>来显示视频。它是在<font face="Times New Roman">WindowsXP</font>之前操作系统的默认视频显示过滤器。</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3><font face="Times New Roman"><strong>Video Mixing Renderer Filter 7 </strong>(VMR-7). VMR-7</font>可用于<font face="Times New Roman">WindowsXP</font>操作系统，并且是该系统下的默认视频显示过滤器。与老的视频显示过滤器相比，它具有一些更强大的性能，包括采用插件模式来控制<font face="Times New Roman">DirectShow</font>显示。</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3><font face="Times New Roman"><strong>Video Mixing Renderer Filter 9</strong> (VMR-9). VMR-9</font>是一个更新的视频混合显示过滤器，它采用了<font face="Times New Roman">Direct3D</font>来显示。它可用于所有的支持<font face="Times New Roman">DirectX</font>的平台。它不是默认的显示过滤器，因为它与其它的显示过滤器相比，对系统要求更高。</font></p>
<p><font size=3>一般来说，在视频显示应用上，<font face="Times New Roman">VMR-9</font>是首选。因为，它使用了最新的图像<font face="Times New Roman">API</font>，并且提供了最好的性能。</font>
<p><font size=3><font face="Times New Roman">
<p>&#160;</p>
</font></font>
<p>&#160;</p>
<p><font size=3><strong>窗体模式和非窗体模式</strong><strong>
<p>&#160;</p>
</strong></font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">DirectShow</font>视频显示可以<a title=PPS影视百科-选择 href="http://bk.pps.tv/ct82172/" target=_blank>选择</a>在窗体模式或者非窗体模式下进行。</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>在窗体模式下，视频将创建一个它自己的窗体来显示。</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>在非窗体模式下，视频可以自己在你程序的一个窗口上显示，而不让视频自己区创建窗体来显示。</font></p>
<p><font size=3><font face="Times New Roman"><strong>Video Renderer</strong></font>过滤器只支持窗体模式，<font face="Times New Roman">VMR-7</font>和<font face="Times New Roman">VMR-9</font>支持这两种模式。它们默认状态是窗体模式。</font></p>
<h1><font face=宋体>设置视频窗口
<p>&#160;</p>
</font></h1>
<p><font size=3><font face="Times New Roman"></font>在窗体模式下，视频将创建一个窗口，然后在该窗口上显示视频。大多数情况下，你可能想要把该窗口绑定到你的应用程序中。通过使用<strong><font face="Times New Roman">IVideoWindow</font></strong>接口，可以设置视频窗口的类型和位置。</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>在开始播放前，在过滤器图表管理器中去查找<strong><font face="Times New Roman">IVideoWindow</font></strong>接口：</font></p>
<p><font face="Times New Roman" size=3>IVideoWindow *pVidWin = NULL;</font></p>
<p><font size=3><font face="Times New Roman">pGraph-&gt;QueryInterface(IID_IVideoWindow, (void **)&amp;g_pVidWin);<code>
<p>&#160;</p>
</code></font></font>
<p>&#160;</p>
<p><font size=3>调用<strong><font face="Times New Roman">IVideoWindow::put_Owner</font></strong>方法去处理你应用程序的窗体。该方法提供了一个<strong><font face="Times New Roman">OAHWND</font></strong>类型的变量，所以要把句柄转换为该类型：</font></p>
<p><font size=3><font face="Times New Roman">pVidWin-&gt;put_Owner((OAHWND)hwnd);<code>
<p></code></font></font><font size=3>调用<strong><font face="Times New Roman">IVideoWindow::Put_WindowStyle</font></strong>来改变视频窗体的类型：</font></p>
<p><font size=3><font face="Times New Roman">pVidWin-&gt;put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);<code>
<p></code></font></font><font size=3><font face="Times New Roman">WS_CHILD</font>标志设置视频窗体为一个子窗体，<font face="Times New Roman">WS_CLIPSIBLINGS</font>标志可以防止视频窗体在另一个子窗体的客户区内显示视频。</font></p>
<p><font size=3>调用<strong><font face="Times New Roman">IVideoWindow::SetWindowPosition</font></strong>方法可以视频窗口的相对于你应用程序的客户区的位置。该方法的参数带了一个<font face="Times New Roman">RECT</font>参数，用它去指定视频窗口的位置。下例，让视频窗口和它父窗体的客户区想匹配。</font></p>
<p><font face="Times New Roman" size=3>RECT grc;</font></p>
<p><font face="Times New Roman" size=3>GetClientRect(hwnd, &amp;grc);</font></p>
<p><font face="Times New Roman" size=3>pVidWin-&gt;SetWindowPosition(0, 0, grc.right, grc.bottom);</font></p>
<p><font size=3>通过在过滤器图表管理器上调用<strong><font face="Times New Roman">IBaseicVide:<strong>GetVideoSize</strong></font></strong>方法可以得到视频本身的尺寸大小。你可以通过这些信息让视频保持正确的纵横比例。
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3>在应用程序退出前，停止图表并重置视频窗口为<font face="Times New Roman">NULL</font>。否则，窗口消息可能被错误的发送给错误的窗口，从而导致错误发生，</font></p>
<p><font face="Times New Roman" size=3>pControl-&gt;Stop(); </font></p>
<p><font face="Times New Roman" size=3>pVidWin-&gt;put_Visible(OAFALSE);</font></p>
<p><font size=3><font face="Times New Roman">pVidWin-&gt;put_Owner(NULL); </font></font></p>
<h1><font face=宋体>使用非窗体模式
<p>&#160;</p>
</font></h1>
<p><font size=3>视频混合显示过滤器（<font face="Times New Roman">VMR-7</font>和<font face="Times New Roman"> VMR-9</font>）都支持非窗体模式。这里将描述窗体模式和非窗体模式之间的不同，以及如何使用非模式窗体。
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>为了向后兼容已经在使用的应用程序，<font face="Times New Roman">VMR</font>默认的显示模式为窗体模式。在窗体模式中，视频创建一个它自己的窗体去显示视频。应用程序设置这个视频窗体为它的一个子窗体。这个单独<a title=PPS影视百科-存在 href="http://bk.pps.tv/ct400150737/" target=_blank>存在</a>的窗体会导致如下问题：
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>最严重的是，<a title=PPS影视百科-如果 href="http://bk.pps.tv/ct63850/" target=_blank>如果</a>窗体的消息在线程间发送可能导致消息死锁。<font face="Times New Roman">
<p>&#160;</p>
</font></font>
<p>&#160;</p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>过滤器图表管理器必须传递某些<font face="Times New Roman">window</font>消息，比如<font face="Times New Roman">WM_PAINT</font>，给视频显示器<font face="Times New Roman">(Video Renderer)</font>。这些对<font face="Times New Roman">IvideoWIndow</font>的操作必须是由过滤器图表管理器来完成，而不是视频显示器来完成，所以要靠过滤器图表管理器来纠正内部状态。</font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>要视频窗体的鼠标或者键盘事件，应用程序必须建立一个&#8220;消息通道&#8221;，让视频窗口把消息传递给应用程序。<font face="Times New Roman"> </font></font></p>
<p><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>为了防止剪接的情况，视频窗体还必须拥有正确的窗口状态。<font face="Times New Roman"> </font></font></p>
<p><font size=3>非窗体模式通过使用<font face="Times New Roman">VMR</font>直接在应用程序的客户区上画图来避免了上述的问题。它使用<font face="Times New Roman">DirectDraw</font>去剪接视频矩形。非窗体模式极大程度减少了死锁的<a title=PPS影视百科-偶然 href="http://bk.pps.tv/ct300000595/" target=_blank>偶然</a>发生。同样，应用程序不必去设置视频自身创建的窗口和窗口的状态。事实上，当<font face="Times New Roman">VRM</font>使用窗体模式时，它也不使用<strong><font face="Times New Roman">IVideoWindow</font></strong>接口。</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>要使用非窗体模式，你必须明确地去配置<font face="Times New Roman">VMR</font>。你会发现配置工作非常灵活并且比窗体模式更容易。</font></p>
<p><font size=3>在配置<font face="Times New Roman">VMR </font>前应建立过滤器图表<font face="Times New Roman">(Filter graph): </font></font></p>
<ol type=1>
    <li><font size=3>创建过滤器图表管理器（<font face="Times New Roman">Filter Graph Manager</font>）。<font face="Times New Roman"> </font></font>
    <li><font size=3>创建<font face="Times New Roman">VMR</font>并添加到过滤器图表中（<font face="Times New Roman">filter graph</font>）。</font>
    <li><font size=3>在<font face="Times New Roman">VMR</font>中调用<font face="Times New Roman"><strong>IVMRFilterConfig::SetRenderingMode</strong> </font>设置<font face="Times New Roman">VMRMode_Windowless </font>标识。<font face="Times New Roman"> </font></font>
    <li><font size=3>在<font face="Times New Roman">VMR</font>中调用<font face="Times New Roman"> <strong>IVMRWindowlessControl::SetVideoClippingWindow</strong> </font>去指定视频将要显示的窗体句柄。<font face="Times New Roman"> </font></font></li>
</ol>
<p><font size=3>现在调用<strong><font face="Times New Roman">IGraphBuilder::RenderFile</font></strong>完成过滤器图表余下的工作。过滤器图表管理器将自动使用这个你添加到过滤器图表中的<font face="Times New Roman">VMR</font>实例。</font></p>
<p><font size=3>下面代码显示了这些工作：</font></p>
<p><font face="Times New Roman" size=3>HRESULT InitWindowlessVMR( </font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HWND hwndApp,&nbsp;&nbsp;&nbsp;&nbsp;// </font>视频窗体<font face="Times New Roman"> </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;IGraphBuilder* pGraph,&nbsp;&nbsp;&nbsp;&nbsp;// </font>过滤器图表指针<font face="Times New Roman">
<p>&#160;</p>
</font></font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;IVMRWindowlessControl** ppWc, // </font>接收<font face="Times New Roman">VMR</font>指针</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;) </font></font></p>
<p><font face="Times New Roman" size=3>{ </font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (!pGraph || !ppWc) return E_POINTER;</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;IBaseFilter* pVmr = NULL; </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;IVMRWindowlessControl* pWc = NULL; </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>创建<font face="Times New Roman">VMR </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HRESULT hr = CoCreateInstance(CLSID_VideoMixingRenderer, NULL, </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;CLSCTX_INPROC, IID_IBaseFilter, (void**)&amp;pVmr); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (FAILED(hr))</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;return hr;</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;}</font></font></p>
<p><font face="Times New Roman" size=3>&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>把<font face="Times New Roman">VMR</font>添加到过滤器图表中
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = pGraph-&gt;AddFilter(pVmr, L"Video Mixing Renderer"); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (FAILED(hr)) </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pVmr-&gt;Release();</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;return hr;</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;}</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>设置显示模式<font face="Times New Roman">&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;IVMRFilterConfig* pConfig; </font></font>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = pVmr-&gt;QueryInterface(IID_IVMRFilterConfig, (void**)&amp;pConfig); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (SUCCEEDED(hr))<br>&nbsp; </font></font><font size=3><font face="Times New Roman">{ </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = pConfig-&gt;SetRenderingMode(VMRMode_Windowless); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pConfig-&gt;Release(); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;}</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (SUCCEEDED(hr))<br></font></font><font size=3><font face="Times New Roman">{</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>设置窗体<font face="Times New Roman">&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = pVmr-&gt;QueryInterface(IID_IVMRWindowlessControl, (void**)&amp;pWc);<br></font></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if( SUCCEEDED(hr))&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;{ </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = pWc-&gt;SetVideoClippingWindow(hwndApp); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (SUCCEEDED(hr))</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;*ppWc = pWc; //</font>返回<font face="Times New Roman">AddRef</font>指针<font face="Times New Roman"> </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;}</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;else</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pWc-&gt;Release();</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;}</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;} </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;} </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pVmr-&gt;Release(); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;return hr; </font></font></p>
<p><font face="Times New Roman" size=3>} </font></p>
<p><font size=3>该函数假设正在显示一个视频流并没有混合的静态位图。你将看到按如下调用该函数：</font></p>
<p><font face="Times New Roman" size=3>IVMRWindowlessControl *pWc = NULL;</font></p>
<p><font face="Times New Roman" size=3>hr = InitWindowlessVMR(hwnd, pGraph, &amp;g_pWc);</font></p>
<p><font face="Times New Roman" size=3>if (SUCCEEDED(hr))</font></p>
<p><font face="Times New Roman" size=3>{</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>建立图表
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pGraph-&gt;RenderFile(wszMyFileName, 0);</font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;//</font>完成后释放<font face="Times New Roman">VMR</font>接口
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;pWc-&gt;Release();</font></font></p>
<p><font face="Times New Roman" size=3>}</font></p>
<p><strong><font size=3>视频定位</font></strong></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>配置完<font face="Times New Roman">VMR</font>后，下一个步骤就是去设置视频显示的位置。有两个矩形位置要考虑，一个是<font face="Times New Roman">Source</font>矩形位置，一个是<font face="Times New Roman">desitnation</font>矩形位置。<font face="Times New Roman">Source</font>定义视频显示的位置。<font face="Times New Roman">Destination</font>指定包含视频的窗体的客户区的位置。<font face="Times New Roman">VMR</font>从<font face="Times New Roman">source</font>把图像按<font face="Times New Roman">destination</font>的尺寸匹配后显示出来。</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>调用<strong><font face="Times New Roman">IVMRWindowlessControl::SetVideoPosition</font></strong>去指定这个两个矩形位置。<font face="Times New Roman">Source</font>矩形的大小必须等于或小于视频本身的尺寸大小；你可以使用<strong><font face="Times New Roman">IVMRWindowlessControl::GetNativeVideoSize</font></strong>去获得视频本身的尺寸。</font></p>
<p><font size=3>下面的实例，将设置<font face="Times New Roman">Source</font>等于视频尺寸<font face="Times New Roman">1/4</font>（左上角的位置相等），并设置<font face="Times New Roman">destination</font>矩形等于窗体客户区的大小<font face="Times New Roman">1/</font>（左上角的位置相等）：</font></p>
<p><font size=3><font face="Times New Roman">// </font>获得视频自身尺寸大小
<p>&#160;</p>
</font>
<p>&#160;</p>
<p><font face="Times New Roman" size=3>long lWidth, lHeight; </font></p>
<p><font face="Times New Roman" size=3>HRESULT hr = g_pWc-&gt;GetNativeVideoSize(&amp;lWidth, &amp;lHeight, NULL, NULL); </font></p>
<p><font face="Times New Roman" size=3>if (SUCCEEDED(hr))</font></p>
<p><font face="Times New Roman" size=3>{</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;RECT rcSrc, rcDest; <br></font></font><font size=3><font face="Times New Roman">// </font>设置<font face="Times New Roman">Source</font>尺寸&nbsp;<br></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;SetRect(&amp;rcSrc, 0, 0, lWidth/2, lHeight/2);&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;// </font>获得显示窗体的客户区尺寸 <br>&nbsp;&nbsp;&nbsp; </font><font size=3><font face="Times New Roman">GetClientRect(hwnd, &amp;rcDest); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;//</font>设置<font face="Times New Roman">destination</font>尺寸</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;SetRect(&amp;rcDest, 0, 0, rcDest.right/2, rcDest.bottom/2); </font></font></p>
<p><font face="Times New Roman" size=3>&nbsp;&nbsp;&nbsp;&nbsp;</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>视频定位</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hr = g_pWc-&gt;SetVideoPosition(&amp;rcSrc, &amp;rcDest); </font></font></p>
<p><font face="Times New Roman" size=3>}</font></p>
<p><strong><font size=3>处理窗体消息</font></strong></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;</font>因为<font face="Times New Roman">VMR</font>没有自己的窗体，当视频需要重画或者尺寸要改变是，你必须要通知窗体来适应。</font></p>
<p align=left><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>当接收到一个<font face="Times New Roman">WM_PAINT</font>消息，可调用<font face="Times New Roman"><strong>IVMRWindowlessControl::RepaintVideo</strong> </font>来重画图像。<font face="Times New Roman"> </font></font></p>
<p align=left><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>当接收到一个<font face="Times New Roman">WM_DISPLAYCHANGE </font>消息<font face="Times New Roman">, </font>可调用<font face="Times New Roman"> <strong>IVMRWindowlessControl::DisplayModeChanged</strong></font>消息。<font face="Times New Roman">VMR</font>就可以获得如下行为比如改变分辨率或者色深。<font face="Times New Roman"> </font></font></p>
<p align=left><font size=3>l</font>&nbsp;&nbsp;&nbsp;&nbsp;<font size=3>当接收到一个<font face="Times New Roman">WM_SIZE </font>消息<font face="Times New Roman">, </font>可以重新调用<font face="Times New Roman"><strong>SetVideoPosition</strong> </font>来改变视频的显示位置。<font face="Times New Roman"> </font></font></p>
<p><font size=3>下面显示如何处理<font face="Times New Roman">WM_PAINT</font>消息。它将在窗体的客户区重绘，但是不会对视频显示的区域进行重绘。不对视频显示的区域进行重绘，是因为<font face="Times New Roman">VMR</font>会对该区域显示视频，如果你的程序再对该区域重绘会引起屏幕闪烁。也是应为这个原因，所有不要在你窗体类中去设置背景刷。</font></p>
<p><font face="Times New Roman" size=3>void OnPaint(HWND hwnd) </font></p>
<p><font face="Times New Roman" size=3>{ </font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;PAINTSTRUCT ps; </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HDC&nbsp;&nbsp;&nbsp;&nbsp;hdc; </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;RECT&nbsp;&nbsp;&nbsp;&nbsp;rcClient; </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;GetClientRect(hwnd, &amp;rcClient); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;hdc = BeginPaint(hwnd, &amp;ps); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;if (g_pWc != NULL) </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{ </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>查找窗体需要重绘的客户区，该区域应该减去视频显示的区域&nbsp;<br></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// (</font>这里假设<font face="Times New Roman">g_rcDest </font>是已经计算好了的区域<font face="Times New Roman">)</font></font>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HRGN rgnClient = CreateRectRgnIndirect(&amp;rcClient); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HRGN rgnVideo = CreateRectRgnIndirect(&amp;g_rcDest); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;CombineRgn(rgnClient, rgnClient, rgnVideo, RGN_DIFF);&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;// </font>重绘窗体</font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HBRUSH hbr = GetSysColorBrush(COLOR_BTNFACE); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;FillRgn(hdc, rgnClient, hbr); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;// </font>释放对象</font>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;DeleteObject(hbr); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;DeleteObject(rgnClient); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;DeleteObject(rgnVideo); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;// </font>请求<font face="Times New Roman">VMR to </font>重绘视频</font>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;HRESULT hr = g_pWc-&gt;RepaintVideo(hwnd, hdc); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;} </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;else // </font>没有视频显示，重绘整个客户区<font face="Times New Roman">&nbsp;<br></font></font><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;{ </font></font>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;FillRect(hdc, &amp;rc2, (HBRUSH)(COLOR_BTNFACE + 1)); </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;} </font></font></p>
<p><font size=3><font face="Times New Roman">&nbsp;&nbsp;&nbsp;&nbsp;EndPaint(hwnd, &amp;ps); </font></font></p>
<p><font face="Times New Roman" size=3>} </font></p>
<img src ="http://www.cppblog.com/fenglinuestc/aggbug/68525.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglinuestc/" target="_blank">Fagged</a> 2008-12-04 00:10 <a href="http://www.cppblog.com/fenglinuestc/articles/68525.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>WinCE 6.0 DirectDraw改动</title><link>http://www.cppblog.com/fenglinuestc/articles/64070.html</link><dc:creator>Fagged</dc:creator><author>Fagged</author><pubDate>Wed, 15 Oct 2008 14:51:00 GMT</pubDate><guid>http://www.cppblog.com/fenglinuestc/articles/64070.html</guid><wfw:comment>http://www.cppblog.com/fenglinuestc/comments/64070.html</wfw:comment><comments>http://www.cppblog.com/fenglinuestc/articles/64070.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/fenglinuestc/comments/commentRss/64070.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/fenglinuestc/services/trackbacks/64070.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;刚用上WinCE6.0，DirectDraw部分有较大的改动，在自己改以前的代码的同时在这记录下：都比较简短，要更详细的话可以看PB的帮助文档，自己去比较下Simple中的代码^_^：<br>&nbsp;
<p><span>1.</span><span>接口不再使用</span><span>XXXXX2/4/7,</span><span>而是使用统一的</span><span>XXXXX, </span><span>如</span><span>:<br></span><span>&nbsp;&nbsp;&nbsp;LPDIRECTDRAW<span>&nbsp;&nbsp; </span>g_pDD = NULL;(</span><span>新</span><span>)<br></span><span>&nbsp;&nbsp;&nbsp;LPDIRECTDRAW4&nbsp;g_pDD = NULL;(</span><span>旧</span><span>)<br><span>&nbsp;&nbsp; </span></span><span>新版本</span><span>不兼容旧版本</span><span>.<br></span><span>2.</span><span>在初始化</span><span>LPDIRECTDRAW<span>&nbsp;&nbsp; </span>g_pDD</span><span>的时候不再需要</span><span>QueryInterface,</span><span>而是直接用</span><span><span>&nbsp;&nbsp;&nbsp; </span>DirectDrawCreate(NULL, &amp;g_pDD, NULL);</span><span>完成</span><span>.<br></span><span>3.DDCAPS</span><span>结构改变</span><span>,</span><span>很多成员不再使用</span><span>ddcaps.dwCaps&amp;DDSCAP_XXX</span><span>判断是否有这个功能</span><span>.</span><span>而是根据直接提供的</span><span>ddcaps.dwXXXCpas</span><span>的成员是否为</span><span>0</span><span>来判断</span><span>.</span><span>还有很多的成员的改变，这个看新的</span><span>PB6.0</span><span>的文档。</span></p>
<p>&nbsp;<span>4. DDSURFACEDESC<span>&nbsp;&nbsp; </span>ddsd</span><span>改动</span><span>(</span><span>只对于</span><span>Overlay)<br>&nbsp;</span><span>不再使用</span><span><span>&nbsp;&nbsp;&nbsp; </span></span><span>ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY;</span></p>
<p><span>中的</span><span>DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY;</span></p>
<p><span>5.Overlay</span><span>的</span><span>Alpha</span><span>设置问题</span><span>:<br></span></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 要Overlay有透明效果只要设置DDOVERLAYFX&nbsp;中的&nbsp;dwAlphaConst,dwAlphaConstBitDepth到合适的值就可以了<br>&nbsp;<span><br>&nbsp;&nbsp;&nbsp; <br></span></p>
要更多更专业的解释请看:<a href="http://msdn.microsoft.com/en-us/library/aa913866.aspx"><br>http://msdn.microsoft.com/en-us/library/aa913866.aspx</a> 
<img src ="http://www.cppblog.com/fenglinuestc/aggbug/64070.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/fenglinuestc/" target="_blank">Fagged</a> 2008-10-15 22:51 <a href="http://www.cppblog.com/fenglinuestc/articles/64070.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>