title: How to get windows folder display order by C + +
body: <p>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.</p>
  <pre><code>//Find out the current folder window according to the mouse click position
  HWND findOpenFileWindow(const QString&amp; path)
  {
      Sleep(3 * 1000);
      POINT pNow = { 0, 0 };
      if (!GetCursorPos(&amp;pNow))
          return NULL;
  
      TCHAR  szClass[255] = {0};
      HWND pMouseChooseHandle = WindowFromPoint(pNow);
      HWND pParent = ::GetParent(pMouseChooseHandle);
  
      GetClassName(pParent, szClass, 255);
      if (_tcscmp(szClass, L&quot;SHELLDLL_DefView&quot;) == 0 || _tcscmp(szClass, L&quot;NSEViewClass&quot;) == 0 )
      {
          bool bQingDisk = _tcscmp(szClass, L&quot;NSEViewClass&quot;) == 0;
          pParent = ::GetParent(pParent);
          GetClassName(pParent, szClass, 255);
          if (_tcscmp(szClass, L&quot;WorkerW&quot;))
          {
              pParent = pMouseChooseHandle;
              for (int i = 0; i &lt; 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(&quot;ShellTabWindowClass&quot;)) == 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&amp; path, const QStringList&amp; filter )
  {
      HWND folderWnd = findOpenFileWindow(path);
  
      HWND          hwnd;
      IDispatch     *pDispatch;
      CComPtr&lt;IShellWindows&gt; pShellWindows;
  
      CoInitialize(NULL);
      HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_PPV_ARGS(&amp;pShellWindows));
      if (FAILED(hr)) 
      {
          CoUninitialize();
          return QStringList();
      }
      LONG lCount = 0;
      pShellWindows-&gt;get_Count(&amp;lCount);
  
      QStringList fileList;
      for (LONG i = 0; i &lt; lCount; i++) 
      {
          CComPtr&lt;IShellBrowser&gt; pShellBrowser;
          VARIANT  var;
          var.vt = VT_I4;
          var.lVal = i;
          if(SUCCEEDED(pShellWindows-&gt;Item(var, &amp;pDispatch))) 
          {
              if(SUCCEEDED(IUnknown_QueryService(pDispatch, SID_STopLevelBrowser, IID_PPV_ARGS(&amp;pShellBrowser))))
              {
                  if (SUCCEEDED(IUnknown_GetWindow(pShellBrowser, &amp;hwnd)))
                  {
                      TCHAR  szBuf[256];
                      GetWindowText(hwnd, szBuf, sizeof(szBuf) / sizeof(TCHAR));
                      if(QFileInfo(path).fileName() != QString().fromWCharArray(szBuf))
                          continue;
  
                      CComPtr&lt;IShellView&gt; pShellView;
                      if(FAILED(pShellBrowser-&gt;QueryActiveShellView(&amp;pShellView)))
                          continue;
  
                      CComPtr&lt;IFolderView&gt; pFv = NULL;
                      /*
                      do something here
                      */
  
                      CComPtr&lt;IPersistFolder2 &gt; pFolder;
                      if( FAILED(pFv-&gt;GetFolder(IID_IPersistFolder2, (void**) &amp;pFolder)))
                          continue;
  
                      LPITEMIDLIST pidl = nullptr;
                      if( SUCCEEDED(pFolder-&gt;GetCurFolder(&amp;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;
  </code></pre>