在做一个仿QQ好友列表那样的效果。 用的list control控件, report风格。重载DrawItem. 现在能显示特定的bmp格式图标,有的bmp显示不出来。 经过试验发现只要给bmp图片在photoshop中添加一个alpha通道,就可以显示了。 因为程序运行后要从文件中载入图片。不能从资源中获取。 所以这些bmp图片不可能我每一张都手动去ps里处理。 想问问这是为什么? 为什么需要添加alpha通道才能在重绘后的list control显示出来。 是我重绘代码写错了吗?
void COrangeListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
CBitmap *bmp=new CBitmap;
CString strText;
int wz = 0;
TCHAR lpBuffer[256];
LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
long hight = 0;
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
lpDrawItemStruct->rcItem.left += hight ;
lpDrawItemStruct->rcItem.right += hight ;
hight = lvc.cx ;
LV_ITEM lvi;
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&lpDrawItemStruct->rcItem,RGB(231, 243, 229)) ;
pDC->SetTextColor(RGB(255, 0, 0)) ;
}
}
//载入图片
m_imagelist.Create(ICON_SHOW_SIZE, ICON_SHOW_SIZE,ILC_COLORDDB|ILC_COLOR32 | ILC_MASK, 5, 5);
bmp->m_hObject=(HBITMAP)LoadImage(AfxGetInstanceHandle(), _T("qqface\\a12.bmp"), IMAGE_BITMAP, ICON_SHOW_SIZE, ICON_SHOW_SIZE, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
m_imagelist.Add(bmp, RGB(255,255, 255));
strText = lpBuffer;
CRect lrect = lpDrawItemStruct->rcItem;
CDC ldc;
ldc.CreateCompatibleDC(pDC);
CBitmap lbmp;
lbmp.CreateCompatibleBitmap(pDC, ICON_SHOW_SIZE, ICON_SHOW_SIZE);
ldc.SelectObject(lbmp);
m_imagelist.Draw(&ldc, lpDrawItemStruct->itemID % 2, CPoint(0, 0), ILD_TRANSPARENT);
BLENDFUNCTION ab;
ab.BlendOp = AC_SRC_OVER;
ab.BlendFlags = 0;
ab.SourceConstantAlpha = 0xFF;
ab.AlphaFormat = AC_SRC_ALPHA;
AlphaBlend(pDC->GetSafeHdc(), lrect.left, lrect.top, ICON_SHOW_SIZE, ICON_SHOW_SIZE, ldc.GetSafeHdc(), 0, 0, ICON_SHOW_SIZE, ICON_SHOW_SIZE, ab);
CFont lfont;
lfont.CreateFont(12, 0, 0, 0, FW_BOLD ,0, 1, 0, GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH ,L"宋体");
CFont* pold = pDC->SelectObject(&lfont);
wz = ICON_SHOW_SIZE + 10;
lrect.DeflateRect( wz, 6, 0, 0);
pDC->DrawText(strText, lrect, DT_LEFT | DT_TOP | DT_SINGLELINE);
pDC->SelectObject(pold);
pDC->SetTextColor(RGB(223, 0, 0));
}
report list LoadImage bmp alpha
------解决方案--------------------
恭喜解决,,看不懂,但要顶一下。