http://codeguru.earthweb.com/listview/background_image.shtml
解决方案 »
- 关于树控件节点命名排序的问题,求助
- 如何让创建的控制台窗口执行结束后还能停留在桌面上
- 从数据库读取数据后,为什么不能从_variant_t 转换为int,而只能转换为short?
- 建立类时自动化的选择?
- Variant类型如何转为string类型?
- 怎样做一个控制台(跟2000的CMD.exe一样)?
- 如果获得一个进程中已打开了哪些文件?
- Windows API 编程的问题。如何精确调整客户区的大小?
- 这是一个外部程序的没有关闭 最大化 最小化的窗口 它是自动弹出的 我用findwindow获得它的句柄 但sendmessage没响应 请问怎么办 谢谢
- 怎样在odbc中添加新表?是join好还是多记录集好?用了这两者好像都不能更改数据了?怎样才能再更改数据?
- 高手请进!高分求助!
- 编辑框问题
other is use NM_COSTUMDRAW. the second way
is very simple. the netants use it.
after ie4,the listview has support the background(bmp).
if you want use second way,you need do as follow.
1) in your app class OnInstance...,you need call AfxOleInit();
2) in your ListView init..,you need call function SetBkImage();there three type,
but i feel that using the arg is file name.
3) then the listview has bkground but it is opaque,if you want it is transparent
you need use NM_CUSTOMDRAW notify message.
in your .h define callback function.
afx_msg void OnCustomDrawYourList( NMHDR* pNMHDR, LRESULT* pResult );
in your .cpp
add message entry
ON_NOTIFY(NM_CUSTOMDRAW, YOUR_LIST_ID, OnCustomDrawYourList)
implement the callbak function.
OnCustomDrawYourList(NMHDR* pNMHDR, LRESULT* pResult );
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
*pResult = 0; if(pLVCD->nmcd.dwDrawStage==CDDS_PREPAINT){
*pResult=CDRF_NOTIFYITEMDRAW;
}
else if(pLVCD->nmcd.dwDrawStage==(CDDS_ITEMPREPAINT | CDDS_SUBITEM)){ CDC* pDC=CDC::FromHandle(pLVCD->nmcd.hdc);
int nItem = static_cast<int>( pLVCD->nmcd.dwItemSpec );
CRect rcItem,rcText;
LVITEM rItem;
CString sText=m_list.GetItemText(nItem,pLVCD->iSubItem); UINT uFormat=ILD_TRANSPARENT;
BOOL bListHasFocus = ( m_list.GetSafeHwnd() == ::GetFocus() );
ZeroMemory ( &rItem, sizeof(LVITEM) );
rItem.mask = LVIF_IMAGE | LVIF_STATE;
rItem.iItem = nItem;
rItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
m_list.GetItem ( &rItem ); //draw icon
if(pLVCD->iSubItem==0){
m_list.GetItemRect (nItem, &rcItem, LVIR_ICON );
if((rItem.state & LVIS_SELECTED ) && bListHasFocus )
uFormat |= ILD_FOCUS;
m_imagelist.Draw(pDC,rItem.iImage,rcItem.TopLeft(),uFormat);
} //get whole item rect.
m_list.GetSubItemRect(nItem,pLVCD->iSubItem,LVIR_LABEL,rcItem);
rcText=rcItem;
rcText.top++;
if(pLVCD->iSubItem==0){
rcText.left += 20;
rcItem.left +=20;
} if ( rItem.state & LVIS_SELECTED ){
if ( bListHasFocus ){
pDC->SetTextColor (RGB(255,255,255));
if(pLVCD->iSubItem !=0)
pDC->FillSolidRect ( rcItem, RGB(0,0,255));
}else{
if(pLVCD->iSubItem !=0)
pDC->FillSolidRect ( rcItem, RGB(192,192,192));
} }else{
if(pLVCD->nmcd.uItemState & CDIS_HOT)
pDC->SetTextColor(RGB(0,0,255));
else
pDC->SetTextColor ( GetSysColor ( COLOR_BTNTEXT ));
}
pDC->SetBkMode ( TRANSPARENT );
pDC->DrawText ( sText, rcText, DT_VCENTER | DT_SINGLELINE ); // Draw a focus rect around the item if necessary.
if ( bListHasFocus && ( rItem.state & LVIS_FOCUSED ) && pLVCD->iSubItem !=0 ){
pDC->DrawFocusRect ( rcItem );
} *pResult = CDRF_SKIPDEFAULT;
}
else if(pLVCD->nmcd.dwDrawStage ==CDDS_ITEMPREPAINT){
*pResult=CDRF_NOTIFYSUBITEMDRAW;
}
}4 when the listview item is changed,you need invalidate the listview.To treeview,i think it is implemented with the same way. in fact, i
have finish part work.