且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

复制窗口指定位置的图片

更新时间:2022-05-22 12:03:01

有时候为了制作透明的控件,特别是外边缘不规则的控件,可将控件背后的窗口图片复制到控件中,重绘.这样控件看起来就象透明的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
.h文件中声明变量
CMemoryDC  m_EmptyMemDC;//空白内存DC(另外封装的CDC的类)
CBitmap    m_BmpBackground;
void CLabel::CopyBackground(CDC * pDC)
{
    CRect theRt;//获取控件大小(此处是自定义的控件,在控件内部执行)
    GetWindowRect(&theRt);
 
    CPoint pt(theRt.left, theRt.top);
    ::ScreenToClient(GetParent()->m_hWnd,&pt);
 
    CClientDC  clientDC(this->GetParent());
   //创建兼容位图
    m_BmpBackground.CreateCompatibleBitmap(&clientDC, m_rect.Width(), m_rect.Height());
    HBITMAP hOldBitmap = (HBITMAP)m_EmptyMemDC.SelectObject(&m_BmpBackground);
 
    ::StretchBlt(m_EmptyMemDC, 0, 0, theRt.Width(), theRt.Height(), clientDC.m_hDC, 
      pt.x, pt.y, theRt.Width(), theRt.Height(), SRCCOPY);
 
    pDC->BitBlt(0, 0, m_rect.Width(), m_rect.Height(), &m_EmptyMemDC, 0, 0, SRCCOPY);
 
    m_EmptyMemDC.SelectObject(hOldBitmap);
    m_BmpBackground.DeleteObject();
 
}




















本文转自Chinayu201451CTO博客,原文链接http://blog.51cto.com/9233403/1974578 ,如需转载请自行联系原作者