Home > 2. Windows Programming, Native Code > An easy way to make transparent picture control in MFC

An easy way to make transparent picture control in MFC

I’m going to describe the way to make a transparent picture box control in MFC (Smart Device application). It worked well on Windows CE, and it should also be fine on Win32 application.

The key function is TransparentImage. Using it, you will see that the job is pretty simple…

I called my class CPictureEx, which derived from CStatic.

#pragma once

// CPictureEx

class CPictureEx : public CStatic
{
	DECLARE_DYNAMIC(CPictureEx)

	INT m_iPicStyle;
	COLORREF m_colorTransparent;
	COLORREF m_colorBkgrnd;
public:
	enum PIC_STYLE
	{
		PICSTYLE_NORMAL = 0,
		PICSTYLE_CENTER = 1,
		PICSTYLE_STRETCH = 2
	};

	CPictureEx();
	virtual ~CPictureEx();

protected:
	DECLARE_MESSAGE_MAP()
public:
	void SetTransparentColor(COLORREF color);
	COLORREF GetTransparentColor();
	void SetBkColor(COLORREF color);
	COLORREF GetBkColor();
	void SetPictureStyle(INT style);
	INT GetPictureStyle();

	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnPaint();
};

And this is in PictureEx.cpp:

// 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);
 }
}
Advertisements
  1. Igor
    25/07/2012 at 9:54 PM

    Well it should be fine, but unfortunately is not. There is NO TransparentImage function for Win32.

  2. PHAM Hoai Vu
    26/07/2012 at 12:37 AM

    well, if you are working on Win32, go with qt or better libraries. I’m not sure the messy MFC should be used nowadays…

  3. Igor
    26/07/2012 at 3:55 PM

    I would love to, but unfortunately I’m not freelancer and work with old project written on MFC. Anyway, thank you!

  4. PHAM Hoai Vu
    27/07/2012 at 1:07 AM

    No problem. Btw, on Win32 you can use TransparentBlt.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: