http://www.codeguru.com/listbox/index.shtml

解决方案 »

  1.   

    class COwnerListColor : public CListCtrl
    {
    // Construction
    BOOL m_bFocus;
    public:
    COwnerListColor();
    CArray<float,float>* m_fpValuePoint;
    INTARRAY* m_ipColor; virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
    void FillList();
    void AddPoint();
    void DeletePoint();
    COwnerSelList m_SelColorList;
    // Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(COwnerListColor)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~COwnerListColor(); // Generated message map functions
    protected:
    //{{AFX_MSG(COwnerListColor)
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
    afx_msg void OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnSetFocus(CWnd* pOldWnd);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
      

  2.   

    COwnerListColor::COwnerListColor()
    {
    m_bFocus = FALSE;
    }COwnerListColor::~COwnerListColor()
    {}BEGIN_MESSAGE_MAP(COwnerListColor, CListCtrl)
    //{{AFX_MSG_MAP(COwnerListColor)
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONDBLCLK()
    ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndlabeledit)
    ON_WM_KILLFOCUS()
    ON_WM_SETFOCUS()
    ON_WM_MEASUREITEM()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
      
    void COwnerListColor::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
    {
    CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect rectClient = lpDrawItemStruct->rcItem;
    int iID = lpDrawItemStruct->itemID;
    ASSERT(pDC);
    if (!pDC) return ; int iWidth1 = GetColumnWidth(0);
    CRect rect = rectClient;
    rect.right = rect.left + iWidth1;
    rect.DeflateRect(2, 2); // 铺 底 色
    if ((lpDrawItemStruct->itemState & ODS_SELECTED))
    {
    pDC->FillSolidRect(&rect, 0x800000);
    pDC->SetTextColor(0xffffff);
    rect.InflateRect(1,1);
    //if (GetItemState(iID, LVIS_FOCUSED))
    if (m_bFocus)
    pDC->DrawFocusRect(&rect);
    }
    else
    {
    rect.InflateRect(1,1);
    pDC->FillSolidRect(&rect, 0xffffff);
    } // 第 一 列
    rect.left += 8; 
    CString strValue;// = GetItemText(lpDrawItemStruct->itemID,0);
    strValue.Format(_T("%g"), (double)(*m_fpValuePoint)[iID]);
    pDC->DrawText(strValue, &rect, DT_LEFT | DT_SINGLELINE | DT_VCENTER);
    pDC->SetTextColor(0x0); // 第 二 列 ,  字 色
    int iWidth2 = GetColumnWidth(1);
    rect = rectClient;
    rect.left += (iWidth1+4);
    rect.right = rect.left + iWidth2 - 6;
    rect.top += 2;
    rect.bottom -= 2;

    LOGBRUSH logbrush;
    logbrush.lbStyle = BS_SOLID;
    logbrush.lbHatch = HS_HORIZONTAL;
    logbrush.lbColor = (*m_ipColor)[iID];
    CBrush brush;
    if (brush.CreateBrushIndirect(&logbrush))
    pDC->FillRect(&rect, &brush);
    brush.DeleteObject();
    pDC->DrawEdge(&rect, EDGE_BUMP, BF_RECT);
    }void COwnerListColor::FillList()
    {
    int iActualItem;
    LV_ITEM lvitem; DeleteAllItems(); for (int i=0;i<(*m_fpValuePoint).GetSize();i++)
    {
    lvitem.mask = LVIF_TEXT ;
    lvitem.iItem = i;
    lvitem.iSubItem = 0;
    CString strValue;
    strValue.Format(_T("%g"), (double)(*m_fpValuePoint)[i]);
    lvitem.pszText = (LPSTR)(LPCTSTR)strValue;
    iActualItem = InsertItem(&lvitem);

    lvitem.iItem = iActualItem;
    lvitem.iSubItem = 1;
    lvitem.pszText = NULL;
    SetItem(&lvitem); lvitem.iItem = iActualItem;
    lvitem.iSubItem = 2;
    lvitem.pszText = NULL;
    SetItem(&lvitem);
    }
    }
      

  3.   

    请把代码发到我的信箱。
    [email protected]
    收到急送分
      

  4.   

    已经发了,上边的代码少了一句#define INTARRAY CArray<int,int>
      

  5.   

    不对呀!你给的代码是从CListCtrl派生的
    我要的是从CListBox派生的
      

  6.   

    COwnerListColor m_ctlListColor;添加一个按钮,作如下子类化操作,
    BOOL CDrawDlg::OnInitDialog() 
    {
    CDialog::OnInitDialog();

    //初始化构件背景颜色按钮
    m_ctlListColor.SubclassDlgItem( IDC_BUTTON1 , this ) ; return TRUE;  
    }这样按钮就成了颜色选择列表了这样不行我就没办法了!上边的代码不完整,我发给你的邮件中的是完整的
      

  7.   

    我有呀!
    #if !defined(AFX_LISTEX_H__3A5DCC58_5B9A_49AC_9CB7_6A80C864360A__INCLUDED_)
    #define AFX_LISTEX_H__3A5DCC58_5B9A_49AC_9CB7_6A80C864360A__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    // ListEx.h : header file
    ///////////////////////////////////////////////////////////////////////////////
    // CListEx windowclass CListEx : public CListBox
    {
    // Construction
    public:
    CListEx();
    COLORREF m_clrFor,m_clrBack;
    CBrush m_brush;
    CBitmap bmp;
    // Attributes
    public:// Operations
    public:// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CListEx)
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CListEx();
    CBitmap m_bitmap;
    bool m_mousedown;
    // Generated message map functions
    protected:
    //{{AFX_MSG(CListEx)
    afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    afx_msg void OnPaint();
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
    afx_msg UINT OnGetDlgCode();
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_LISTEX_H__3A5DCC58_5B9A_49AC_9CB7_6A80C864360A__INCLUDED_)// ListEx.cpp : implementation file
    //#include "stdafx.h"
    #include "13.h"
    #include "ListEx.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CListExCListEx::CListEx()
    {
    m_mousedown=false;
    m_clrFor=RGB(255,0,0);
    m_clrBack=RGB(0,255,0);
    bmp.LoadBitmap(IDB_BITMAP); ///加载位图
        m_brush.CreatePatternBrush(&bmp); ///创建位图画刷}CListEx::~CListEx()
    {
    }
    BEGIN_MESSAGE_MAP(CListEx, CListBox)
    //{{AFX_MSG_MAP(CListEx)
    ON_WM_CTLCOLOR_REFLECT()
    ON_WM_PAINT()
    ON_WM_ERASEBKGND()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_MOUSEMOVE()
    ON_WM_GETDLGCODE()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CListEx message handlersHBRUSH CListEx::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
    pDC->SetTextColor(this->m_clrFor);//改变字体的颜色
    pDC->SetBkMode(TRANSPARENT); //设置背景透明,这样,输出字符的时候就
    //是所谓的空心字,而不是有白的底色
     
    return HBRUSH(GetStockObject(HOLLOW_BRUSH));}void CListEx::OnPaint() 
    {
    }
    BOOL CListEx::OnEraseBkgnd(CDC* pDC) 
    {
    RECT updatarect; 
    GetClientRect(&updatarect);
    CBrush * oldBrush = pDC->SelectObject(&m_brush);
    pDC->Rectangle(&updatarect);
    pDC->SelectObject(oldBrush);
    return TRUE;
    }void CListEx::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    m_mousedown = TRUE;
    SetCapture();

    CListBox::OnLButtonDown(nFlags, point);
    }void CListEx::OnLButtonUp(UINT nFlags, CPoint point) 
    {
    if(m_mousedown) 
    ReleaseCapture();
    m_mousedown = FALSE ;
    CListBox::OnLButtonUp(nFlags, point);
    }void CListEx::OnMouseMove(UINT nFlags, CPoint point) 
    {
    if(m_mousedown)
    RedrawWindow(NULL, NULL,RDW_INVALIDATE | RDW_ERASE );
    CListBox::OnMouseMove(nFlags, point);
    }UINT CListEx::OnGetDlgCode() 
    {
    RedrawWindow(NULL, NULL,RDW_INVALIDATE | RDW_ERASE ); 

    return CListBox::OnGetDlgCode();
    }