如题,网上找不到完整的例子,东一块西一块的,听说好像在DrawItem里面重画没有在onpaint里面重画好?我主要就是想改改表头的背景颜色

解决方案 »

  1.   

    既然你已经问到重绘了,那么技术问题就不多说了,只把OnPaint写给你,不要用DrawItem!void CHeaderCtrlEx::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting

    CRect rect, rectItem, clientRect;
    GetClientRect(&rect);
    GetClientRect(&clientRect);
    CMemDC memDC(&dc, rect);
    CDC bitmapDC;
    bitmapDC.CreateCompatibleDC(&dc);

    memDC.FillSolidRect(&rect, RGB(64,109,176));你的背景颜色
    int nItems = GetItemCount();
    for(int i = 0; i <nItems; i++)
    {

    TCHAR buf1[256];
    HD_ITEM hditem1;

    hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
    hditem1.pszText = buf1;
    hditem1.cchTextMax = 255;
    GetItem( i, &hditem1 );

    GetItemRect(i, &rect);
    DRAWITEMSTRUCT DrawItemStruct;
    GetItemRect(i, &rectItem);


    DrawItemStruct.CtlType = 100;
    DrawItemStruct.hDC = dc.GetSafeHdc();
    DrawItemStruct.itemAction = ODA_DRAWENTIRE; 
    DrawItemStruct.hwndItem  = GetSafeHwnd(); 
    DrawItemStruct.rcItem = rectItem;
    DrawItemStruct.itemID = i;
    DrawItem(&DrawItemStruct);

    UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;


    CFont font;
    LOGFONT lf;
    memset(&lf, 0, sizeof(LOGFONT));
    lf.lfHeight = 12;
    strcpy(lf.lfFaceName, "宋体");
    font.CreateFontIndirect(&lf);
    CFont* def_font = memDC.SelectObject(&font);

    memDC.SetTextColor(RGB(255,255,255));
    memDC.SetBkMode(TRANSPARENT);
    rectItem.DeflateRect(2,2,2,2);

    TCHAR buf[256];
    HD_ITEM hditem;

    hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
    hditem.pszText = buf;
    hditem.cchTextMax = 255;
    GetItem( DrawItemStruct.itemID, &hditem ); memDC.DrawText(buf, &rectItem, uFormat);
    memDC.SelectObject(def_font);
    font.DeleteObject();
    } }为了防止闪烁最好也处理一下WM_ERASEBKGND这样出来的效果仅仅是背景色填充纯色,显示不了网格线,很难重绘得好,我一般是填充背景图来实现网格线效果,底图做得好可以有金属效果
      

  2.   

    额我是想改“表头”(CHEADERCTRL)的背景颜色,这些是改CLISTCTRL的吧?不过也好,应该会用得着,谢谢高手。
      

  3.   

    void CHeaderCtrlEx::OnPaint() 
    这么大的CHeaderCtrlEx啊,晕死!!!!!!!!
      

  4.   

    唉,没看到这么懒的人,既然你不愿意自己动手做,那就给你完整的代码吧,现在的人啊,懒死!//.H File
    // CHeaderCtrlEx
    class CMemDC : public CDC 
    {
    private:
    CBitmap* m_bitmap;
    CBitmap* m_oldBitmap;
    CDC* m_pDC;
    CRect m_rcBounds;
    public:
    CMemDC(CDC* pDC, const CRect& rcBounds) : CDC()
    {
    CreateCompatibleDC(pDC);
    m_bitmap = new CBitmap;
    m_bitmap->CreateCompatibleBitmap(pDC, rcBounds.Width(), rcBounds.Height());
    m_oldBitmap = SelectObject(m_bitmap);
    m_pDC = pDC;
    m_rcBounds = rcBounds;
    }
    ~CMemDC() 
    {
    m_pDC->BitBlt(m_rcBounds.left, m_rcBounds.top, m_rcBounds.Width(), m_rcBounds.Height(), 
    this, m_rcBounds.left, m_rcBounds.top, SRCCOPY);
    SelectObject(m_oldBitmap);
    if (m_bitmap != NULL) 
    delete m_bitmap;
    }
    CMemDC* operator->() 
    {
    return this;
    }
    }; class   CHeaderCtrlEx   :   public   CHeaderCtrl   
      {   
      public:   
      CHeaderCtrlEx();   
      public:   
      virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
     virtual   ~CHeaderCtrlEx();       
    protected:
    //{{AFX_MSG(CHeaderCtrlEx)
    afx_msg void OnPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
        
      };   
    //.cpp File
    // HeaderCtrlEx.cpp : 实现文件
    //#include "stdafx.h"
    #include "HeaderCtrlEx.h"
    #include "resource.h"CHeaderCtrlEx::CHeaderCtrlEx()   
      {   
      }   
        
      CHeaderCtrlEx::~CHeaderCtrlEx()   
      {   
      }   
        
        
      BEGIN_MESSAGE_MAP(CHeaderCtrlEx,   CHeaderCtrl)   
       //{{AFX_MSG_MAP(CHeaderCtrlEx)
    ON_WM_PAINT()
    ON_WM_ERASEBKGND()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()   
        
    void CHeaderCtrlEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
    {}  void CHeaderCtrlEx::OnPaint() 
    {
    CPaintDC dc(this); // device context for painting

    CRect rect, rectItem, clientRect;
    GetClientRect(&rect);
    GetClientRect(&clientRect);
    CMemDC memDC(&dc, rect);
    CDC bitmapDC;
    bitmapDC.CreateCompatibleDC(&dc);

    memDC.FillSolidRect(&rect, RGB(64,109,176));
    /*
    CBitmap bitmapSpan;
    bitmapSpan.LoadBitmap(IDB_COLUMNHEADER_SPAN);
    CBitmap* pOldBitmapSpan = bitmapDC.SelectObject(&bitmapSpan);

    for(int v=0; v<rect.Width(); v++)
    {
    memDC.BitBlt((rect.left+2)+(v*1), 0, 1, 23, &bitmapDC,0,0,SRCCOPY);
    } bitmapDC.SelectObject(pOldBitmapSpan);
    bitmapSpan.DeleteObject();
    */
    int nItems = GetItemCount(); CBitmap bitmap;
    CBitmap bitmap2;
    CBitmap bitmap3;

    bitmap.LoadBitmap(IDB_COLUMNHEADER_START);
    bitmap2.LoadBitmap(IDB_COLUMNHEADER_SPAN);
    bitmap3.LoadBitmap(IDB_COLUMNHEADER_END); for(int i = 0; i <nItems; i++)
    {

    TCHAR buf1[256];
    HD_ITEM hditem1;

    hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
    hditem1.pszText = buf1;
    hditem1.cchTextMax = 255;
    GetItem( i, &hditem1 );

    GetItemRect(i, &rect);

    /*
    CBitmap* pOldBitmap = NULL;

    //make sure we draw the start piece
    //on the first item so it has a left border //For the following items we will just use the
    //right border of the previous items as the left
    //border
    if(hditem1.iOrder==0)
    {
    pOldBitmap = bitmapDC.SelectObject(&bitmap);
    memDC.BitBlt(rect.left,rect.top,2,23,&bitmapDC,0,0,SRCCOPY);
    }
    else
    {
    memDC.BitBlt(rect.left-1,rect.top,2,23,&bitmapDC,0,0,SRCCOPY);
    pOldBitmap = bitmapDC.SelectObject(&bitmap2);
    memDC.BitBlt(rect.left+1,rect.top,1,23,&bitmapDC,0,0,SRCCOPY);
    } bitmapDC.SelectObject(pOldBitmap);

    //span the bitmap for the width of the column header item
    int nWidth = rect.Width() - 4;

    CBitmap* pOldBitmap2 = bitmapDC.SelectObject(&bitmap2);
    for(int v=0; v<nWidth; v++)
    {
    memDC.BitBlt((rect.left+2)+(v*1), 0, 1, 23, &bitmapDC,0,0,SRCCOPY);
    } bitmapDC.SelectObject(pOldBitmap2);


    //draw the end piece of the column header
    CBitmap* pOldBitmap3 = bitmapDC.SelectObject(&bitmap3);
    memDC.BitBlt((rect.right-2), 0, 2, 23, &bitmapDC,0,0,SRCCOPY);
    bitmapDC.SelectObject(pOldBitmap3);
    */

    //Get all the info for the current
    //item so we can draw the text to it
    //in the desired font and style
    DRAWITEMSTRUCT DrawItemStruct;
    GetItemRect(i, &rectItem);


    DrawItemStruct.CtlType = 100;
    DrawItemStruct.hDC = dc.GetSafeHdc();
    DrawItemStruct.itemAction = ODA_DRAWENTIRE; 
    DrawItemStruct.hwndItem  = GetSafeHwnd(); 
    DrawItemStruct.rcItem = rectItem;
    DrawItemStruct.itemID = i;
    DrawItem(&DrawItemStruct);

    UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;


    CFont font;
    LOGFONT lf;
    memset(&lf, 0, sizeof(LOGFONT));
    lf.lfHeight = 12;
    strcpy(lf.lfFaceName, "宋体");
    font.CreateFontIndirect(&lf);
    CFont* def_font = memDC.SelectObject(&font);

    memDC.SetTextColor(RGB(255,255,255));
    memDC.SetBkMode(TRANSPARENT);
    rectItem.DeflateRect(2,2,2,2);

    TCHAR buf[256];
    HD_ITEM hditem;

    hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
    hditem.pszText = buf;
    hditem.cchTextMax = 255;
    GetItem( DrawItemStruct.itemID, &hditem ); memDC.DrawText(buf, &rectItem, uFormat);
    memDC.SelectObject(def_font);
    font.DeleteObject();
    }

        
    BOOL CHeaderCtrlEx::OnEraseBkgnd(CDC* pDC) 
    {
    return false;
    }