[SOLVED] Help needed with Alphablend function [modified]
-
Is there anbody out there who has a working code snippet that uses WinApi function Alphablend()? Can Alphablend() be used with a bitmap that has no alphachannel? Thanx
modified on Sunday, June 28, 2009 5:00 AM
I use it on a splash screen. header file:
typedef BOOL (WINAPI ALPHABLEND)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
typedef ALPHABLEND* LPALPHABLEND;class ...
{
...
HINSTANCE m_hLib;
LPALPHABLEND m_lpfnAlphaBlend;
...
};source file:
// in class c'tor
m_hLib = LoadLibrary(_T("msimg32.dll"));
if (m_hLib != NULL)
{
m_lpfnAlphaBlend = (LPALPHABLEND)GetProcAddress(m_hLib, "AlphaBlend");
}// in class d'tor
if (m_hLib != NULL)
{
FreeLibrary(m_hLib);
m_hLib = NULL;
}BOOL C*********::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);if (m\_lpfnAlphaBlend != NULL) { CRect rc; GetClientRect(rc); BITMAP b; if (GetObject(m\_hBitmap, sizeof(BITMAP), &b)) { CDC DC; DC.CreateCompatibleDC(pDC); int s = DC.SaveDC(); DC.SelectObject(m\_hBitmap); BLENDFUNCTION bf = {0}; bf.BlendOp = AC\_SRC\_OVER; bf.SourceConstantAlpha = 0x20; m\_lpfnAlphaBlend(\*pDC, (rc.Width() - b.bmWidth) / 2, (rc.Height() - b.bmHeight) / 2, b.bmWidth, b.bmHeight, DC, 0, 0, b.bmWidth, b.bmHeight, bf); DC.RestoreDC(s); } } return TRUE;
}
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
I use it on a splash screen. header file:
typedef BOOL (WINAPI ALPHABLEND)(HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION);
typedef ALPHABLEND* LPALPHABLEND;class ...
{
...
HINSTANCE m_hLib;
LPALPHABLEND m_lpfnAlphaBlend;
...
};source file:
// in class c'tor
m_hLib = LoadLibrary(_T("msimg32.dll"));
if (m_hLib != NULL)
{
m_lpfnAlphaBlend = (LPALPHABLEND)GetProcAddress(m_hLib, "AlphaBlend");
}// in class d'tor
if (m_hLib != NULL)
{
FreeLibrary(m_hLib);
m_hLib = NULL;
}BOOL C*********::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);if (m\_lpfnAlphaBlend != NULL) { CRect rc; GetClientRect(rc); BITMAP b; if (GetObject(m\_hBitmap, sizeof(BITMAP), &b)) { CDC DC; DC.CreateCompatibleDC(pDC); int s = DC.SaveDC(); DC.SelectObject(m\_hBitmap); BLENDFUNCTION bf = {0}; bf.BlendOp = AC\_SRC\_OVER; bf.SourceConstantAlpha = 0x20; m\_lpfnAlphaBlend(\*pDC, (rc.Width() - b.bmWidth) / 2, (rc.Height() - b.bmHeight) / 2, b.bmWidth, b.bmHeight, DC, 0, 0, b.bmWidth, b.bmHeight, bf); DC.RestoreDC(s); } } return TRUE;
}
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
Tnx for an answer.. I spelled your code character by character and it seems that i do essentially the same. So this leaves me to ask you some stupid questions: 1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ?? 2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that? 3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend? Thanks Rozis
-
Tnx for an answer.. I spelled your code character by character and it seems that i do essentially the same. So this leaves me to ask you some stupid questions: 1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ?? 2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that? 3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend? Thanks Rozis
Rozis wrote:
1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ??
from wingdi.h:
#define AC_SRC_OVER 0x00
Rozis wrote:
2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that?
When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.
So you are correct, I did not worry about the alpha channel. I just set the AlphaFormat value to zero.
Rozis wrote:
3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend?
See http://msdn.microsoft.com/en-ca/library/dd162945(VS.85).aspx[^] and http://www.flounder.com/savedc.htm[^]
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
Rozis wrote:
1. Is bf.BlendOp = AC_SRC_OVER; the same as: bf.BlendOp = 0; ??
from wingdi.h:
#define AC_SRC_OVER 0x00
Rozis wrote:
2. To my opinion you do nothing to add an alpha channel to your bitmap. Thus your bitmap has no alpha channel - do you agree on that?
When the AlphaFormat member is AC_SRC_ALPHA, the source bitmap must be 32 bpp. If it is not, the AlphaBlend function will fail.
So you are correct, I did not worry about the alpha channel. I just set the AlphaFormat value to zero.
Rozis wrote:
3. Because i use another language then you: what does int s = DC.SaveDC() ; do? Is this one essential to Alphablend?
See http://msdn.microsoft.com/en-ca/library/dd162945(VS.85).aspx[^] and http://www.flounder.com/savedc.htm[^]
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
hmmm... im out of questions but it still doesn't run. This my code: (warning: this is not C-code, but i'll explain when needed)
hMemDC:=CreateCompatibleDC(hdc) // ':=' is used as '=' in C
if hMemDC!=NULL_PTR
hbitmap:=CreatecompatibleBitmap(hdc,w,h)
hold:=SelectObject(hMemDC,hBitmap)
Rectangle(hmemDC,0,0,w,h)
blendfunc.BlendOp:=0 // blendfunc is a structure BlendOp a member of this structure
blendfunc.BlendFlags:=0
blendfunc.SourceConstantAlpha:=128 //half transparent
blendfunc.AlphaFormat:=0
AlphaBlend(hdc,x,y,w,h,hMemDC,0,0,w,h,@blendfunc) // Here's the troublemaker
//StretchBlt(hdc,x,x,w,h,hMemDC,0,0,w,h,SRCCOPY)
//BitBlt(hdc,x,x,w,h,hMemDC,0,0,SRCCOPY)
SelectObject(hMemDC,hold)
DeleteObject(hBitmap)
DeleteDC(hMemDC)
endifWhen i use StretchBlt() or BitBlt() instead it works (altough not transparent). I checked if x,y,w and h are possitive - they are. Think you may ask whats '@blendfunc'. It means 'pointer to structure'. Use this construct for years so must be correct (for example in GetClientRect()). Tried a demo-exe from the code project to find out if problem is system related - it worked ok. Checked the prototype and the structure. I must do something really stupid... Rozis
-
hmmm... im out of questions but it still doesn't run. This my code: (warning: this is not C-code, but i'll explain when needed)
hMemDC:=CreateCompatibleDC(hdc) // ':=' is used as '=' in C
if hMemDC!=NULL_PTR
hbitmap:=CreatecompatibleBitmap(hdc,w,h)
hold:=SelectObject(hMemDC,hBitmap)
Rectangle(hmemDC,0,0,w,h)
blendfunc.BlendOp:=0 // blendfunc is a structure BlendOp a member of this structure
blendfunc.BlendFlags:=0
blendfunc.SourceConstantAlpha:=128 //half transparent
blendfunc.AlphaFormat:=0
AlphaBlend(hdc,x,y,w,h,hMemDC,0,0,w,h,@blendfunc) // Here's the troublemaker
//StretchBlt(hdc,x,x,w,h,hMemDC,0,0,w,h,SRCCOPY)
//BitBlt(hdc,x,x,w,h,hMemDC,0,0,SRCCOPY)
SelectObject(hMemDC,hold)
DeleteObject(hBitmap)
DeleteDC(hMemDC)
endifWhen i use StretchBlt() or BitBlt() instead it works (altough not transparent). I checked if x,y,w and h are possitive - they are. Think you may ask whats '@blendfunc'. It means 'pointer to structure'. Use this construct for years so must be correct (for example in GetClientRect()). Tried a demo-exe from the code project to find out if problem is system related - it worked ok. Checked the prototype and the structure. I must do something really stupid... Rozis
Rozis wrote:
Think you may ask whats '@blendfunc'. It means 'pointer to structure'.
That is the problem right there. AlphaBlend takes the structure, not a pointer to a structure, as the final parameter.
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
Rozis wrote:
Think you may ask whats '@blendfunc'. It means 'pointer to structure'.
That is the problem right there. AlphaBlend takes the structure, not a pointer to a structure, as the final parameter.
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
Guess what, you're right!! That was the thing i was overlooking. You're a genius!!! Did a quick try, made 0x00800000 as the last parameter to Alphablend() and it does the job. And it is lightyears faster then the workaround i made with getpixel(). Problem solved! The reason i never questioned it, is because a struct is a chunk of memory so i expected this would always be a pointer when used as a parameter. I mean: when a have a struct of let's say 200 bytes and use it as a parameter to a function would this mean these 200 bytes are allocated on the stack? Saw you're living in Canada, although you have a dutch name. So whenever you are in holland, call me, i have a present for you. Thanks for taking your time. Rozis
-
Guess what, you're right!! That was the thing i was overlooking. You're a genius!!! Did a quick try, made 0x00800000 as the last parameter to Alphablend() and it does the job. And it is lightyears faster then the workaround i made with getpixel(). Problem solved! The reason i never questioned it, is because a struct is a chunk of memory so i expected this would always be a pointer when used as a parameter. I mean: when a have a struct of let's say 200 bytes and use it as a parameter to a function would this mean these 200 bytes are allocated on the stack? Saw you're living in Canada, although you have a dutch name. So whenever you are in holland, call me, i have a present for you. Thanks for taking your time. Rozis
Rozis wrote:
You're a genius!!!
Not really, I just read the documentation :rolleyes: , I am just glad to be able to help.
Rozis wrote:
Saw you're living in Canada, although you have a dutch name.So whenever you are in holland, call me, i have a present for you
My grandparents emigrated from Holland to Canada in the fifties. I would love to go there some day and see where my heritage comes from. Maybe when I do finally manage the trip I will try to look you up and take you up on your offer.
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!