我现在想把GIF、JPEG、BMP格式的图片在ListCtrl(ICON风格)中作为图标显示出来。
怎么显示。。

解决方案 »

  1.   

    http://blog.csdn.net/jiangsheng/archive/2003/11/20/3796.aspx
      

  2.   

    LPPICTURE gpPicture;
    // This function loads a file into an IStream.
    void LoadPictureFile(LPCTSTR szFile)
    {
    // open file
    HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    _ASSERTE(INVALID_HANDLE_VALUE != hFile); // get file size
    DWORD dwFileSize = GetFileSize(hFile, NULL);
    _ASSERTE(-1 != dwFileSize); LPVOID pvData = NULL;
    // alloc memory based on file size
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
    _ASSERTE(NULL != hGlobal); pvData = GlobalLock(hGlobal);
    _ASSERTE(NULL != pvData); DWORD dwBytesRead = 0;
    // read file and store in global memory
    BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
    _ASSERTE(FALSE != bRead);
    GlobalUnlock(hGlobal);
    CloseHandle(hFile); LPSTREAM pstm = NULL;
    // create IStream* from global memory
    HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
    _ASSERTE(SUCCEEDED(hr) && pstm); // Create IPicture from image file
    if (gpPicture)
    gpPicture->Release();
    hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
    _ASSERTE(SUCCEEDED(hr) && gpPicture);
    pstm->Release(); InvalidateRect(ghWnd, NULL, TRUE);
    }
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    if (gpPicture)
    {
    // get width and height of picture
    long hmWidth;
    long hmHeight;
    gpPicture->get_Width(&hmWidth);
    gpPicture->get_Height(&hmHeight);
    // convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH);
    RECT rc;
    GetClientRect(hWnd, &rc);
    // display picture using IPicture::Render
    gpPicture->Render(hdc, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, &rc);
    }
    EndPaint(hWnd, &ps);
    break;
      

  3.   

    现在可以在PICTURE控件上显示GIF、JPG、BMP图片没问题。我看了jiangsheng(蒋晟.MSMVP2004Jan) 的文章,但是在LISTCTRL(ICON)中用GIF、JPG图片显示在ICON位置还是不会,还请指教。。
      

  4.   

    我用的就是IPICTURE,但是在listctrl中某一ITEM占的矩形区域绘图,没有成功。
    我是这样用的。
    C**::OnPaint()
    {
       pPicutre->render(..)
    }
    但是没有成功绘出来,显示的还是原来的ICON图标
      

  5.   

    To Mackz(在相互):我是这样写的,能否帮我看看到底哪里有问题,不甚感激
    我选中了ListCtrl(ICON风格)中Ownern Draw fixed属性,下面是在第一个ICON
    位置显示图片,但没有成功,帮我看看
    C**::OninitDialog()
    {
    m_ImageList.Create(64,64,ILC_COLOR32,10,30);
    m_list.SetImageList(&m_ImageList,LVSIL_NORMAL);
    HICON hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_ImageList.Add(hIcon);
    m_list.InsertItem(0, "HELLO", 0);
          //加载图片,省略
    }
    C**:: OnPaint()
    {
                    CPaintDC dc(this);
    long hmWidth;
    long hmHeight;
    pPicture->get_Width(&hmWidth);
      pPicture->get_Height(&hmHeight);
    #define HIMETRIC_INCH 2540
    // convert himetric to pixels
    int nWidth = MulDiv(hmWidth, GetDeviceCaps(dc, LOGPIXELSX), HIMETRIC_INCH);
    int nHeight = MulDiv(hmHeight, GetDeviceCaps(dc, LOGPIXELSY), HIMETRIC_INCH);
    CRect rect2; CRect rc;
    CRect rect1;
    GetClientRect(&rect1); POINT  point2;
    point2.x = rect1.left;
    point2.y = rect1.top; CRect rcImage(CPoint(0,0),CPoint(hmWidth, hmHeight));
    rc.IntersectRect(&rcImage, &rect2);
    CPoint point; ::ClientToScreen(this->m_hWnd, &point2);
    ::ClientToScreen(m_Pic2.m_hWnd, &point1);

    CRect rect3;
    m_list.GetItemRect(0, &rect3, LVIR_ICON);
    POINT point3;
    point3.x = rect3.left;
    point3.y = rect3.top;
    ::ClientToScreen(m_list.m_hWnd, &point3);
    rc.IntersectRect(&rcImage, &rect3);
    pPicture->Render(dc, point3.x-point2.x, point3.y-point2.y, rc.Width(), (long)rc.Height(), 0, hmHeight, hmWidth, -hmHeight, &rc);
    }