A little example i use to create iconic windows from PNGs. Layered windows will not work with windows 95,98,Me. Layered windows with alpha channel (RGBA) need not a window region.
Example (the bitmap must be 32bit RGBA):
unsigned char __alphatable[256][256] = { 42, };
static void __alphatable_init()
{
unsigned int x,y;
if(42==__alphatable[0][0])
{
for(x=0;x<256;x++)
for(y=0;y<256;y++)
__alphatable[x][y] = min(255,x*y/255);
}
}
static unsigned char __alphatable_value(unsigned char src,unsigned char dst,unsigned char alpha)
{
unsigned short p = __alphatable[alpha][src] + __alphatable[255-alpha][dst];
return min(p,255);
}
HBITMAP __CreateAlphaBitmap(BITMAPINFO* pbmi,const unsigned char* lpbits,const unsigned int cbbits)
{
HDC mdc;
HBITMAP hbmp;
unsigned char* lpc;
int x,y;
mdc = CreateCompatibleDC(0);
hbmp = CreateDIBSection(mdc,pbmi,DIB_RGB_COLORS,(void**)&lpc,0,0);
if(lpc)
{
__alphatable_init();
for(y=0;y<pbmi->bmiHeader.biHeight;y++)
{
for(x=0;x<pbmi->bmiHeader.biWidth;x++)
{
lpc[0] = __alphatable_value(lpbits[0],0,lpbits[3]);
lpc[1] = __alphatable_value(lpbits[1],0,lpbits[3]);
lpc[2] = __alphatable_value(lpbits[2],0,lpbits[3]);
lpc[3] = lpbits[3];
lpc += 4; lpbits += 4;
}
}
}
DeleteDC(mdc);
return hbmp;
}
void iWndWidget::SetFace(IBitmap* pBmp,HWND hwnd)
{
if(!hwnd) return;
unsigned long exstyle = GetWindowLong(hwnd,GWL_EXSTYLE);
BITMAPINFO* pbmp;
unsigned char* lp;
unsigned int cb;
if(pBmp && (S_OK==pBmp->GetBitmapDirect(&pbmp,&lp,&cb)) && (32==pbmp->bmiHeader.biBitCount))
{
RECT& rcw = GetWndPos();
BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
HDC mdc;
HDC hdc = 0;
SIZE size = { pbmp->bmiHeader.biWidth, pbmp->bmiHeader.biHeight };
POINT pdst = { rcw.left,rcw.top };
POINT psrc = { 0,0 };
HGDIOBJ obmp;
HBITMAP hbmp;
SetWindowLong(hwnd,GWL_EXSTYLE,exstyle|WS_EX_LAYERED);
hbmp = __CreateAlphaBitmap(pbmp,lp,cb);
mdc = CreateCompatibleDC(hdc);
obmp = SelectObject(mdc,hbmp);
UpdateLayeredWindow(hwnd,hdc,&pdst,&size,mdc,&psrc,(COLORREF)0,&blend,ULW_ALPHA);
SelectObject(mdc,obmp);
DeleteDC(mdc);
DeleteObject(hbmp);
SetWindowPos(hwnd,0,pdst.x,pdst.y,size.cx,size.cy,
SWP_NOZORDER|SWP_FRAMECHANGED|SWP_NOSENDCHANGING|SWP_NOACTIVATE);
}
else
{
SetWindowLong(hwnd,GWL_EXSTYLE,exstyle&~WS_EX_LAYERED);
SetWindowPos(hwnd,0,0,0,0,0,
SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_FRAMECHANGED|SWP_NOSENDCHANGING|SWP_NOACTIVATE);
}
}
Regards.