I setup an image list for a listview
using ILC_COLOR32
and then used a 32-bit PNG with transparancy and GdiPlus to load it to the image list using ImageList_Add(himl, hbmp, NULL);
. I also setup the listview
background to be some random color to test transparancy. However I don't understand the results I'm getting in the transparancy areas.
Also, where I can get the transparancy to work, I don't understand why there is a gap between icons?
Can someone explain why I get the results I do and if there a way to get rid of the gap between icons (to fill with the background color)?
//--------------
// this produces white (matches standard window color) in the transparent areas
//--------------
Bitmap* bitmap=Bitmap::FromFile(pfilepath, false);
HBITMAP hbmp;
bitmap->GetHBITMAP(Color(255, 1, 1), &hbmp);
delete bitmap;
//--------------
// this produces white as well
//--------------
Bitmap* bitmap=Bitmap::FromFile(pfilepath, false);
HBITMAP hbmp;
bitmap->GetHBITMAP(Color(255, 1, 1), &hbmp);
delete bitmap;
if (hbmp) {
CalculateAlphaChannel(hbmp);
}
//--------------
// This produces red in the transparent areas (as expected)
//--------------
Bitmap* bitmap=Bitmap::FromFile(pfilepath, false);
HBITMAP hbmp;
bitmap->GetHBITMAP(Color(255, 1, 1), &hbmp);
delete bitmap;
if (hbmp) {
BITMAP bm;
if (::GetObject(hbmp, sizeof(bm), &bm)!=0) {
if (bm.bmBitsPixel==32 && bm.bmBits) {
for (int h=0, i=sizeof(COLORREF)-1; h<bm.bmHeight; h++) {
for (int w=0; w<bm.bmWidth; w++) {
((LPBYTE) bm.bmBits)[i]=0;
i+=sizeof(COLORREF);
}
}
}
}
}
//--------------
// this produces black in the transparent areas
//--------------
Bitmap* bitmap=Bitmap::FromFile(pfilepath, false);
HBITMAP hbmp;
bitmap->GetHBITMAP(Color(255, 1, 1), &hbmp);
delete bitmap;
if (hbmp) {
CalculateAlphaChannel(hbmp);
BITMAP bm;
if (::GetObject(hbmp, sizeof(bm), &bm)!=0) {
if (bm.bmBitsPixel==32 && bm.bmBits) {
for (int h=0, i=sizeof(COLORREF)-1; h<bm.bmHeight; h++) {
for (int w=0; w<bm.bmWidth; w++) {
((LPBYTE) bm.bmBits)[i]=0;
i+=sizeof(COLORREF);
}
}
}
}
}
To be complete, the listview background color is handle via the NM_CUSTOMDRAW
notification message.
INT_PTR CGUIObjControl::ProcessCustomDraw(LPARAM lparam)
{
LPNMLVCUSTOMDRAW plvcd=(LPNMLVCUSTOMDRAW) lparam;
switch (plvcd->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
{
//Before the paint cycle begins
if (HasTextColor() || GetBrush()) {
//request notifications for individual listview items
return CDRF_NOTIFYITEMDRAW;
}
break;
}
//case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
case CDDS_ITEMPREPAINT:
{
// Before an item is drawn
if (HasTextColor()) {
plvcd->clrText=TextColor();
}
if (GetBrush()) {
SelectObject(plvcd->nmcd.hdc, GetBrush());
plvcd->clrTextBk=BackgroundColor();
}
// done - or could return CDRF_NOTIFYSUBITEMDRAW to handle those
// via case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
return CDRF_NEWFONT;
}
}
return CDRF_DODEFAULT;
}