19,464
社区成员
发帖
与我相关
我的任务
分享

BOOL CMyStatic::OnEraseBkgnd(CDC* pDC)
{
CRect rectBk;// 客户区的大小
// 无标题栏-获取客户窗口区域
GetWindowRect( &rectBk ); //这里我去取的窗口大小,当然去客户区也行 主要是为了跟SetupRegion中统一
CDC memDC;// 内存设备描述表
CBitmap cBitmap;//位图对象
CBitmap* pOldMemBmp = NULL;
BITMAP stBitmap;
//1.构造内存设备环境,并将位图装入
memDC.CreateCompatibleDC(pDC);
//2.加载背景位图
cBitmap.LoadBitmap(m_MaskBitmapID);
pOldMemBmp = memDC.SelectObject(&cBitmap);
//read the cBitmap's size
cBitmap.GetBitmap(&stBitmap);
int stBMPwidth = stBitmap.bmWidth;
int stBMPHeigth = stBitmap.bmHeight;
// 将背景位图复制到窗口客户区
pDC->StretchBlt( 0, 0, rectBk.Width(), rectBk.Height(), &memDC, 0, 0,stBMPwidth, stBMPHeigth, SRCCOPY); //这里直接把原图缩放到Static窗口贴图就行了。
//绘图完成后的清理
memDC.SelectObject(pOldMemBmp);
if(pOldMemBmp)
{
cBitmap.DeleteObject();
memDC.DeleteDC();
}
return TRUE;
}void CMyStatic::SetupRegion(CDC *pDC)
{
//---------------------------------------------------------
CRect wndRet;
GetWindowRect( &wndRet );
//---------------------------------------------------------
CBitmap &orgImg = m_bmpDraw;
BITMAP orgInf; orgImg.GetBitmap( &orgInf );
int orgWid = orgInf.bmWidth;
int orgHit = orgInf.bmHeight; //取得原图宽度和高度
//---------------------------------------------------------
CDC istMDC;
CBitmap* preImg = NULL;
istMDC.CreateCompatibleDC( pDC );
preImg = istMDC.SelectObject( &orgImg ); //创建第一层内存DC,并选中原图
//---------------------------------------------------------
CBitmap sndImg;
CDC sndMDC;
sndMDC.CreateCompatibleDC( &istMDC );
sndImg.CreateCompatibleBitmap( &istMDC, wndRet.Width( ),wndRet.Height( ) );
sndMDC.SelectObject( &sndImg ); //创建第二层内存DC,创建一个与当前窗口大小相同的内存图片并选入第二层内存DC。
sndMDC.StretchBlt( 0, 0, wndRet.Width( ), wndRet.Height( ) , &istMDC, 0, 0,orgWid, orgHit, SRCCOPY);
//将原图缩小或放大到当前窗口大小,从第一层内存DC贴到第二层内存DC
//经过上面一步后,实际上就是将位图收缩到了Static窗体当中,此后再去除掩码颜色得到的窗口区域将和Static窗口匹配
//这里直接使用默认的图像缩放(插值),如果对图像不满意,这里只能再用图像处理的方法来解决了,不过速度上可能就比较慢了。
//---------------------------------------------------------
COLORREF mrkCol = sndMDC.GetPixel( 0, 0 );
CRgn wndRgn; wndRgn.CreateRectRgn( 0, 0, wndRet.Width( ), wndRet.Height( ) );
for( int x = 0; x <= wndRet.Width( ); x ++ )
{
for( int y = 0; y <= wndRet.Height( ); y ++ )
{
COLORREF nowCol = sndMDC.GetPixel( x, y );
if( nowCol == mrkCol )
{
CRgn tmpRgn;
tmpRgn.CreateRectRgn( x, y, x + 1, y + 1 );
wndRgn.CombineRgn( &wndRgn, &tmpRgn, RGN_XOR );
tmpRgn.DeleteObject( );
}
}
}
if ( preImg ) istMDC.SelectObject( preImg );
SetWindowRgn( ( HRGN )wndRgn, TRUE );
//----清理内存,防止内存泄露---------------------------------
preImg->DeleteObject();
preImg = NULL;
sndMDC.DeleteDC();
istMDC.DeleteDC();
sndImg.DeleteObject();
orgImg.DeleteObject();
//-----------------------------------------------------------
}