自定义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.gif    CListCtrlEx();
InBlock.gif
InBlock.gif
public:
InBlock.gif
InBlock.gif    CPalette m_pal;
//调色板
InBlock.gif
    CBitmap m_bitmap;//背景位图
InBlock.gif
    int m_cxBitmap, m_cyBitmap;//背景位图高度,宽度信息
InBlock.gif
    int m_nHighlight;//高亮方式
InBlock.gif

InBlock.gif    BOOL SetBkImage(LPCTSTR lpszResourceName);
//设置背景图片
InBlock.gif
    BOOL SetBkImage(UINT nIDResource);
InBlock.gif    
int GetColumnCount();//获取列数目
InBlock.gif
    void AdjustColumnWidth();//调整列宽
InBlock.gif
InBlock.gif
// Operations
InBlock.gif
public:
InBlock.gif
InBlock.gif
// Overrides
InBlock.gif    
// ClassWizard generated virtual function overrides
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    
// Generated message map functions
InBlock.gif
protected:
InBlock.gif    
//{{AFX_MSG(CListCtrlEx)
InBlock.gif
    afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);//水平滚动
InBlock.gif
    afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);//垂直滚动
InBlock.gif
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);//擦除背景
InBlock.gif
    afx_msg void OnPaletteChanged(CWnd* pFocusWnd);//调色板更改
InBlock.gif
    afx_msg BOOL OnQueryNewPalette();//查询新调色板
InBlock.gif
    afx_msg BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
InBlock.gif    
//}}AFX_MSG
InBlock.gif
     virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);//画每一行
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.gif   m_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.gif    ON_WM_VSCROLL()
None.gif    ON_WM_ERASEBKGND()
None.gif    ON_WM_PALETTECHANGED()
None.gif    ON_WM_QUERYNEWPALETTE()    
// }}AFX_MSG_MAP
None.gif
END_MESSAGE_MAP()
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /////
None.gif //  CListCtrlEx message handlers
None.gif
BOOL CListCtrlEx::SetBkImage(UINT nIDResource)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return SetBkImage((LPCTSTR)nIDResource);
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CListCtrlEx::SetBkImage(LPCTSTR lpszResourceName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
// If this is not the first call then Delete GDI objects
InBlock.gif
    if( m_bitmap.m_hObject != NULL )
InBlock.gif        m_bitmap.DeleteObject();
InBlock.gif    
if( m_pal.m_hObject != NULL )
InBlock.gif        m_pal.DeleteObject();
InBlock.gif
InBlock.gif
InBlock.gif    HBITMAP hBmp 
= (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
InBlock.gif            lpszResourceName, IMAGE_BITMAP, 
0,0, LR_CREATEDIBSECTION );
InBlock.gif
InBlock.gif    
if( hBmp == NULL )
InBlock.gif        
return FALSE;
InBlock.gif
InBlock.gif    m_bitmap.Attach( hBmp );
InBlock.gif    BITMAP bm;
InBlock.gif    m_bitmap.GetBitmap( 
&bm );
InBlock.gif    m_cxBitmap 
= bm.bmWidth;
InBlock.gif    m_cyBitmap 
= bm.bmHeight;
InBlock.gif
InBlock.gif
InBlock.gif    
// Create a logical palette for the bitmap
InBlock.gif
    DIBSECTION ds;
InBlock.gif    BITMAPINFOHEADER 
&bmInfo = ds.dsBmih;
InBlock.gif    m_bitmap.GetObject( 
sizeof(ds), &ds );
InBlock.gif
InBlock.gif    
int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
InBlock.gif
InBlock.gif    
// Create a halftone palette if colors > 256. 
InBlock.gif
    CClientDC dc(NULL);            // Desktop DC
InBlock.gif
    if( nColors > 256 )
InBlock.gif        m_pal.CreateHalftonePalette( 
&dc );
InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// Create the palette
InBlock.gif

InBlock.gif        RGBQUAD 
*pRGB = new RGBQUAD[nColors];
InBlock.gif        CDC memDC;
InBlock.gif        memDC.CreateCompatibleDC(
&dc);
InBlock.gif
InBlock.gif        memDC.SelectObject( 
&m_bitmap );
InBlock.gif        ::GetDIBColorTable( memDC, 
0, nColors, pRGB );
InBlock.gif
InBlock.gif        UINT nSize 
= sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
InBlock.gif        LOGPALETTE 
*pLP = (LOGPALETTE *new BYTE[nSize];
InBlock.gif
InBlock.gif        pLP
->palVersion = 0x300;
InBlock.gif        pLP
->palNumEntries = nColors;
InBlock.gif
InBlock.gif        
forint i=0; i < nColors; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            pLP
->palPalEntry[i].peRed = pRGB[i].rgbRed;
InBlock.gif            pLP
->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
InBlock.gif            pLP
->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
InBlock.gif            pLP
->palPalEntry[i].peFlags = 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        m_pal.CreatePalette( pLP );
InBlock.gif
InBlock.gif        delete[] pLP;
InBlock.gif        delete[] pRGB;
ExpandedSubBlockEnd.gif    }

InBlock.gif    Invalidate();
InBlock.gif
InBlock.gif    
return TRUE;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CListCtrlEx::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *  pScrollBar) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your message handler code here and/or call default
InBlock.gif
    if( m_bitmap.m_hObject != NULL ) 
