Esempio n. 1
0
HRESULT CMainWindow::FindImages()
{
    HRESULT hr = S_OK;
    
    if (m_thumbs == NULL)
    {
        // Walk the Pictures library

        IShellItem *pShellItemPicturesLibrary;
        hr = SHGetKnownFolderItem(
            FOLDERID_PicturesLibrary,
            KF_FLAG_CREATE,
            NULL,
            IID_PPV_ARGS(&pShellItemPicturesLibrary)
            );
        if (SUCCEEDED(hr))
        {
            INamespaceWalk *pNamespaceWalk;
            hr = CoCreateInstance(
                CLSID_NamespaceWalker,
                NULL,
                CLSCTX_INPROC,
                IID_PPV_ARGS(&pNamespaceWalk)
                );
            if (SUCCEEDED(hr))
            {
                hr = pNamespaceWalk->Walk(
                    pShellItemPicturesLibrary,
                    NSWF_NONE_IMPLIES_ALL,
                    1,
                    NULL
                    );
                if (SUCCEEDED(hr))
                {
                    // Retrieve the array of PIDLs gathered in the walk
                
                    UINT itemCount;
                    PIDLIST_ABSOLUTE *ppidls;
                    hr = pNamespaceWalk->GetIDArrayResult(
                        &itemCount,
                        &ppidls
                        );
                    if (SUCCEEDED(hr))
                    {
                        // Create the uninitialized thumbnails
                        m_thumbs = new CThumbnail[itemCount];                 
                    
                        // Get the bitmap for each item and initialize the corresponding thumbnail object
                    
                        for (UINT i = 0; i < itemCount; i++)
                        {
                            IShellItem *pShellItem;
                            hr = SHCreateItemFromIDList(
                                ppidls[i],
                                IID_PPV_ARGS(&pShellItem)
                                );
                            if (SUCCEEDED(hr))
                            {
                                ID2D1Bitmap *pBitmap;
                                hr = DecodeImageFromThumbCache(
                                    pShellItem,
                                    &pBitmap
                                    );
                                if (SUCCEEDED(hr))
                                {
                                    hr = m_thumbs[m_uThumbCount].Initialize(
                                        pBitmap,
                                        m_pAnimationManager,
                                        m_pRenderTarget->GetSize().width * 0.5,
                                        m_pRenderTarget->GetSize().height * 0.5
                                        );
                                    if (SUCCEEDED(hr))
                                    {
                                        m_uThumbCount++;
                                    }
                                        
                                    pBitmap->Release();
                                }
                                
                                pShellItem->Release();
                            }
                        }
                        
                        // The calling function is responsible for freeing the PIDL array
                        FreeIDListArray(ppidls, itemCount);
                        
                        if (SUCCEEDED(hr))
                        {
                            // Arrange the images when they are first loaded

                            m_pLayoutManager = new CLayoutManager();
                            hr = m_pLayoutManager->Initialize(
                                m_pAnimationManager,
                                m_pAnimationTimer,
                                m_pTransitionLibrary,
                                m_uThumbCount,
                                m_thumbs
                                );
                            if (SUCCEEDED(hr))
                            {
                                hr = m_pLayoutManager->Resize(
                                    m_pRenderTarget->GetSize()
                                    );
                            }
                        }
                    }
                }
            
                pNamespaceWalk->Release();
            }
        
            pShellItemPicturesLibrary->Release();
        }
    }
    
    return hr;
}
static HRESULT GetScaledBitmap(LPWSTR pszFilename,
                               HBITMAP &hBitmap,
                               int cXY) {
    HRESULT hr = S_OK;

    // Walk the Pictures library
    IShellItem* pShellItem;

    hr = SHCreateItemFromParsingName(pszFilename,
                                     NULL,
                                     IID_PPV_ARGS(&pShellItem));

    if (SUCCEEDED(hr)) {
        INamespaceWalk* pNamespaceWalk;
        hr = CoCreateInstance(CLSID_NamespaceWalker,
                              NULL,
                              CLSCTX_INPROC,
                              IID_PPV_ARGS(&pNamespaceWalk));

        if (SUCCEEDED(hr)) {
            hr = pNamespaceWalk->Walk(pShellItem,
                                      NSWF_NONE_IMPLIES_ALL,
                                      1,
                                      NULL);
            if (SUCCEEDED(hr)) {
                // Retrieve the array of PIDLs gathered in the walk
                UINT itemCount;
                PIDLIST_ABSOLUTE* ppidls;
                hr = pNamespaceWalk->GetIDArrayResult(&itemCount, &ppidls);
                if (SUCCEEDED(hr)) {
                    // Create the uninitialized thumbnails
                    // Get the bitmap for each item and initialize the corresponding thumbnail object
                    for (UINT i = 0; i < itemCount; i++) {
                        IShellItem* pShellItem;
                        hr = SHCreateItemFromIDList(ppidls[i], IID_PPV_ARGS(&pShellItem));
                        if (SUCCEEDED(hr)) {
                            hr = DecodeImageFromThumbCache(pShellItem, hBitmap, cXY);
                            pShellItem->Release();
                        }
                    }

                    // The calling function is responsible for freeing the PIDL array
                    FreeIDListArray(ppidls, itemCount);
                } else {
                    LOG_ERROR("Failed to walk to get the array of PIDLs, result = %d", hr);
                }
            } else {
                LOG_ERROR("Failed to walk to the shell item, result = %d", hr);
            }
            pNamespaceWalk->Release();
        } else {
            LOG_ERROR("Failed to create instance of INamespaceWalk, result = %d", hr);
        }

        pShellItem->Release();
    } else {
        LOG_ERROR("Failed to create instance of IShellItem, result = %d", hr);
    }

    return hr;
}