MFC : Make a bitmap with a transparant color - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 36

Thread: MFC : Make a bitmap with a transparant color

  1. #16
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    Quote Originally Posted by thomas_nibu
    There is already a TransparentBlt function.
    how using it ?

  2. #17
    Join Date
    Nov 2005
    Location
    India, Kerala, Trivandrum
    Posts
    37

    Re: MFC : Make a bitmap with a transparant color

    You can search Code project or Code guru to find out.

    www.codeproject.com

  3. #18
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    Quote Originally Posted by thomas_nibu
    You can search Code project or Code guru to find out.

    www.codeproject.com
    Searching http://www.codeproject.com/info/sear...d=2%2F7%2F2006

    just give 6 result which dosn't help me using this function,

    an idea ? an advice ?

  4. #19
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    Quote Originally Posted by humptydumpty
    Just Tryied That Once and let me know.Don't Forget to use CBitmapClass
    There isn't any change with your code

  5. #20
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    Quote 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;
    }

  6. #21
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,258

    Re: MFC : Make a bitmap with a transparant color

    Quote Originally Posted by FireJocker
    @ John E
    ------------
    [...]
    when I modify by calling your function with the parameter bAssume100Green to "false" transparency seems working
    Yes - the transparency will work regardless of that setting. What the setting does is this....

    If a colour is found which has a high percentage of green but a low percentage of the other colours, it is assumed to be 100% green (in other words, anything that is "close" to 100% will be assumed to be exact).

    Another way of putting this is to say, "if the image contains any 'bright' green, assume that it was intended to be 100% green."
    "A problem well stated is a problem half solved. - Charles F. Kettering

  7. #22
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    @John E :

    In your code, where is get pixel color which replace the transparent one ?

    [EDIT]

    I tried to patch with the following code to test, but instead of getting right pixel I got black one where pixel might be transparent (nb: IDB_BITMAP1 is my CDialog background)

    Code:
    	//************************************************************
    	// Copy the background of the main DC to the destination.
    	//BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, 0, 0, SRCCOPY);
    	CBitmap Bitmap;
    	HDC MemDC;
      
    	Bitmap.LoadBitmap(IDB_BITMAP1); // lecture bitmap dans les ressources
        BITMAP InfosBmp;				// structure d'informations.
        Bitmap.GetBitmap(&InfosBmp);
        MemDC = CreateCompatibleDC(hdc);	// creation d'un DC en memoire
    	if (MemDC)
    	{
    		SelectObject(MemDC, &Bitmap);	// selection du bitmap dans le DC en memoire
    		// transfert final du bitmap dans le dc de la view.
    		BitBlt(hdcMem, 0,0,InfosBmp.bmWidth, InfosBmp.bmHeight, MemDC, 0,0, SRCCOPY); 
    
    		DeleteDC(MemDC);
    	}
    
    	//************************************************************
    What do you think about this new problem ?
    Last edited by FireJocker; February 7th, 2006 at 09:16 AM.

  8. #23
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,258

    Re: MFC : Make a bitmap with a transparant color

    Quote Originally Posted by FireJocker
    @John E :

    In your code, where is get pixel color which replace the transparent one ?
    In my original code, that colour is passed as a parameter to "DrawTransparentButmap(...) - i.e.

    Code:
    DrawTransparentBitmap(HDC hdc, HBITMAP hBitmap, short xStart, short yStart, COLORREF kTransparentColour, bool bAssume100Green);
    However, I noticed that you've removed that parameter in your implementation. In fact, I can't tell from your code whether you're using a different strategy so I can't guess at what you're doing wrong. However, my code does work. I'm using it myself.
    Last edited by John E; February 7th, 2006 at 09:33 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  9. #24
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    I know your code run,

    I just modify it to get the color of a pixel given by X,Y :

    Code:
    	COLORREF transparentColour = GetPixel(hdcTemp, m_iXPxlTranparent, m_iYPxlTranparent);
    
    	if (transparentColour) cColour = SetBkColor(hdcTemp, transparentColour);
    it's the only change I do (after removing thresholds possibility)

    I try to get as background a picture given by IDB_BITMAP1 (in resource)

    So i'm trying to understand how to use it in all the DC moves

    I succefully load it but where to put it in your code

    Now I'm trying to put it in the mask

    but I'm not really good with the DC

    I think I have to put it near :
    Code:
    	// Create the inverse of the object mask.
    	BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, NOTSRCCOPY);
    What do you think about it ?

  10. #25
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,258

    Re: MFC : Make a bitmap with a transparant color

    After creating the object mask (and before creating the inverse of the object mask) you need to set the background colour back to its original colour - e.g.
    Code:
    	cColour = SetBkColor(hdcTemp, kTransparentColour);
    
    	// 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);
    "A problem well stated is a problem half solved. - Charles F. Kettering

  11. #26
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Re: MFC : Make a bitmap with a transparant color

    I'm trying many things and I think I really near of the solution,

    wrinting this code :

    Code:
    	// Copy the destination to the screen.
    	BitBlt(hdc, 0, 0, ptSize.x, ptSize.y, hdcMem, 0, 0, SRCCOPY);
    	
    	//************************************************************
    
    	CBitmap Bitmap;
        CDC MemDC;
    	CDC* pDC = CDC::FromHandle(hdc) ;
      
    	Bitmap.LoadBitmap(IDB_BITMAP1); // 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
    
    	if (MemDC)
    	{
    		SelectObject(MemDC, &Bitmap);	// selection du bitmap dans le DC en memoire
    
    		// transfert final du bitmap dans le dc de la view.
    		BitBlt(hdc, 0, 0, ptSize.x, ptSize.y, MemDC, 0, 0, SRCCOPY);
    
    		DeleteDC(MemDC);
    	}
    
    //************************************************************
    
    	// Place the original bitmap back into the bitmap sent here.
    	BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
    I can see instead of my button the upper left corner of my backgroung,

    how getting the position of the button to put it here :

    BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);

    to get the good background

  12. #27
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Thumbs up Re: MFC : Make a bitmap with a transparant color

    I got it !

    Changing the background (in green) by the code in red, I got My background !
    Code:
    	// Copy the background of the main DC to the destination.
    	//BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, 0, 0, SRCCOPY);
    
    		//************************************************************
    
    	CBitmap Bitmap;
        CDC MemDC;
    	CDC* pDC = CDC::FromHandle(hdc) ;
      
    	Bitmap.LoadBitmap(IDB_BITMAP1); // lecture bitmap dans les ressources
        MemDC.CreateCompatibleDC(pDC);	// creation d'un DC en memoire
    
    
    	if (MemDC)
    	{
        MemDC.SelectObject(&Bitmap);	// selection du bitmap dans le DC en memoire
    
    		// transfert final du bitmap dans le dc de la view.
            CRect rect ; 
            this->GetWindowRect(&rect) ; 		
    		this->GetParent()->ScreenToClient(&rect);
    
    		BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, MemDC, rect.left, rect.top, SRCCOPY);
    
    		DeleteDC(MemDC);
    	}	
    
    //************************************************************
    And now, I just have to past it properly
    Last edited by FireJocker; February 7th, 2006 at 11:10 AM.

  13. #28
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Resolved Re: MFC : Make a bitmap with a transparant color

    Ok,

    So my final code is :

    Code:
    //=====================================
    //               transparentbitmapbutton.h
    //=====================================
    #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;
    
    	CBitmap m_BitmapBackground;		// Background bitmap
    	BOOL	m_BoolBackground;		// Background exist ?
    
    public:
    	CTransparentBitmapButton(int XPxlT, int YPxlT, UINT IDD_BITMAP = 0);
    	~CTransparentBitmapButton(void);
    
    	virtual void OnPaint();
    	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    };
    
    
    
    //=====================================
    //               transparentbitmapbutton.cpp
    //=====================================
    
    #include "StdAfx.h"
    #include ".\transparentbitmapbutton.h"
    
    CTransparentBitmapButton::CTransparentBitmapButton(int XPxlT,int YPxlT, UINT IDD_BITMAP)
    {
    	// Initialise the transparent pixel X,Y
    	m_iXPxlTranparent = XPxlT;
    	m_iYPxlTranparent = YPxlT;
    
    	m_BoolBackground = IDD_BITMAP != 0;
    	if (m_BoolBackground)
    	{
    		m_BitmapBackground.LoadBitmap(IDD_BITMAP); // lecture bitmap dans les ressources			
    	}
    
    }
    
    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.
    	if (m_BoolBackground) // if bitmap background exist, use it
    	{	
    		CDC MemDC;
    		CDC* pDC = CDC::FromHandle(hdc) ;
    
    		// Button location in the windows
    		CRect RectButton;			
    		this->GetWindowRect(&RectButton) ; 		
    		this->GetParent()->ScreenToClient(&RectButton);
      
    		MemDC.CreateCompatibleDC(pDC);				// creation of a DC in memory
    		MemDC.SelectObject(&m_BitmapBackground);	// select bitmap in the DC in memory 
    
    		BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, MemDC, RectButton.left, RectButton.top, SRCCOPY);
    
    		DeleteDC(MemDC);
    	}
    	else
    	{
    		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);
    }
    Any comment ?

  14. #29
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,258

    Re: MFC : Make a bitmap with a transparant color

    Sorry for the delay - CodeGuru is very slow today, for some reason.

    The code seems okay - but just bear in mind that it relies on the transparency colour being precisely right. If my experience is anything to go by - you can't rely on this with most 3rd party drawing packages. If you stipulate a particular colour, you may or may not get that colour in the final output. The final colour might be indistinguishable from the wanted colour - but "indistinguishable" isn't necessarily exact...! That's why I needed to put some code in to deal with "approximate" colours
    "A problem well stated is a problem half solved. - Charles F. Kettering

  15. #30
    Join Date
    Jan 2006
    Location
    Marseille, France
    Posts
    94

    Talking Re: MFC : Make a bitmap with a transparant color

    Ok, in fact, I put myself the transparancy colour on picture given by the graphical team, I fill all corner with only one colour, to get round rectangle button.

    If necessary, I'll know where to find your code to get an approximative colour of transparancy

    Thanks a lot for your help

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center