首页 > 解决方案 > C++如何获取windows文件夹显示顺序

问题描述

我需要获取 windows 文件夹中的图片,并根据它们的显示顺序对其进行排序。现在有一种方法,通过获取文件夹窗口句柄,通过句柄遍历整个文件夹显示项。但是,这种方法有个缺点:无法获取未打开文件夹的顺序,因为没有打开的文件,也没有窗口句柄。使用 Qt。请原谅我的语法错误。

//Find out the current folder window according to the mouse click position
HWND findOpenFileWindow(const QString& path)
{
    Sleep(3 * 1000);
    POINT pNow = { 0, 0 };
    if (!GetCursorPos(&pNow))
        return NULL;

    TCHAR  szClass[255] = {0};
    HWND pMouseChooseHandle = WindowFromPoint(pNow);
    HWND pParent = ::GetParent(pMouseChooseHandle);

    GetClassName(pParent, szClass, 255);
    if (_tcscmp(szClass, L"SHELLDLL_DefView") == 0 || _tcscmp(szClass, L"NSEViewClass") == 0 )
    {
        bool bQingDisk = _tcscmp(szClass, L"NSEViewClass") == 0;
        pParent = ::GetParent(pParent);
        GetClassName(pParent, szClass, 255);
        if (_tcscmp(szClass, L"WorkerW"))
        {
            pParent = pMouseChooseHandle;
            for (int i = 0; i < 6; i++)
            {
                if(pParent != NULL)
                    pParent = ::GetParent(pParent);
            }
            HWND pChild = ::GetWindow(pParent, GW_CHILD);
            GetClassName(pChild, szClass, 255);
            while (pChild != NULL)
            {
                GetClassName(pChild, szClass, 255);
                if (_tcscmp(szClass, TEXT("ShellTabWindowClass")) == 0)
                {
                    pParent = pChild;
                    break;
                }
                pChild = ::GetNextWindow(pChild, GW_HWNDNEXT);
            }
            TCHAR  exploreWndName[MAX_PATH] = {0};
            ::GetWindowText(pParent, exploreWndName, MAX_PATH);
            if(QFileInfo(path).fileName() == QString().fromWCharArray(exploreWndName))
                return pParent;
        }
    }
    return NULL;
}

//Traverse window display items, get them
QStringList listNormalFolderFile( const QString& path, const QStringList& filter )
{
    HWND folderWnd = findOpenFileWindow(path);

    HWND          hwnd;
    IDispatch     *pDispatch;
    CComPtr<IShellWindows> pShellWindows;

    CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pShellWindows));
    if (FAILED(hr)) 
    {
        CoUninitialize();
        return QStringList();
    }
    LONG lCount = 0;
    pShellWindows->get_Count(&lCount);

    QStringList fileList;
    for (LONG i = 0; i < lCount; i++) 
    {
        CComPtr<IShellBrowser> pShellBrowser;
        VARIANT  var;
        var.vt = VT_I4;
        var.lVal = i;
        if(SUCCEEDED(pShellWindows->Item(var, &pDispatch))) 
        {
            if(SUCCEEDED(IUnknown_QueryService(pDispatch, SID_STopLevelBrowser, IID_PPV_ARGS(&pShellBrowser))))
            {
                if (SUCCEEDED(IUnknown_GetWindow(pShellBrowser, &hwnd)))
                {
                    TCHAR  szBuf[256];
                    GetWindowText(hwnd, szBuf, sizeof(szBuf) / sizeof(TCHAR));
                    if(QFileInfo(path).fileName() != QString().fromWCharArray(szBuf))
                        continue;

                    CComPtr<IShellView> pShellView;
                    if(FAILED(pShellBrowser->QueryActiveShellView(&pShellView)))
                        continue;

                    CComPtr<IFolderView> pFv = NULL;
                    /*
                    do something here
                    */

                    CComPtr<IPersistFolder2 > pFolder;
                    if( FAILED(pFv->GetFolder(IID_IPersistFolder2, (void**) &pFolder)))
                        continue;

                    LPITEMIDLIST pidl = nullptr;
                    if( SUCCEEDED(pFolder->GetCurFolder(&pidl)))
                    {
                        wchar_t cPath[32767];
                        if( ::SHGetPathFromIDList(pidl, cPath))
                        {
                            QString filePath;
                            QFileInfo fileInfo(filePath.fromWCharArray(cPath));
                            if(fileInfo.absoluteFilePath() == QFileInfo(path).absoluteFilePath())
                            {
                                if(folderWnd == NULL || folderWnd == hwnd)
                                {
                                    fileList = listFileInBrowser(pShellBrowser, filter);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    CoUninitialize();
    return fileList;

标签: windowsqtc++11winapidirectory

解决方案


推荐阅读