본문 바로가기
C & C++/MFC Media

picture control 투명화 처리

by izen8 2012. 8. 30.
반응형

// PictureEx.cpp : implementation file
//

#include "stdafx.h"
#include "PictureEx.h"

// CPictureEx

IMPLEMENT_DYNAMIC(CPictureEx, CStatic)

CPictureEx::CPictureEx()
{
 m_iPicStyle = PICSTYLE_CENTER;
 m_colorBkgrnd = m_colorTransparent = RGB(0, 0, 0);
}

CPictureEx::~CPictureEx()
{
}

BEGIN_MESSAGE_MAP(CPictureEx, CStatic)
 ON_WM_ERASEBKGND()
 ON_WM_PAINT()
END_MESSAGE_MAP()

void CPictureEx::SetTransparentColor(COLORREF color)
{
 m_colorTransparent = color;
 Invalidate();
}
COLORREF CPictureEx::GetTransparentColor()
{
 return m_colorTransparent;
}
void CPictureEx::SetBkColor(COLORREF color)
{
 m_colorBkgrnd = color;
 Invalidate();
}
COLORREF CPictureEx::GetBkColor()
{
 return m_colorBkgrnd;
}
void CPictureEx::SetPictureStyle(INT style)
{
 m_iPicStyle = style;
 Invalidate();
}
INT CPictureEx::GetPictureStyle()
{
 return m_iPicStyle;
}

// CPictureEx message handlers
BOOL CPictureEx::OnEraseBkgnd(CDC* pDC)
{
 if(GetBitmap())
 {
 return TRUE;
 }
 return CStatic::OnEraseBkgnd(pDC);
}

void CPictureEx::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 CRect rClient;
 this->GetClientRect(&rClient);

 // background
 CBrush* br = new CBrush(m_colorBkgrnd);
 dc.FillRect(rClient, br);
 delete br;

 // image
 HBITMAP hBmp = this->GetBitmap();
 BITMAP bmpObj;
 if(hBmp && GetObject(hBmp, sizeof(BITMAP), &bmpObj))
 {

 INT x, y, w, h;
 switch(m_iPicStyle)
 {
 case PICSTYLE_NORMAL:
 x = y = 0;
 w = bmpObj.bmWidth;
 h = bmpObj.bmHeight;
 break;
 case PICSTYLE_CENTER:
 w = bmpObj.bmWidth;
 h = bmpObj.bmHeight;
 x = rClient.Width() / 2 - w/2;
 y = rClient.Height() / 2 - h/2;
 break;
 case PICSTYLE_STRETCH:
 x = y = 0;
 w = rClient.Width();
 h = rClient.Height();
 break;
 }
 ::TransparentImage(dc, x, y, w, h, hBmp, 0, 0, bmpObj.bmWidth, bmpObj.bmHeight, m_colorTransparent);
 }
}

반응형

댓글