Sign in to follow this  
Mikefarr

Transarent Bitmaps usingTransparentBlt()

Recommended Posts

I am trying to use TransparentBlt() function without success! I have not been able to find an example of how it is used. Can anyone paste a piece of code showing how this function is called? I hope someone can help out. Thanks a lot.

Share this post


Link to post
Share on other sites
I am putting a bitmap on a dialog using a CStatic.
The following code works:

CStatic logoStatic;
CPoint logoPos(10,10);
CSize logoSize(211,102);
CRect logoPosSize(logoPos.x, logoPos.y, logoPos.x+logoSize.cx,logoPos.y+logoSize.cy);
logoStatic.Create(_T("") ,WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE,logoPosSize,this);
HBITMAP logoBitmap=::LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_LOGO));
logoStatic.SetBitmap(logoBitmap);


But the bitmap has a white background and I would like it to be transparent. I found that I can do this using transparentBlt().
The following is a tentative but the bitmap is not displayed on the screen! Off course I am doing something wronge, but I cannot get it right.


CPoint logoPos(10,10);
CSize logoSize(211,102);
CRect logoPosSize(logoPos.x, logoPos.y, logoPos.x+logoSize.cx,logoPos.y+logoSize.cy);
logoStatic.Create(_T(""),
WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE ,logoPosSize,this);
HBITMAP logoBitmap=::LoadBitmap(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDB_LOGO));
CDC *dc = logoStatic.GetDC();
CDC dcMem;
dcMem.CreateCompatibleDC (dc);
dcMem.SelectObject (logoBitmap);
dcMem.SetMapMode (dc->GetMapMode ());
BOOL ret =TransparentBlt(
dc->m_hDC, // handle to destination DC
logoPos.x, // x-coord of destination upper-left corner
logoPos.y, // y-coord of destination upper-left corner
logoSize.cx, // width of destination rectangle
logoSize.cy, // height of destination rectangle
dcMem.m_hDC, // handle to source DC
logoPos.x, // x-coord of source upper-left corner
logoPos.y, // y-coord of source upper-left corner
logoSize.cx, // width of source rectangle
logoSize.cy, // height of source rectangle
RGB(255,255,255) // color to make transparent
);


Michael

Share this post


Link to post
Share on other sites
I am not sure what you're using, but maybe it is just recognizing TrnsparentBlt() to be used to make the whole bitmap clear, not just the background. It helps to know where you're programming so people can test;)

Share this post


Link to post
Share on other sites
The following is some old code I used awhile back for a card game. I'm assuming that if you use TransparentBlt() instead of StretchBlt() that it'll work fine. I think the basic code you're looking for will be like what I have in PaintPlayerCard(). pButtonCwnd is a CWnd pointer to a button on a dialog box. It could just as easily a static dialog control.



/////////////////////////////////////
//Calling function

void foo()
{
int x=0;
CGame MyGame;

MyGame.LoadBitmap();

//... other stuff

MyGame.PaintPlayerCard(GetDlgItem(IDC_MYDIALOGCONTROL),x);
}



/////////////////////////////////////
//Header file
class CGame
{
...
CDC m_Bcdc;
CBitmap m_bmp;

void LoadBitmap();
void PaintPlayerCard(CWnd * pButtonCwnd, int ButtonIndex);
...
};

/////////////////////////////////////////
//CPP file

void CGame::LoadBitmap()
{
m_Bcdc.CreateCompatibleDC( NULL);
m_bmp.LoadBitmap(IDB_CARDS);

}

void CGame::PaintPlayerCard(CWnd * pButtonCwnd, int ButtonIndex)
{

RECT rc;
CBitmap * bmpOld;
int iNumber;
int iSuit;

iNumber=m_Players[0].m_Hand[ButtonIndex].m_nNumber;
iSuit=m_Players[0].m_Hand[ButtonIndex].m_nSuit;

CDC * Acdc;
Acdc = pButtonCwnd->GetDC();
bmpOld =m_Bcdc.SelectObject( &m_bmp);
pButtonCwnd->GetWindowRect(&rc);
Acdc ->StretchBlt( 0, 0, rc.right-rc.left, rc.bottom-rc.top, &m_Bcdc, 38*(iNumber-1),50*iSuit,38,50,SRCCOPY);
m_Bcdc.SelectObject( bmpOld );
pButtonCwnd->ReleaseDC(Acdc);

}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this