自定义ListCtrl中设置背景图片的问题

自定义的列表控件必须是自绘制的,因此需要在资源编辑器中设置LVS_OWNERDRAWFIXED标志,而且还必须在自定义的控件类中实现DrawItem函数。

200781701.jpg


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

代码如下:

None.gif class CListCtrlEx: public CListCtrl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//Construction
InBlock.gif
public:
InBlock.gifCListCtrlEx();
InBlock.gif
InBlock.gif
public:
InBlock.gif
InBlock.gifCPalettem_pal;
//调色板
InBlock.gif
CBitmapm_bitmap;//背景位图
InBlock.gif
intm_cxBitmap,m_cyBitmap;//背景位图高度,宽度信息
InBlock.gif
intm_nHighlight;//高亮方式
InBlock.gif

InBlock.gifBOOLSetBkImage(LPCTSTRlpszResourceName);
//设置背景图片
InBlock.gif
BOOLSetBkImage(UINTnIDResource);
InBlock.gif
intGetColumnCount();//获取列数目
InBlock.gif
voidAdjustColumnWidth();//调整列宽
InBlock.gif
InBlock.gif
//Operations
InBlock.gif
public:
InBlock.gif
InBlock.gif
//Overrides
InBlock.gif
//ClassWizardgeneratedvirtualfunctionoverrides
InBlock.gif
//{{AFX_VIRTUAL(CListCtrlEx)
InBlock.gif
//}}AFX_VIRTUAL
InBlock.gif
InBlock.gif
//Implementation
InBlock.gif
public:
InBlock.gif
virtual~CListCtrlEx();
InBlock.gif
InBlock.gif
//Generatedmessagemapfunctions
InBlock.gif
protected:
InBlock.gif
//{{AFX_MSG(CListCtrlEx)
InBlock.gif
afx_msgvoidOnHScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);//水平滚动
InBlock.gif
afx_msgvoidOnVScroll(UINTnSBCode,UINTnPos,CScrollBar*pScrollBar);//垂直滚动
InBlock.gif
afx_msgBOOLOnEraseBkgnd(CDC*pDC);//擦除背景
InBlock.gif
afx_msgvoidOnPaletteChanged(CWnd*pFocusWnd);//调色板更改
InBlock.gif
afx_msgBOOLOnQueryNewPalette();//查询新调色板
InBlock.gif
afx_msgBOOLOnNotify(WPARAMwParam,LPARAMlParam,LRESULT*pResult);
InBlock.gif
//}}AFX_MSG
InBlock.gif
virtualvoidDrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct);//画每一行
InBlock.gif
DECLARE_MESSAGE_MAP()
ExpandedBlockEnd.gif}
;
None.gif


ExpandedBlockStart.gif ContractedBlock.gif /**/ /////
None.gif // CListCtrlEx
None.gif

None.gifCListCtrlEx::CListCtrlEx()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifm_nHighlight
=0;
ExpandedBlockEnd.gif}

