我要在CListCtrl上创建自画的位图(缩略图),如下代码总是创建出黑色的图,各位帮看看有什么问题?         m_img_list.Create(50,50,ILC_COLOR,20, 20); //创建CImageList

CBitmap * bmp = new CBitmap;//新位图 HDC hdc = ::GetDC(NULL); //得到DC CDC *dc = CDC::FromHandle(hdc);//得到CDC
CDC memdc;//内存DC memdc.CreateCompatibleDC(dc);//创建兼容DC
bmp->CreateCompatibleBitmap(dc,50,50); //创建兼容位图

CBitmap * oldbmp = memdc.SelectObject(bmp);  memdc.SetBkColor(0xFFFFFF);
memdc.FillSolidRect(0,0,50,50,0x0000ff);
memdc.LineTo(100,100); //画线

m_img_list.Add(bmp,RGB(0,255,0)); //加入

m_list_ctrl.SetImageList(&m_img_list,LVSIL_NORMAL); m_list_ctrl.InsertItem(1,"直线",0); memdc.SelectObject(oldbmp);
::ReleaseDC(NULL,hdc);  正确结果应该是有一个画直线的项目, 但现在是一个黑色的框。
  问题出在哪里?

解决方案 »

  1.   

    非常感谢 badknife(断刀) 的帮助:不过。。
     
    我修改过后还是画出线来,图表只是白色的区域
    哪里还有问题??迷茫以下是修改过后的代码:m_img_list.Create(50,50,ILC_COLOR,20, 20);  BITMAPINFOHEADER bi;
    DWORD*  pBits = NULL;
    bi.biSize = sizeof(bi);
    bi.biPlanes = 1;
    bi.biBitCount = 24;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biWidth = 50;
    bi.biHeight = 50;
    bi.biXPelsPerMeter = 0; 
    bi.biYPelsPerMeter = 0; 
    bi.biClrUsed = 0; 
    bi.biClrImportant = 0;  HBITMAP hBitmap;
    HBITMAP hOldBitmap;
    HDC hdc = ::GetDC(NULL); //得到DC
    CDC *dc = CDC::FromHandle(hdc);//得到CDC
    CDC memdc;//内存DC memdc.CreateCompatibleDC(dc); 
    hBitmap = CreateDIBSection(hdc,(BITMAPINFO*)&bi, 0, (void**)&pBits,NULL, 0);
    hOldBitmap =(HBITMAP)memdc.SelectObject(hBitmap);
    memdc.SetBkColor(0xFFFF00);
    memdc.FillSolidRect(0,0,50,50,0xff00ff);
    memdc.LineTo(100,100); //画线

    CBitmap * cmap = new CBitmap;
    cmap->FromHandle(hBitmap); m_img_list.Add(cmap,RGB(0,255,0)); //加入
    m_list_ctrl.SetImageList(&m_img_list,LVSIL_NORMAL);
    m_list_ctrl.InsertItem(0,"直线",0);

    memdc.SelectObject(hOldBitmap);
    DeleteObject(hBitmap);
    ::ReleaseDC(NULL,hdc);
      

  2.   

    非常感谢  badknife(断刀)  的帮助:  
     
    不过还是不行。。  
       
    我修改过后还是画不出线来,图表只是白色的区域  哪里还有问题??迷茫  
     
    以下是修改过后的代码:  
     
    m_img_list.Create(50,50,ILC_COLOR,20,  20);    
     
               BITMAPINFOHEADER  bi;  
               DWORD*    pBits  =  NULL;  
               bi.biSize  =  sizeof(bi);  
               bi.biPlanes  =  1;  
               bi.biBitCount  =  24;  
               bi.biCompression  =  BI_RGB;  
               bi.biSizeImage  =  0;  
               bi.biWidth  =  50;  
               bi.biHeight  =  50;  
               bi.biXPelsPerMeter  =  0;    
               bi.biYPelsPerMeter  =  0;    
               bi.biClrUsed  =  0;    
               bi.biClrImportant  =  0;    
     
               HBITMAP  hBitmap;  
               HBITMAP  hOldBitmap;  
               HDC  hdc  =  ::GetDC(NULL);  //得到DC  
               CDC  *dc  =  CDC::FromHandle(hdc);//得到CDC  
               CDC  memdc;//内存DC  
     
               memdc.CreateCompatibleDC(dc);    
               hBitmap  =  CreateDIBSection(hdc,(BITMAPINFO*)&bi,  0,  (void**)&pBits,NULL,  0);  
               hOldBitmap  =(HBITMAP)memdc.SelectObject(hBitmap);  
               memdc.SetBkColor(0xFFFF00);  
               memdc.FillSolidRect(0,0,50,50,0xff00ff);  
               memdc.LineTo(100,100);  //画线  
                 
               CBitmap  *  cmap  =  new  CBitmap;  
               cmap-  >FromHandle(hBitmap);  
     
               m_img_list.Add(cmap,RGB(0,255,0));  //加入              
               m_list_ctrl.SetImageList(&m_img_list,LVSIL_NORMAL);  
               m_list_ctrl.InsertItem(0,  "直线  ",0);              
                 
               memdc.SelectObject(hOldBitmap);  
               DeleteObject(hBitmap);  
               ::ReleaseDC(NULL,hdc);
      

  3.   

    我这里从LISTCTL中派生了一个类
    typedef struct
    {
    UINT nImageID;
    char* strText;
    }CONTENT_ITEMS,*pCONTENT_ITEMS;
    /////////////////////////////////////////////////////////////////////////////
    // CContentMenu windowclass CContentMenu : public CListBox
    {
    // Construction
    public:
    CContentMenu();// Attributes
    public:// Operations
    public:
    void SetItems(pCONTENT_ITEMS pItems,int nNumItems)
    {
    m_pContents=pItems;
    m_nNumItems=nNumItems;
    for(int i=0;i<nNumItems;i++)
    AddString("");
    }// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CContentMenu)
    public:
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CContentMenu();
    protected:
    bool m_bHilight;
    bool m_bLBDown;
    pCONTENT_ITEMS m_pContents;
    int m_nNumItems;
    CPoint m_Point;
    // Generated message map functions
    protected:
    //{{AFX_MSG(CContentMenu)
    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 void OnTimer(UINT nIDEvent);
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    };
    整个自画函数是这样的:
    void CContentMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
    {
    // TODO: Add your code to draw the specified item
    CDC*            pDC = CDC::FromHandle(lpDIS->hDC);
        CRect           rcItem(lpDIS->rcItem);
        CRect           rClient(rcItem);
        CRect           rText;
        CPoint          Pt(((rcItem.Width() - 32) /2 ), rcItem.top + 15);
        pCONTENT_ITEMS  pContents    = &m_pContents[lpDIS->itemID];
        HICON           hIcon = AfxGetApp()->LoadIcon(pContents->nImageID);
    CDC memDC[4];
    CBitmap MyButtonBK[4];

    MyButtonBK[0].LoadBitmap(IDB_BUTTON1);
    memDC[0].CreateCompatibleDC(pDC);
    CBitmap* pOldBmp=memDC[0].SelectObject(&MyButtonBK[0]);

    MyButtonBK[1].LoadBitmap(IDB_BUTTON2);
    memDC[1].CreateCompatibleDC(pDC);
    CBitmap* pOldBmp1=memDC[1].SelectObject(&MyButtonBK[1]); MyButtonBK[2].LoadBitmap(IDB_BUTTON3);
    memDC[2].CreateCompatibleDC(pDC);
    CBitmap* pOldBmp2=memDC[2].SelectObject(&MyButtonBK[2]); MyButtonBK[3].LoadBitmap(IDB_BUTTON4);
    memDC[3].CreateCompatibleDC(pDC);
    CBitmap* pOldBmp3=memDC[3].SelectObject(&MyButtonBK[3]);    rClient.DeflateRect(2,5);
    rcItem.CopyRect(rClient);
        rText.CopyRect(rClient);
        rText.top += 45;    pDC->SetBkMode(TRANSPARENT);
    if (lpDIS->itemAction & ODA_DRAWENTIRE)
        {
            pDC->DrawIcon(Pt, hIcon);
    pDC->SetTextColor(RGB(24,255,3));
            pDC->DrawText(pContents->strText, rText, DT_CENTER|DT_WORDBREAK);
        }    if  (m_bLBDown)
        {
            Pt.x += 2;
            Pt.y += 2;
            rcItem.OffsetRect(2,2);
            rText.OffsetRect(2,2);
        } if ((lpDIS->itemState & ODS_SELECTED) &&
    (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
        {
            if  (m_bHilight)
            {
                pDC->FillSolidRect(rClient,RGB(255,255,255));            if  (m_bLBDown)
                    pDC->Draw3dRect(rClient,RGB(3,38,3), 
    RGB(36,94,34));
                else
                    pDC->Draw3dRect(rClient,GetSysColor(COLOR_BTNHIGHLIGHT),
    GetSysColor(COLOR_3DSHADOW));        }
    switch(GetCurSel())
    {
    case 0:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[0],0,0,SRCCOPY);
    break;
    case 1:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[1],0,0,SRCCOPY);
    break;
    case 2:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[2],0,0,SRCCOPY);
    break;
    case 3:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[3],0,0,SRCCOPY);
    break;
    }
            pDC->DrawIcon(Pt, hIcon);
    pDC->SetTextColor(RGB(24,255,3));
            pDC->DrawText(pContents->strText, rText, DT_CENTER|DT_WORDBREAK);
        } if (!(lpDIS->itemState & ODS_SELECTED) &&
    (lpDIS->itemAction & ODA_SELECT))
        {
            pDC->FillSolidRect(rClient,RGB(24,255,3));
    switch(GetCurSel())
    {
    case 0:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[0],0,0,SRCCOPY);
    break;
    case 1:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[1],0,0,SRCCOPY);
    break;
    case 2:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[2],0,0,SRCCOPY);
    break;
    case 3:
    pDC->BitBlt(rcItem.left,rcItem.top,rcItem.right,rcItem.bottom,
    &memDC[3],0,0,SRCCOPY);
    break;
    }
    pDC->DrawIcon(Pt, hIcon);
    pDC->SetTextColor(RGB(24,255,3));
            pDC->DrawText(pContents->strText, rText, DT_CENTER|DT_WORDBREAK);
        }}
      

  4.   

    CBitmap 是动态创建的,不能调资源。