重写list control插入进度条

VC ListCtrl中嵌入进度条
[入库:2005年8月18日][来源]
本文简介:选择自 oeichenwei 的 blog

vc中在listctrl中嵌入进度条,截图如下:

 

其实要实现这个非常容易,以下是自绘listctrl的代码,首先继承clistctrl,
然后增加函数:
void cprocesslist::oncustomdraw(nmhdr *pnmhdr, lresult *presult)
{
//draw each item.set txt color,bkcolor....
nmlvcustomdraw* plvcd = reinterpret_cast(pnmhdr);

// 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.

*presult = cdrf_notifysubitemdraw;
}
else if (plvcd->nmcd.dwdrawstage == (cdds_itemprepaint | cdds_subitem))
{
// this is the prepaint stage for a subitem. here's where we set the
// item's text and background colors. our return value will tell
// windows to draw the subitem itself, but it will use the new colors
// we set here.

int nitem = static_cast (plvcd->nmcd.dwitemspec);
int nsubitem = plvcd->isubitem;

if(nsubitem != 2)//这里我只重绘第二列
return;

colorref crtext = ::getsyscolor(color_windowframe);
colorref crbkgnd = ::getsyscolor(color_window);

cdc* pdc = cdc::fromhandle(plvcd->nmcd.hdc);
crect rect;
getsubitemrect(nitem, nsubitem, lvir_bounds, rect);
if (getitemstate(nitem, lvis_selected))
drawtext(nitem, nsubitem, pdc, ::getsyscolor(color_highlight),
::getsyscolor(color_highlight), rect);
else
drawtext(nitem, nsubitem, pdc, crtext, crbkgnd, rect);

*presult = cdrf_skipdefault;// we've painted everything.
}
}

然后为该函数增加消息映射:
on_notify_reflect(nm_customdraw, oncustomdraw)

最后我们为画进度条而努力,这里程序中把进度存在itemdata中。
void cprocesslist::drawtext(int nitem,
int nsubitem,
cdc *pdc,
colorref crtext,
colorref crbkgnd,
crect &rect)
{
assert(pdc);
pdc->fillsolidrect(&rect, crbkgnd);

int nprocess = getitemdata(nitem);
crect procrect = rect;
pdc->rectangle(procrect);

procrect.left += 1;
procrect.bottom -= 1;
procrect.top += 1;
procrect.right = procrect.left + rect.width() * nprocess / 100;
cbrush brush(rgb(255,0,0));
pdc->fillrect(&procrect, &brush);

cstring str;
str.format("%d%%", nprocess);

if (!str.isempty())
{
uint nformat = dt_vcenter | dt_singleline | dt_center;

pdc->setbkmode(transparent);
pdc->settextcolor(crtext);
pdc->setbkcolor(crbkgnd);
pdc->drawtext(str, &rect, nformat);
}
}

怎么样?很简单吧,其实使用vc开发界面也非常迅速的,虽然还是觉得mfc的封装有java界面库封装得那么好该多好啊……


本文关键:VC ListCtrl中嵌入进度条

posted @ 2012-09-21 19:06  雷盼  阅读(549)  评论(0编辑  收藏  举报