mercredi 9 décembre 2020

How to get windows folder display order by C + +

I need to get the pictures in the windows folder and sort them according to the order in which they are displayed. Now there is a method to traverse the entire folder display items through the handle by getting the folder window handle. However, this method has a drawback: it can't get the order of the unopened folder, because there is no open file and no window handle. Qt is used. Please forgive my grammatical mistakes.

//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;

Aucun commentaire:

Enregistrer un commentaire