None.gif
None.gifCListCtrlEx::
~ CListCtrlEx()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gifBEGIN_MESSAGE_MAP(CListCtrlEx,CListCtrl)
None.gif
// {{AFX_MSG_MAP(CListCtrlEx)
None.gif
ON_WM_HSCROLL()
None.gifON_WM_VSCROLL()
None.gifON_WM_ERASEBKGND()
None.gifON_WM_PALETTECHANGED()
None.gifON_WM_QUERYNEWPALETTE()
// }}AFX_MSG_MAP
None.gif
END_MESSAGE_MAP()
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /////
None.gif // CListCtrlExmessagehandlers
None.gif
BOOLCListCtrlEx::SetBkImage(UINTnIDResource)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
returnSetBkImage((LPCTSTR)nIDResource);
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCListCtrlEx::SetBkImage(LPCTSTRlpszResourceName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//IfthisisnotthefirstcallthenDeleteGDIobjects
InBlock.gif
if(m_bitmap.m_hObject!=NULL)
InBlock.gifm_bitmap.DeleteObject();
InBlock.gif
if(m_pal.m_hObject!=NULL)
InBlock.gifm_pal.DeleteObject();
InBlock.gif
InBlock.gif
InBlock.gifHBITMAPhBmp
=(HBITMAP)::LoadImage(AfxGetInstanceHandle(),
InBlock.giflpszResourceName,IMAGE_BITMAP,
0,0,LR_CREATEDIBSECTION);
InBlock.gif
InBlock.gif
if(hBmp==NULL)
InBlock.gif
returnFALSE;
InBlock.gif
InBlock.gifm_bitmap.Attach(hBmp);
InBlock.gifBITMAPbm;
InBlock.gifm_bitmap.GetBitmap(
&bm);
InBlock.gifm_cxBitmap
=bm.bmWidth;
InBlock.gifm_cyBitmap
=bm.bmHeight;
InBlock.gif
InBlock.gif
InBlock.gif
//Createalogicalpaletteforthebitmap
InBlock.gif
DIBSECTIONds;
InBlock.gifBITMAPINFOHEADER
&bmInfo=ds.dsBmih;
InBlock.gifm_bitmap.GetObject(
sizeof(ds),&ds);
InBlock.gif
InBlock.gif
intnColors=bmInfo.biClrUsed?bmInfo.biClrUsed:1<<bmInfo.biBitCount;
InBlock.gif
InBlock.gif
//Createahalftonepaletteifcolors>256.
InBlock.gif
CClientDCdc(NULL);//DesktopDC
InBlock.gif
if(nColors>256)
InBlock.gifm_pal.CreateHalftonePalette(
&dc);
InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Createthepalette
InBlock.gif

InBlock.gifRGBQUAD
*pRGB=newRGBQUAD[nColors];
InBlock.gifCDCmemDC;
InBlock.gifmemDC.CreateCompatibleDC(
&dc);
InBlock.gif
InBlock.gifmemDC.SelectObject(
&m_bitmap);
InBlock.gif::GetDIBColorTable(memDC,
0,nColors,pRGB);
InBlock.gif
InBlock.gifUINTnSize
=sizeof(LOGPALETTE)+(sizeof(PALETTEENTRY)*nColors);
InBlock.gifLOGPALETTE
*pLP=(LOGPALETTE*)newBYTE[nSize];
InBlock.gif
InBlock.gifpLP
->palVersion=0x300;
InBlock.gifpLP
->palNumEntries=nColors;
InBlock.gif
InBlock.gif
for(inti=0;i<nColors;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpLP
->palPalEntry[i].peRed=pRGB[i].rgbRed;
InBlock.gifpLP
->palPalEntry[i].peGreen=pRGB[i].rgbGreen;
InBlock.gifpLP
->palPalEntry[i].peBlue=pRGB[i].rgbBlue;
InBlock.gifpLP
->palPalEntry[i].peFlags=0;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifm_pal.CreatePalette(pLP);
InBlock.gif
InBlock.gifdelete[]pLP;
InBlock.gifdelete[]pRGB;
ExpandedSubBlockEnd.gif}

InBlock.gifInvalidate();
InBlock.gif
InBlock.gif
returnTRUE;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void CListCtrlEx::OnHScroll(UINTnSBCode,UINTnPos,CScrollBar * pScrollBar)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//TODO:Addyourmessagehandlercodehereand/orcalldefault
InBlock.gif
if(m_bitmap.m_hObject!=NULL)
InBlock.gifInvalidateRect(NULL);
InBlock.gif
InBlock.gifCListCtrl::OnHScroll(nSBCode,nPos,pScrollBar);
ExpandedBlockEnd.gif}

