有時候為了制作透明的控件,特别是外邊緣不規則的控件,可将控件背後的視窗圖檔複制到控件中,重繪.這樣控件看起來就象透明的.
<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(&theRt);</code>
<code> </code><code>CPoint pt(theRt.left, theRt.top);</code>
<code> </code><code>::ScreenToClient(GetParent()->m_hWnd,&pt);</code>
<code> </code><code>CClientDC clientDC(</code><code>this</code><code>->GetParent());</code>
<code> </code><code>//建立相容位圖</code>
<code> </code><code>m_BmpBackground.CreateCompatibleBitmap(&clientDC, m_rect.Width(), m_rect.Height());</code>
<code> </code><code>HBITMAP</code> <code>hOldBitmap = (</code><code>HBITMAP</code><code>)m_EmptyMemDC.SelectObject(&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->BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &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 ,如需轉載請自行聯系原作者