1.H文件
#ifndef __TRANSPARENTIMAGE_H_TRANSPARENTIMAGE_42A6E395_97E4_11D3_B6F0_005004024A9E
#define __TRANSPARENTIMAGE_H_TRANSPARENTIMAGE_42A6E395_97E4_11D3_B6F0_005004024A9E
#if _MSC_VER >= 1000
#pragma once
#endif
class CTransparentImage : public CStatic
{
////////////////////////////////////////////////////////////////////////////
// @access Public methods.
//
public:
COLORREF m_Back;
//
// @cmember Constructor.
CTransparentImage()
//
// @cmember Constructor.
virtual ~CTransparentImage()
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// @access ClassWizard generated virtual function overrides
//
//{{AFX_VIRTUAL( CTransparentImage )
//}}AFX_VIRTUAL
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// @access Generated message map functions
//
protected:
//
//{{AFX_MSG( CTransparentImage )
afx_msg void OnPaint()
//}}AFX_MSG
//
DECLARE_MESSAGE_MAP()
//
////////////////////////////////////////////////////////////////////////////
}
//{{AFX_INSERT_LOCATION}}
#endif
CPP文件
//////////////////////////////////////////////////////////////
//******************************************************************************
//******************************************************************************
// Owner: Andrew Forget (andrewfo@csolve.net)
// Copyright ?1999, DragonWerks Software, Inc.
//------------------------------------------------------------------------------
//
// @doc TRANSPARENTIMAGE
//
// @module TransparentImage.cpp |
// This file implements the transparent image staic class
// <c CTransparentImage>.
//
// @version
//------------------------------------------------------------------------------
//.Version: Date: Author: Comments:
//.-------- ----------- ----------- --------------------------------------------
//. 1.0 11/11/99 andrewfo New file.
//******************************************************************************
//******************************************************************************
//******************************************************************************
//
// Includes
//
//******************************************************************************
#include "StdAfx.h"
#include "TransparentImage.h"
//******************************************************************************
//
// Debugging
//
//******************************************************************************
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__
#endif
//******************************************************************************
// Owner: Andrew Forget (andrewfo@csolve.net)
//------------------------------------------------------------------------------
//
// @mfunc | CTransparentImage | CTransparentImage |
// Constructor for <c CTransparentImage>.
//
// @syntax CTransparentImage()
//
// @version
//------------------------------------------------------------------------------
//.Version: Date: Author: Comments:
//.-------- ----------- ----------- --------------------------------------------
//. 1.0 11/11/99 andrewfo New code.
//******************************************************************************
CTransparentImage::CTransparentImage()
{
}
//******************************************************************************
// Owner: Andrew Forget (andrewfo@csolve.net)
//------------------------------------------------------------------------------
//
// @mfunc | CTransparentImage | ~CTransparentImage |
// Destructor for <c CTransparentImage>.
//
// @syntax ~CTransparentImage()
//
// @version
//------------------------------------------------------------------------------
//.Version: Date: Author: Comments:
//.-------- ----------- ----------- --------------------------------------------
//. 1.0 11/11/99 andrewfo New code.
//******************************************************************************
CTransparentImage::~CTransparentImage()
{
}
//******************************************************************************
// Owner: Andrew Forget (andrewfo@csolve.net)
//------------------------------------------------------------------------------
//
// @msgmap CTransparentImage | CStatic |
// Message map for <c CTransparentImage>.
//
// @mapmsg ON_WM_PAINT() | OnPaint |
// Paints the control.
//
// @version
//------------------------------------------------------------------------------
//.Version: Date: Author: Comments:
//.-------- ----------- ----------- --------------------------------------------
//. 1.0 11/11/99 andrewfo New code.
//******************************************************************************
begin
_MESSAGE_MAP( CTransparentImage, CStatic )
//{{AFX_MSG_MAP( CTransparentImage )
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//******************************************************************************
// Owner: Andrew Forget (andrewfo@csolve.net)
//------------------------------------------------------------------------------
//
// @mfunc void | CTransparentImage | OnPaint |
// This handles the WM_PAINT windows message for the window.
//
// @syntax void OnPaint()
//
// @version
//------------------------------------------------------------------------------
//.Version: Date: Author: Comments:
//.-------- ----------- ----------- --------------------------------------------
//. 1.0 11/11/99 andrewfo New code.
//******************************************************************************
void CTransparentImage::OnPaint()
{
HBITMAP l_hbmpBitmap = GetBitmap()
if( l_hbmpBitmap == NULL )
{
Default()
return
}
CPaintDC l_PaintDC( this )
////////////////////////////////////////////////////////////////////////////
// Prepare everything for drawing
////////////////////////////////////////////////////////////////////////////
CRect l_rcClient
GetClientRect( &l_rcClient )
CDC l_BufferDC
l_BufferDC.CreateCompatibleDC( &l_PaintDC )
CBitmap l_BufferBitmap
l_BufferBitmap.CreateCompatibleBitmap( &l_PaintDC, l_rcClient.Width(), l_rcClient.Height() )
CBitmap* l_pOldBufferBitmap = l_BufferDC.SelectObject( &l_BufferBitmap )
CDC l_MaskDC
l_MaskDC.CreateCompatibleDC( &l_PaintDC )
CBitmap l_MaskBitmap
l_MaskBitmap.CreateBitmap( l_rcClient.Width(), l_rcClient.Height(), 1, 1, NULL )
CBitmap* l_pOldMaskBitmap = l_MaskDC.SelectObject( &l_MaskBitmap )
#define ROP4_TRANSPARENTBLIT 0xCCAA0020
#define SRCMASK 0x00220326
////////////////////////////////////////////////////////////////////////////
// Fill with transparent color
////////////////////////////////////////////////////////////////////////////
l_BufferDC.FillSolidRect( &l_rcClient, m_Back )
////////////////////////////////////////////////////////////////////////////
// Blit the bitmap to the buffer
////////////////////////////////////////////////////////////////////////////
CDC l_MemoryDC
l_MemoryDC.CreateCompatibleDC( &l_PaintDC )
CBitmap* l_pOldMemoryBitmap = l_MemoryDC.SelectObject( CBitmap::FromHandle( l_hbmpBitmap ) )
l_BufferDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MemoryDC, 0, 0, SRCCOPY )
l_MemoryDC.SelectObject( l_pOldMemoryBitmap )
////////////////////////////////////////////////////////////////////////////
// Create the mask.
////////////////////////////////////////////////////////////////////////////
COLORREF l_crOldBack = l_BufferDC.SetBkColor( RGB( 255, 0, 255 ) )
l_MaskDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC, 0, 0, SRCCOPY )
l_BufferDC.SetBkColor( l_crOldBack )
if( ! l_PaintDC.MaskBlt( 0,
0,
l_rcClient.Width(),
l_rcClient.Height(),
&l_BufferDC,
0,
0,
l_MaskBitmap,
0,
0,
ROP4_TRANSPARENTBLIT ) )
{
CDC l_CopyDC
l_CopyDC.CreateCompatibleDC( &l_PaintDC )
CBitmap l_CopyBitmap
l_CopyBitmap.CreateCompatibleBitmap( &l_PaintDC, l_rcClient.Width(), l_rcClient.Height() )
CBitmap* l_pOldCopyBitmap = l_CopyDC.SelectObject( &l_CopyBitmap )
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_PaintDC, 0, 0, SRCCOPY )
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC, 0, 0, SRCAND )
l_BufferDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_MaskDC, 0, 0, SRCMASK )
l_CopyDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_BufferDC, 0, 0, SRCPAINT )
l_PaintDC.BitBlt( 0, 0, l_rcClient.Width(), l_rcClient.Height(), &l_CopyDC, 0, 0, SRCCOPY )
l_CopyDC.SelectObject( l_pOldCopyBitmap )
}
l_MaskDC.SelectObject( l_pOldMaskBitmap )
l_BufferDC.SelectObject( l_pOldBufferBitmap )
}
///////////////////////////////////////////////////////
2.将位图和此类相关
CTransparentImage m_Image
3.初始化时候要设置
m_Image.m_Back=RGB(232,213,23);
//此颜色必需和位图颜色相符