首页 > 其他分享 >自绘列表框控件显示略缩图----再稍微改进点点。。

自绘列表框控件显示略缩图----再稍微改进点点。。

时间:2022-12-13 20:36:57浏览次数:36  
标签:控件 列表框 自绘 nSubItem sizeImage pLVCD nItem pXLCD rect


每天思考一点无聊的问题,又是接上一篇日记的列表框自绘,在上一个自绘里面图片是用WINDOWS自己画上去的,这次彻底的全部的自己绘制(包括文字),这样做的好处是图片上不会覆盖一层阴影影响美观,改动是下面的红色显示的代码片段,有一个问题不知道为什么,就是下面这句

GetSubItemRect(nItem, nSubItem, LVIR_ICON, rect);

得到的矩形rect的宽度总是比图片宽度大42像素(列表框在ICO模式时),why?

 

 


自绘列表框控件显示略缩图----再稍微改进点点。。_processing

                              

自绘列表框控件显示略缩图----再稍微改进点点。。_notifications_02

 


只是在前面的基础上修改这两个函数。。。


///


OnNMCustomdraw

void CListImageCtrl::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)

{

//LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);

NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);



//TRACE("CListImageCtrl::OnNMCustomdraw()");

// Take the default processing unless we set this to something else below.

*pResult = CDRF_DODEFAULT;



// First thing - check the draw stage. If it's the control's prepaint

// stage, then tell Windows we want messages for every item.



if (pLVCD->nmcd.dwDrawStage == CDDS_PREPAINT)

{

*pResult = CDRF_NOTIFYITEMDRAW;

}

else if (pLVCD->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)

{

// This is the notification message for an item. We'll request

// notifications before each subitem's prepaint stage.

int nItem = static_cast<int> (pLVCD->nmcd.dwItemSpec);

int nSubItem = pLVCD->iSubItem;

CDC* pDC = CDC::FromHandle(pLVCD->nmcd.hdc);

CRect rect;

GetSubItemRect(nItem, nSubItem, LVIR_ICON, rect);



XLISTCTRLDATA *pXLCD = (XLISTCTRLDATA *) pLVCD->nmcd.lItemlParam;

ASSERT(pXLCD);



COLORREF crText = m_crWindowText;

COLORREF crBkgnd = m_crWindow;



if (pXLCD)

{

crText = pXLCD[nSubItem].crText;

crBkgnd = pXLCD[nSubItem].crBackground;



if (!pXLCD[0].bEnabled)

crText = m_crGrayText;

}



// store the colors back in the NMLVCUSTOMDRAW struct

pLVCD->clrText = crText;

pLVCD->clrTextBk = crBkgnd;





//draw image,

rect.left += 19;

rect.right -= 19;

DrawImage(nItem, nSubItem, pDC, crText, crBkgnd, rect, pXLCD);



//draw checkbox

if (pXLCD && (pXLCD[nSubItem].nCheckedState != -1))

DrawCheckbox(nItem, nSubItem, pDC, crText, crBkgnd, rect,pXLCD);

//draw text

GetSubItemRect(nItem, nSubItem, LVIR_LABEL, rect);



DrawText(nItem, nSubItem, pDC, crText, crBkgnd, rect, pXLCD);



*pResult = CDRF_SKIPDEFAULT; //we have done all the paint



}

else if (pLVCD->nmcd.dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))

{



*pResult = CDRF_DODEFAULT;

}



}





///



// DrawImage



int CListImageCtrl::DrawImage(int nItem,















{


if (rect.IsRectEmpty())

{

return 0;

}




GetDrawColors(nItem, nSubItem, crText, crBkgnd);





CBrush bkBrush(crBkgnd);

CRgn rgn;

rgn.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom);

pDC->FrameRgn(&rgn,&bkBrush,2,2); //draw my border



int nGap = 0;

rect.left += 0;




CImageList* pImageList = GetImageList(LVSIL_SMALL);

if (pImageList)

{

SIZE sizeImage;

sizeImage.cx = sizeImage.cy = 0;

IMAGEINFO info;




int nImage = -1;

if (pXLCD)

nImage = pXLCD[nSubItem].nImage;




if (nImage == -1)

return 0;




if (pImageList->GetImageInfo(nImage, &info))

{

sizeImage.cx = info.rcImage.right - info.rcImage.left;

sizeImage.cy = info.rcImage.bottom - info.rcImage.top;

}




if (nImage >= 0)

{

if (rect.Width() > 0)

{

POINT point;




point.y = rect.CenterPoint().y - (sizeImage.cy >> 1);

point.x = rect.CenterPoint().x - (sizeImage.cx >> 1);




SIZE size;

size.cx = rect.Width() < sizeImage.cx ? rect.Width() : sizeImage.cx;

size.cy = rect.Height() < sizeImage.cy ? rect.Height() : sizeImage.cy;




// save image list background color

COLORREF rgb = pImageList->GetBkColor();




// set image list background color

pImageList->SetBkColor(crBkgnd);

pImageList->DrawIndirect(pDC, nImage, point, size, CPoint(0, 0));

pImageList->SetBkColor(rgb);




nGap = sizeImage.cx + 5;

}

}

}




return nGap;


}

标签:控件,列表框,自绘,nSubItem,sizeImage,pLVCD,nItem,pXLCD,rect
From: https://blog.51cto.com/u_15911341/5935030

相关文章