InBlock.gif        InvalidateRect(NULL);    
InBlock.gif    
InBlock.gif    CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CListCtrlEx::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *  pScrollBar) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your message handler code here and/or call default
InBlock.gif
    if( m_bitmap.m_hObject != NULL )  
InBlock.gif        InvalidateRect(NULL);    
InBlock.gif    
InBlock.gif    CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CListCtrlEx::OnEraseBkgnd(CDC
*  pDC) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your message handler code here and/or call default
InBlock.gif
    if( m_bitmap.m_hObject != NULL )    
InBlock.gif        
return TRUE;
InBlock.gif    
InBlock.gif    
return CListCtrl::OnEraseBkgnd(pDC);
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CListCtrlEx::OnPaletteChanged(CWnd *  pFocusWnd) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    CListCtrl::OnPaletteChanged(pFocusWnd);
InBlock.gif    
InBlock.gif    
// TODO: Add your message handler code here
InBlock.gif
    if( pFocusWnd == this )    
InBlock.gif        
return;
InBlock.gif
InBlock.gif    OnQueryNewPalette();        
ExpandedBlockEnd.gif}

None.gif
None.gifBOOL CListCtrlEx::OnQueryNewPalette() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
// TODO: Add your message handler code here and/or call default
InBlock.gif
    CClientDC dc(this);
InBlock.gif    
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_pal.m_hObject != NULL )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        dc.SelectPalette( 
&m_pal, FALSE );
InBlock.gif        BOOL result 
= dc.RealizePalette();
InBlock.gif        
if( result ) 
InBlock.gif            Invalidate();
InBlock.gif        
return result;
ExpandedSubBlockEnd.gif    }
    
InBlock.gif    
InBlock.gif    
return CListCtrl::OnQueryNewPalette();
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CListCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif    CDC
* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
InBlock.gif    CRect rcItem(lpDrawItemStruct
->rcItem);
InBlock.gif    
int nItem = lpDrawItemStruct->itemID;
InBlock.gif    CImageList
* pImageList;
InBlock.gif
InBlock.gif    
// Save dc state
InBlock.gif
    int nSavedDC = pDC->SaveDC();
InBlock.gif
InBlock.gif    
// Get item image and state info
InBlock.gif
    LV_ITEM lvi;
InBlock.gif    lvi.mask 
= LVIF_IMAGE | LVIF_STATE;
InBlock.gif    lvi.iItem 
= nItem;
InBlock.gif    lvi.iSubItem 
= 0;
InBlock.gif    lvi.stateMask 
= 0xFFFF;        // get all state flags
InBlock.gif
    GetItem(&lvi);
InBlock.gif
InBlock.gif    
// Should the item be highlighted
InBlock.gif
    BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED)&&((GetFocus() == this)||
InBlock.gif        (GetStyle() 
& LVS_SHOWSELALWAYS))));
InBlock.gif
InBlock.gif    
// Get rectangles for drawing
InBlock.gif
    CRect rcBounds, rcLabel, rcIcon;
InBlock.gif    GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
InBlock.gif    GetItemRect(nItem, rcLabel, LVIR_LABEL);
InBlock.gif    GetItemRect(nItem, rcIcon, LVIR_ICON);
InBlock.gif    CRect rcCol( rcBounds ); 
InBlock.gif
InBlock.gif    CString sLabel 
= GetItemText(nItem, 0 );
InBlock.gif
InBlock.gif    
// Labels are offset by a certain amount  
InBlock.gif    
// This offset is related to the width of a space character
InBlock.gif
    int offset = pDC->GetTextExtent(_T(" "), 1 ).cx*2;
InBlock.gif
InBlock.gif    CRect rcHighlight;
InBlock.gif    CRect rcClient;
InBlock.gif    
int nExt;
InBlock.gif    
switch(m_nHighlight)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
case 0
InBlock.gif        nExt
=pDC->GetOutputTextExtent(sLabel).cx + offset;
InBlock.gif        rcHighlight 
= rcLabel;
InBlock.gif
//        if( rcLabel.left + nExt 
InBlock.gif
            