None.gif
None.gif
void CListCtrlEx::OnVScroll(UINTnSBCode,UINTnPos,CScrollBar * pScrollBar)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//TODO:Addyourmessagehandlercodehereand/orcalldefault
InBlock.gif
if(m_bitmap.m_hObject!=NULL)
InBlock.gifInvalidateRect(NULL);
InBlock.gif
InBlock.gifCListCtrl::OnVScroll(nSBCode,nPos,pScrollBar);
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCListCtrlEx::OnEraseBkgnd(CDC
* pDC)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//TODO:Addyourmessagehandlercodehereand/orcalldefault
InBlock.gif
if(m_bitmap.m_hObject!=NULL)
InBlock.gif
returnTRUE;
InBlock.gif
InBlock.gif
returnCListCtrl::OnEraseBkgnd(pDC);
ExpandedBlockEnd.gif}

None.gif
None.gif
void CListCtrlEx::OnPaletteChanged(CWnd * pFocusWnd)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCListCtrl::OnPaletteChanged(pFocusWnd);
InBlock.gif
InBlock.gif
//TODO:Addyourmessagehandlercodehere
InBlock.gif
if(pFocusWnd==this)
InBlock.gif
return;
InBlock.gif
InBlock.gifOnQueryNewPalette();
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCListCtrlEx::OnQueryNewPalette()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
//TODO:Addyourmessagehandlercodehereand/orcalldefault
InBlock.gif
CClientDCdc(this);
InBlock.gif
if(dc.GetDeviceCaps(RASTERCAPS)&RC_PALETTE&&m_pal.m_hObject!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifdc.SelectPalette(
&m_pal,FALSE);
InBlock.gifBOOLresult
=dc.RealizePalette();
InBlock.gif
if(result)
InBlock.gifInvalidate();
InBlock.gif
returnresult;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
returnCListCtrl::OnQueryNewPalette();
ExpandedBlockEnd.gif}

None.gif
None.gif
void CListCtrlEx::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifCDC
*pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
InBlock.gifCRectrcItem(lpDrawItemStruct
->rcItem);
InBlock.gif
intnItem=lpDrawItemStruct->itemID;
InBlock.gifCImageList
*pImageList;
InBlock.gif
InBlock.gif
//Savedcstate
InBlock.gif
intnSavedDC=pDC->SaveDC();
InBlock.gif
InBlock.gif
//Getitemimageandstateinfo
InBlock.gif
LV_ITEMlvi;
InBlock.giflvi.mask
=LVIF_IMAGE|LVIF_STATE;
InBlock.giflvi.iItem
=nItem;
InBlock.giflvi.iSubItem
=0;
InBlock.giflvi.stateMask
=0xFFFF;//getallstateflags
InBlock.gif
GetItem(&lvi);
InBlock.gif
InBlock.gif
//Shouldtheitembehighlighted
InBlock.gif
BOOLbHighlight=((lvi.state&LVIS_DROPHILITED)||((lvi.state&LVIS_SELECTED)&&((GetFocus()==this)||
InBlock.gif(GetStyle()
&LVS_SHOWSELALWAYS))));
InBlock.gif
InBlock.gif
//Getrectanglesfordrawing
InBlock.gif
CRectrcBounds,rcLabel,rcIcon;
InBlock.gifGetItemRect(nItem,rcBounds,LVIR_BOUNDS);
InBlock.gifGetItemRect(nItem,rcLabel,LVIR_LABEL);
InBlock.gifGetItemRect(nItem,rcIcon,LVIR_ICON);
InBlock.gifCRectrcCol(rcBounds);
InBlock.gif
InBlock.gifCStringsLabel
=GetItemText(nItem,0);
InBlock.gif
InBlock.gif
//Labelsareoffsetbyacertainamount
InBlock.gif
//Thisoffsetisrelatedtothewidthofaspacecharacter
InBlock.gif
intoffset=pDC->GetTextExtent(_T(""),1).cx*2;
InBlock.gif
InBlock.gifCRectrcHighlight;
InBlock.gifCRectrcClient;
InBlock.gif
intnExt;
InBlock.gif
switch(m_nHighlight)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case0:
InBlock.gifnExt
=pDC->GetOutputTextExtent(sLabel).cx+offset;
InBlock.gifrcHighlight
=rcLabel;
InBlock.gif
//if(rcLabel.left+nExt
InBlock.gif

