|
-
February 7th, 2006, 08:33 AM
#35
Re: MFC : Make a bitmap with a transparant color
 Originally Posted by John E
Not as far as I know - though someone might correct me about that.
In fact, yes I modified your code to get the color of a given pixel to make the transparancy
Code:
#pragma once
#include "afxext.h"
class CTransparentBitmapButton :
public CBitmapButton
{
private:
void DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap);
// The colour inside this button's bitmap(s) that should be regarded as transparent
// is selected by the one of the pixel locate by X, Y
int m_iXPxlTranparent, m_iYPxlTranparent;
public:
CTransparentBitmapButton(int XPxlT,int YPxlT);
~CTransparentBitmapButton(void);
virtual void OnPaint();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
#include "StdAfx.h"
#include ".\transparentbitmapbutton.h"
CTransparentBitmapButton::CTransparentBitmapButton(int XPxlT,int YPxlT)
{
// Initialise the transparent pixel X,Y
m_iXPxlTranparent = XPxlT;
m_iYPxlTranparent = YPxlT;
}
CTransparentBitmapButton::~CTransparentBitmapButton(void)
{
}
// From 'OnPaint()
void CTransparentBitmapButton::OnPaint()
{
if ((GetParent()) && (GetParent()->IsIconic()))
{
CBitmapButton::OnPaint();
}
else
{
CPaintDC dc(this); // device context for painting
// At least onw bitmap must be loaded before calling 'Paint()'
ASSERT(m_bitmap.m_hObject != NULL);
// and we'll assume until we find otherwise, that
// the button is being drawn in its normal state.
CBitmap* pBitmap = &m_bitmap;
if ((!(IsWindowEnabled())) && (NULL != m_bitmapDisabled.m_hObject))
pBitmap = &m_bitmapDisabled;
DrawTransparentBitmap(dc.m_hDC, *pBitmap);
}
}
void CTransparentBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
// At least onw bitmap must be loaded before calling 'DrawItem()'
ASSERT(m_bitmap.m_hObject != NULL);
CBitmap* pBitmap = &m_bitmap;
UINT state = lpDrawItemStruct->itemState;
if ((state & ODS_SELECTED) && (NULL != m_bitmapSel.m_hObject))
pBitmap = &m_bitmapSel;
else if ((state & ODS_FOCUS) && (NULL != m_bitmapFocus.m_hObject))
pBitmap = &m_bitmapFocus;
else if ((state & ODS_DISABLED) && (NULL != m_bitmapDisabled.m_hObject))
pBitmap = &m_bitmapDisabled;
DrawTransparentBitmap(lpDrawItemStruct->hDC, *pBitmap);
}
void CTransparentBitmapButton::DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap)
{
BITMAP bm;
COLORREF cColour;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
hdcTemp = CreateCompatibleDC(hdc);
if (HGDIOBJ hOldObj = SelectObject(hdcTemp, hBitmap)) // Select the bitmap
DeleteObject(hOldObj);
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC. DCs are required for a number of GDI functions.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP)SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP)SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP)SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP)SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
COLORREF transparentColour = GetPixel(hdcTemp, m_iXPxlTranparent, m_iYPxlTranparent);
if (transparentColour) cColour = SetBkColor(hdcTemp, transparentColour);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
SetBkColor(hdcTemp, cColour);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, 0, 0, SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent coloured pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, 0, 0, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
But as I tell you before this post, I got the grey of the CDialog classic instead of the color of the bitmap I draw on the CDialog with this code :
Code:
BOOL CDigiDialog::OnEraseBkgnd(CDC* pDC)
{
CBitmap Bitmap;
CDC MemDC;
Bitmap.LoadBitmap(p_iIDD_Background); // lecture bitmap dans les ressources
BITMAP InfosBmp; // structure d'informations.
Bitmap.GetBitmap(&InfosBmp);
MemDC.CreateCompatibleDC(pDC); // creation d'un DC en memoire
MemDC.SelectObject(&Bitmap); // selection du bitmap dans le DC en memoire
// transfert final du bitmap dans le dc de la view.
pDC->BitBlt( 0,0,InfosBmp.bmWidth, InfosBmp.bmHeight,
&MemDC,
0,0,
SRCCOPY);
return TRUE;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|