InBlock.gif        
if( m_bitmap.m_hObject != NULL )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif               CDC tempDC;
InBlock.gif            tempDC.CreateCompatibleDC(pDC);
InBlock.gif            tempDC.SelectObject( 
&m_bitmap );
InBlock.gif
InBlock.gif            GetClientRect(
&rcClient);
InBlock.gif
InBlock.gif            CRgn rgnBitmap;
InBlock.gif            CRect rcTmpBmp( rcItem );
InBlock.gif        
InBlock.gif            rcTmpBmp.right 
= rcClient.right;
InBlock.gif
InBlock.gif            
// We also need to check whether it is the last item
InBlock.gif            
// The update region has to be extended to the bottom if it is
InBlock.gif
            if( nItem == GetItemCount() - 1 )    
InBlock.gif                rcTmpBmp.bottom 
= rcClient.bottom;
InBlock.gif
InBlock.gif            rgnBitmap.CreateRectRgnIndirect(
&rcTmpBmp);
InBlock.gif            pDC
->SelectClipRgn(&rgnBitmap);
InBlock.gif            rgnBitmap.DeleteObject();
InBlock.gif        
InBlock.gif            
if( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_pal.m_hObject != NULL )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pDC
->SelectPalette( &m_pal, FALSE );
InBlock.gif                pDC
->RealizePalette();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            CRect rcFirstItem;
InBlock.gif            GetItemRect(
0, rcFirstItem, LVIR_BOUNDS);
InBlock.gif
InBlock.gif            
for (int i = rcFirstItem.left; i < rcTmpBmp.right; i += m_cxBitmap)
InBlock.gif                
for (int j = rcFirstItem.top; j < rcTmpBmp.bottom; j += m_cyBitmap)
InBlock.gif                    pDC
->BitBlt(i, j, m_cxBitmap, m_cyBitmap, &tempDC, 00, SRCCOPY);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

InBlock.gif    
else if( m_bitmap.m_hObject == NULL )
InBlock.gif        pDC
->FillRect(rcHighlight, &CBrush(::GetSysColor(COLOR_WINDOW)));
InBlock.gif
InBlock.gif    
InBlock.gif
InBlock.gif    
// Set clip region
InBlock.gif
    rcCol.right = rcCol.left + GetColumnWidth(0);
InBlock.gif    CRgn rgn;
InBlock.gif    rgn.CreateRectRgnIndirect(
&rcCol);
InBlock.gif    pDC
->SelectClipRgn(&rgn);
InBlock.gif    rgn.DeleteObject();
InBlock.gif
InBlock.gif    
// Draw state icon
InBlock.gif
    if (lvi.state & LVIS_STATEIMAGEMASK)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