InBlock.gif
if(m_bitmap.m_hObject!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCDCtempDC;
InBlock.giftempDC.CreateCompatibleDC(pDC);
InBlock.giftempDC.SelectObject(
&m_bitmap);
InBlock.gif
InBlock.gifGetClientRect(
&rcClient);
InBlock.gif
InBlock.gifCRgnrgnBitmap;
InBlock.gifCRectrcTmpBmp(rcItem);
InBlock.gif
InBlock.gifrcTmpBmp.right
=rcClient.right;
InBlock.gif
InBlock.gif
//Wealsoneedtocheckwhetheritisthelastitem
InBlock.gif
//Theupdateregionhastobeextendedtothebottomifitis
InBlock.gif
if(nItem==GetItemCount()-1)
InBlock.gifrcTmpBmp.bottom
=rcClient.bottom;
InBlock.gif
InBlock.gifrgnBitmap.CreateRectRgnIndirect(
&rcTmpBmp);
InBlock.gifpDC
->SelectClipRgn(&rgnBitmap);
InBlock.gifrgnBitmap.DeleteObject();
InBlock.gif
InBlock.gif
if(pDC->GetDeviceCaps(RASTERCAPS)&RC_PALETTE&&m_pal.m_hObject!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SelectPalette(&m_pal,FALSE);
InBlock.gifpDC
->RealizePalette();
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifCRectrcFirstItem;
InBlock.gifGetItemRect(
0,rcFirstItem,LVIR_BOUNDS);
InBlock.gif
InBlock.gif
for(inti=rcFirstItem.left;i<rcTmpBmp.right;i+=m_cxBitmap)
InBlock.gif
for(intj=rcFirstItem.top;j<rcTmpBmp.bottom;j+=m_cyBitmap)
InBlock.gifpDC
->BitBlt(i,j,m_cxBitmap,m_cyBitmap,&tempDC,0,0,SRCCOPY);
InBlock.gif
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
//Drawthebackgroundcolor
InBlock.gif
if(bHighlight)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
InBlock.gifpDC
->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
InBlock.gif
InBlock.gifpDC
->FillRect(rcHighlight,&CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
ExpandedSubBlockEnd.gif}

InBlock.gif
elseif(m_bitmap.m_hObject==NULL)
InBlock.gifpDC
->FillRect(rcHighlight,&CBrush(::GetSysColor(COLOR_WINDOW)));
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
//Setclipregion
InBlock.gif
rcCol.right=rcCol.left+GetColumnWidth(0);
InBlock.gifCRgnrgn;
InBlock.gifrgn.CreateRectRgnIndirect(
&rcCol);
InBlock.gifpDC
->SelectClipRgn(&rgn);
InBlock.gifrgn.DeleteObject();
InBlock.gif
InBlock.gif
//Drawstateicon
InBlock.gif
if(lvi.state&LVIS_STATEIMAGEMASK)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
intnImage=((lvi.state&LVIS_STATEIMAGEMASK)>>12)-1;
InBlock.gifpImageList
=GetImageList(LVSIL_STATE);
InBlock.gif
if(pImageList)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpImageList
->Draw(pDC,nImage,
InBlock.gifCPoint(rcCol.left,rcCol.top),ILD_TRANSPARENT);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Drawnormalandoverlayicon
InBlock.gif
pImageList=GetImageList(LVSIL_SMALL);
InBlock.gif
if(pImageList)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifUINTnOvlImageMask
=lvi.state&LVIS_OVERLAYMASK;
InBlock.gifpImageList
->Draw(pDC,lvi.iImage,
InBlock.gifCPoint(rcIcon.left,rcIcon.top),
InBlock.gif(bHighlight
?ILD_BLEND50:0)|ILD_TRANSPARENT|nOvlImageMask);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
//Drawitemlabel-Column0
InBlock.gif
rcLabel.left+=offset/2;
InBlock.gifrcLabel.right
-=offset;
InBlock.gif
InBlock.gifpDC
->DrawText(sLabel,-1,rcLabel,DT_LEFT|DT_SINGLELINE|DT_NOPREFIX|DT_NOCLIP
InBlock.gif
|DT_VCENTER|DT_END_ELLIPSIS);
InBlock.gif
InBlock.gif
InBlock.gif
//Drawlabelsforremainingcolumns
InBlock.gif
LV_COLUMNlvc;
InBlock.giflvc.mask
=LVCF_FMT|LVCF_WIDTH;
InBlock.gif
InBlock.gif
if(m_nHighlight==0)//Highlightonlyfirstcolumn
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
InBlock.gifpDC
->SetBkColor(::GetSysColor(COLOR_WINDOW));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifrcBounds.right
=rcHighlight.right>rcBounds.right?rcHighlight.right:
InBlock.gifrcBounds.right;
InBlock.gifrgn.CreateRectRgnIndirect(
&rcBounds);
InBlock.gifpDC
->SelectClipRgn(&rgn);
InBlock.gif
InBlock.gif
for(intnColumn=1;GetColumn(nColumn,&lvc);nColumn++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifrcCol.left
=rcCol.right;
InBlock.gifrcCol.right
+=lvc.cx;
InBlock.gif
InBlock.gif
//Drawthebackgroundifneeded&&m_nHighlight==HIGHLIGHT_NORMAL
InBlock.gif
if(m_bitmap.m_hObject==NULL)
InBlock.gifpDC
->FillRect(rcCol,&CBrush(::GetSysColor(COLOR_WINDOW)));
InBlock.gif
InBlock.gifsLabel
=GetItemText(nItem,nColumn);
InBlock.gif
if(sLabel.GetLength()==0)
InBlock.gif
continue;
InBlock.gif
InBlock.gif
InBlock.gif
//Getthetextjustification
InBlock.gif
UINTnJustify=DT_LEFT;
InBlock.gif
switch(lvc.fmt&LVCFMT_JUSTIFYMASK)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
caseLVCFMT_RIGHT:
InBlock.gifnJustify
=DT_RIGHT;
InBlock.gif
break;
InBlock.gif
caseLVCFMT_CENTER:
InBlock.gifnJustify
=DT_CENTER;
InBlock.gif
break;
InBlock.gif
default:
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifrcLabel
=rcCol;
InBlock.gifrcLabel.left
+=offset;
InBlock.gifrcLabel.right
-=offset;
InBlock.gif
InBlock.gifpDC
->DrawText(sLabel,-1,rcLabel,nJustify|DT_SINGLELINE
InBlock.gif
|DT_NOPREFIX|DT_VCENTER|DT_END_ELLIPSIS);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Drawfocusrectangleifitemhasfocus
InBlock.gif
if(lvi.state&LVIS_FOCUSED&&(GetFocus()==this))
InBlock.gifpDC
->DrawFocusRect(rcHighlight);
InBlock.gif
InBlock.gif
InBlock.gif
//Restoredc
InBlock.gif
pDC->RestoreDC(nSavedDC);
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void CListCtrlEx::AdjustColumnWidth()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifSetRedraw(FALSE);
InBlock.gif
intnColumnCount=GetColumnCount();
InBlock.gif
InBlock.gif
for(inti=0;i<nColumnCount;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSetColumnWidth(i,LVSCW_AUTOSIZE);
InBlock.gif
intnColumnWidth=GetColumnWidth(i);
InBlock.gifSetColumnWidth(i,LVSCW_AUTOSIZE_USEHEADER);
InBlock.gif
intnHeaderWidth=GetColumnWidth(i);
InBlock.gif
InBlock.gifSetColumnWidth(i,max(nColumnWidth,nHeaderWidth));
ExpandedSubBlockEnd.gif}

InBlock.gifSetRedraw(TRUE);
ExpandedBlockEnd.gif}

None.gif
None.gif
int CListCtrlEx::GetColumnCount()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCHeaderCtrl
*pHeaderCtrl=GetHeaderCtrl();
InBlock.gif
return(pHeaderCtrl->GetItemCount());
ExpandedBlockEnd.gif}

None.gif
None.gifBOOLCListCtrlEx::OnNotify(WPARAMwParam,LPARAMlParam,LRESULT
* pResult)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifHD_NOTIFY
*pHDN=(HD_NOTIFY*)lParam;
InBlock.gif
InBlock.gif
//Thiscodeisforusingbitmapinthebackground
InBlock.gif
//Invalidatetherightsideofthecontrolwhenacolumnisresized
InBlock.gif
if(pHDN->hdr.code==HDN_ITEMCHANGINGW||pHDN->hdr.code==HDN_ITEMCHANGINGA)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(m_bitmap.m_hObject!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCRectrcClient;
InBlock.gifGetClientRect(
&rcClient);
InBlock.gifDWORDdwPos
=GetMessagePos();
InBlock.gifCPointpt(LOWORD(dwPos),HIWORD(dwPos));
InBlock.gifScreenToClient(
&pt);
InBlock.gifrcClient.left
=pt.x;
InBlock.gifInvalidateRect(
&rcClient);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
returnCListCtrl::OnNotify(wParam,lParam,pResult);
ExpandedBlockEnd.gif}

None.gif

None.gif BOOLCCdDlg::OnInitDialog()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifCDialog::OnInitDialog();
InBlock.gif
InBlock.gif
//Add"Aboutdot.gif"menuitemtosystemmenu.
InBlock.gif
InBlock.gif
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
InBlock.gif
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
InBlock.gifASSERT(IDM_ABOUTBOX
<0xF000);
InBlock.gif
InBlock.gifCMenu
*pSysMenu=GetSystemMenu(FALSE);
InBlock.gif
if(pSysMenu!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifCStringstrAboutMenu;
InBlock.gifstrAboutMenu.LoadString(IDS_ABOUTBOX);
InBlock.gif
if(!strAboutMenu.IsEmpty())
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpSysMenu
->AppendMenu(MF_SEPARATOR);
InBlock.gifpSysMenu
->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
InBlock.gif
//whentheapplication'smainwindowisnotadialog
InBlock.gif
SetIcon(m_hIcon,TRUE);//Setbigicon
InBlock.gif
SetIcon(m_hIcon,FALSE);//Setsmallicon
InBlock.gif

InBlock.gifm_list.InsertColumn(
0,_T("用户编号"));
InBlock.gifm_list.InsertColumn(
1,_T("用户名称"));
InBlock.gifm_list.InsertColumn(
2,_T("年龄"));
InBlock.gifm_list.InsertColumn(
3,_T("性别"));
InBlock.gifm_list.InsertColumn(
4,_T("用户住址"));
InBlock.gif
charch[5];
InBlock.gif
InBlock.gif
for(intj=1;j<=200;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_list.InsertItem(j,itoa(j,ch,
10));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
for(inti=0;i<200;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gifm_list.SetItemText(i,
1,strcat(itoa(i+1,ch,10),"号用户"));
InBlock.gifm_list.SetItemText(i,
2,itoa((i+100)%100,ch,10));
InBlock.gif
if(i%2==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_list.SetItemText(i,
3,"");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifm_list.SetItemText(i,
3,"");
ExpandedSubBlockEnd.gif}

InBlock.gifm_list.SetItemText(i,
4,strcat(itoa(i+1,ch,10),"号大街"));
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifListView_SetExtendedListViewStyle(m_list.m_hWnd,LVS_EX_FULLROWSELECT
|LVS_EX_FLATSB|LVS_EX_HEADERDRAGDROP);
InBlock.gifm_list.SetBkImage(IDB_Bless);
InBlock.gifm_list.AdjustColumnWidth();
InBlock.gif
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
ExpandedBlockEnd.gif
}

效果如图所示:
200781702.jpg

但是这里还有一个问题,就是当下拉滚动条的时候,如何让背景图片不动,而只是数据行变化,这是接下来要做的工作。

参考资料:

http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c983/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值