天天看點

複制視窗指定位置的圖檔

有時候為了制作透明的控件,特别是外邊緣不規則的控件,可将控件背後的視窗圖檔複制到控件中,重繪.這樣控件看起來就象透明的.

<code>.h檔案中聲明變量</code>

<code>CMemoryDC  m_EmptyMemDC;</code><code>//空白記憶體DC(另外封裝的CDC的類)</code>

<code>CBitmap    m_BmpBackground;</code>

<code>void</code> <code>CLabel::CopyBackground(CDC * pDC)</code>

<code>{</code>

<code>    </code><code>CRect theRt;</code><code>//擷取控件大小(此處是自定義的控件,在控件内部執行)</code>

<code>    </code><code>GetWindowRect(&amp;theRt);</code>

<code>    </code><code>CPoint pt(theRt.left, theRt.top);</code>

<code>    </code><code>::ScreenToClient(GetParent()-&gt;m_hWnd,&amp;pt);</code>

<code>    </code><code>CClientDC  clientDC(</code><code>this</code><code>-&gt;GetParent());</code>

<code>   </code><code>//建立相容位圖</code>

<code>    </code><code>m_BmpBackground.CreateCompatibleBitmap(&amp;clientDC, m_rect.Width(), m_rect.Height());</code>

<code>    </code><code>HBITMAP</code> <code>hOldBitmap = (</code><code>HBITMAP</code><code>)m_EmptyMemDC.SelectObject(&amp;m_BmpBackground);</code>

<code>    </code><code>::StretchBlt(m_EmptyMemDC, 0, 0, theRt.Width(), theRt.Height(), clientDC.m_hDC, </code>

<code>      </code><code>pt.x, pt.y, theRt.Width(), theRt.Height(), SRCCOPY);</code>

<code>    </code><code>pDC-&gt;BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &amp;m_EmptyMemDC, 0, 0, SRCCOPY);</code>

<code>    </code><code>m_EmptyMemDC.SelectObject(hOldBitmap);</code>

<code>    </code><code>m_BmpBackground.DeleteObject();</code>

<code>}</code>

<code></code>

本文轉自Chinayu201451CTO部落格,原文連結http://blog.51cto.com/9233403/1974578 ,如需轉載請自行聯系原作者

繼續閱讀