int nImage = ((lvi.state & LVIS_STATEIMAGEMASK)>>12- 1;
InBlock.gif        pImageList 
= GetImageList(LVSIL_STATE);
InBlock.gif        
if (pImageList)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            pImageList
->Draw(pDC, nImage,
InBlock.gif                CPoint(rcCol.left, rcCol.top), ILD_TRANSPARENT);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
// Draw normal and overlay icon
InBlock.gif
    pImageList = GetImageList(LVSIL_SMALL);
InBlock.gif    
if (pImageList)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        UINT nOvlImageMask
=lvi.state & LVIS_OVERLAYMASK;
InBlock.gif        pImageList
->Draw(pDC, lvi.iImage, 
InBlock.gif            CPoint(rcIcon.left, rcIcon.top),
InBlock.gif            (bHighlight
?ILD_BLEND50:0| ILD_TRANSPARENT | nOvlImageMask );
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
InBlock.gif    
InBlock.gif    
// Draw item label - Column 0
InBlock.gif
    rcLabel.left += offset/2;
InBlock.gif    rcLabel.right 
-= offset;
InBlock.gif
InBlock.gif    pDC
->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    
// Draw labels for remaining columns
InBlock.gif
    LV_COLUMN lvc;
InBlock.gif    lvc.mask 
= LVCF_FMT | LVCF_WIDTH;
InBlock.gif
InBlock.gif    
if( m_nHighlight == 0 )        // Highlight only first column
ExpandedSubBlockStart.gifContractedSubBlock.gif
    dot.gif{
InBlock.gif        pDC
->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
InBlock.gif        pDC
->SetBkColor(::GetSysColor(COLOR_WINDOW));
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    rcBounds.right 
= rcHighlight.right > rcBounds.right ? rcHighlight.right :
InBlock.gif                            rcBounds.right;
InBlock.gif    rgn.CreateRectRgnIndirect(
&rcBounds);
InBlock.gif    pDC
->SelectClipRgn(&rgn);
InBlock.gif                   
InBlock.gif    
for(int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        rcCol.left 
= rcCol.right;
InBlock.gif        rcCol.right 
+= lvc.cx;
InBlock.gif
InBlock.gif        
// Draw the background if needed&& m_nHighlight == HIGHLIGHT_NORMAL
InBlock.gif
        if( m_bitmap.m_hObject == NULL  )
InBlock.gif            pDC
->FillRect(rcCol, &CBrush(::GetSysColor(COLOR_WINDOW)));
InBlock.gif
InBlock.gif        sLabel 
= GetItemText(nItem, nColumn);
InBlock.gif        
if (sLabel.GetLength() == 0)
InBlock.gif            
continue;
InBlock.gif
InBlock.gif
InBlock.gif        
// Get the text justification
InBlock.gif
        UINT nJustify = DT_LEFT;
InBlock.gif        
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
case LVCFMT_RIGHT:
InBlock.gif            nJustify 
= DT_RIGHT;
InBlock.gif            
break;
InBlock.gif        
case LVCFMT_CENTER:
InBlock.gif            nJustify 
= DT_CENTER;
InBlock.gif            
break;
InBlock.gif        
default:
InBlock.gif            
break;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif
InBlock.gif    
// Draw focus rectangle if item has focus
InBlock.gif
    if (lvi.state & LVIS_FOCUSED && (GetFocus() == this))
InBlock.gif        pDC
->DrawFocusRect(rcHighlight);
InBlock.gif
InBlock.gif    
InBlock.gif    
// Restore dc
InBlock.gif
    pDC->RestoreDC( nSavedDC );
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
void  CListCtrlEx::AdjustColumnWidth()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    SetRedraw(FALSE);
InBlock.gif    
int nColumnCount = GetColumnCount();
InBlock.gif
InBlock.gif    
for(int i = 0; i < nColumnCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SetColumnWidth(i, LVSCW_AUTOSIZE);
InBlock.gif        
int nColumnWidth = GetColumnWidth(i);
InBlock.gif        SetColumnWidth(i, LVSCW_AUTOSIZE_USEHEADER);
InBlock.gif        
int nHeaderWidth = GetColumnWidth(i);
InBlock.gif
InBlock.gif        SetColumnWidth(i, max(nColumnWidth, nHeaderWidth));
ExpandedSubBlockEnd.gif    }

InBlock.gif    SetRedraw(TRUE);
ExpandedBlockEnd.gif}

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

None.gif
None.gifBOOL CListCtrlEx::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT
*  pResult)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    HD_NOTIFY    
*pHDN = (HD_NOTIFY*)lParam;
InBlock.gif
InBlock.gif    
// This code is for using bitmap in the background
InBlock.gif    
// Invalidate the right side of the control when a column is resized
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.gif            CRect rcClient;
InBlock.gif            GetClientRect( 
&rcClient );
InBlock.gif            DWORD dwPos 
= GetMessagePos();
InBlock.gif            CPoint pt( LOWORD(dwPos), HIWORD(dwPos) );
InBlock.gif            ScreenToClient( 
&pt );
InBlock.gif            rcClient.left 
= pt.x;
InBlock.gif            InvalidateRect( 
&rcClient );
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

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

None.gif

None.gif BOOL CCdDlg::OnInitDialog()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    CDialog::OnInitDialog();
InBlock.gif
InBlock.gif    
// Add "Aboutdot.gif" menu item to system menu.
InBlock.gif
InBlock.gif    
// IDM_ABOUTBOX must be in the system command range.
InBlock.gif
    ASSERT((IDM_ABOUTBOX & 0xFFF0== IDM_ABOUTBOX);
InBlock.gif    ASSERT(IDM_ABOUTBOX 
< 0xF000);
InBlock.gif
InBlock.gif    CMenu
* pSysMenu = GetSystemMenu(FALSE);
InBlock.gif    
if (pSysMenu != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CString strAboutMenu;
InBlock.gif        strAboutMenu.LoadString(IDS_ABOUTBOX);
InBlock.gif        
if (!strAboutMenu.IsEmpty())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            pSysMenu
->AppendMenu(MF_SEPARATOR);
InBlock.gif            pSysMenu
->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Set the icon for this dialog.  The framework does this automatically
InBlock.gif    
//  when the application's main window is not a dialog
InBlock.gif
    SetIcon(m_hIcon, TRUE);            // Set big icon
InBlock.gif
    SetIcon(m_hIcon, FALSE);        // Set small icon
InBlock.gif

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

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

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

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

InBlock.gif
InBlock.gif    
InBlock.gif      
InBlock.gif      ListView_SetExtendedListViewStyle(m_list.m_hWnd,LVS_EX_FULLROWSELECT
|LVS_EX_FLATSB|LVS_EX_HEADERDRAGDROP );    
InBlock.gif    m_list.SetBkImage(IDB_Bless);
InBlock.gif    m_list.AdjustColumnWidth();
InBlock.gif    
return TRUE;  // return TRUE  unless you set the focus to a control
ExpandedBlockEnd.gif
}

效果如图所示:
200781702.jpg
   

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

 

参考资料:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值