我们都知道一个文件夹窗口其实是SysListView32类的控件,我现在已经取得了这个控件的句柄,接下来是要得到文件夹下选中文件的文件名和路径。
int n = SendMessage(hWnd,LVM_GETITEMCOUNT,0,0);
这条语句我取得了窗口中文件的个数,但接下来要怎么得到文件名和路径呢?
我用ListView_GetItem(hWnd,&Item) 好像的不到呢?  
急。。
在线等

解决方案 »

  1.   

    别自己发消息了,用Shell的COM接口吧。。
      

  2.   

    对呀
    这就是shell编程嘛
    为什么非要自己搞
      

  3.   

    问题已经解决了,
    我的处理方法是从SysListView32晚上推了5层得到了打开的文件夹的窗口句柄,
    再从文件夹窗口往下又推了5层得到了地址栏的句柄,
    这样就得到了文件夹的绝对地址。
    再加上先前得到的文件名,就组成了文件绝对路径。
    最后再调用API: DeleteFile 删除掉文件。

    谁能还有什么好方法吗,欢迎发言哦,分给了系统就浪费了
      

  4.   

    找地址栏的方法不靠谱,如果用户设置了不显示地址栏,就无法获得路径了,最好还是用COM的方式,我曾经写过的,在项目里找了半天才找到,贴代码如下:std::wstring ShellBrowserSelection(int type)
    {
    //dprintf("Enter DllMain");
    //Vista: ShellTabWindowClass
    //XP: CabinetWClass
    std::wstring m_currentDirectory;
    std::map<std::wstring, ULONG> m_selectedItems;///< list of items which are selected in the explorer view
    bool m_bFilesSelected; ///< at least one file is selected
    bool m_bFolderSelected; ///< at least one folder is selected std::wstring result; if(type == IS_SHELLBROWSER_SELECTION)
    result = L"false"; //
    //IShellBrowser *sb = (IShellBrowser *)SendMessage(GetParent(GetParent(pCwp->hwnd)), WM_GETISHELLBROWSER,0,0);
    //IShellBrowser *pShellBrowser = (IShellBrowser *)SendMessage(GetForegroundWindow(), WM_GETISHELLBROWSER,0,0); HWND hShellBrowserParent = GetFocus();
    std::wstring strWndClass;
    do
    {
    hShellBrowserParent = GetParent(hShellBrowserParent);
    strWndClass = GetWindowClass(hShellBrowserParent);
    }while(hShellBrowserParent != NULL && strWndClass != L"ShellTabWindowClass" && strWndClass != L"CabinetWClass" && strWndClass != L"ExploreWClass"); if(hShellBrowserParent == NULL)
    return result; try
    {
    IShellBrowser *pShellBrowser = (IShellBrowser *)SendMessage(hShellBrowserParent, WM_GETISHELLBROWSER,0,0);
    if(pShellBrowser != NULL)
    {
    IShellView * pShellView;
    if (SUCCEEDED(pShellBrowser->QueryActiveShellView(&pShellView)))
    {
    IFolderView * pFolderView;
    if (SUCCEEDED(pShellView->QueryInterface(IID_IFolderView, (LPVOID*)&pFolderView)))
    {
    // hooray! we got the IFolderView interface!
    // that means the explorer is active and well :) // but we also need the IShellFolder interface because
    // we need its GetCurFolder() method
    IPersistFolder2 * pPersistFolder;
    if (SUCCEEDED(pFolderView->GetFolder(IID_IPersistFolder2, (LPVOID*)&pPersistFolder)))
    {
    LPITEMIDLIST folderpidl;
    if (SUCCEEDED(pPersistFolder->GetCurFolder(&folderpidl)))
    {
    // we have the current folder
    TCHAR buf[MAX_PATH] = {0};
    // find the path of the folder
    if (SHGetPathFromIDList(folderpidl, buf))
    m_currentDirectory = buf;
    // if m_currentDirectory is empty here, that means
    // the current directory is a virtual path IShellFolder * pShellFolder;
    if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
    {
    // find all selected items
    IEnumIDList * pEnum;
    if (SUCCEEDED(pFolderView->Items(SVGIO_SELECTION, IID_IEnumIDList, (LPVOID*)&pEnum)))
    {
    LPITEMIDLIST pidl;
    WCHAR buf[MAX_PATH] = {0};
    ULONG fetched = 0;
    ULONG attribs = 0;
    do 
    {
    pidl = NULL;
    if (SUCCEEDED(pEnum->Next(1, &pidl, &fetched)))
    {
    if (fetched)
    {
    // the pidl we get here is relative!
    if(type == IS_SHELLBROWSER_SELECTION)
    {
    result = L"true";
    return result;
    } attribs = SFGAO_FILESYSTEM|SFGAO_FOLDER;
    if (SUCCEEDED(pShellFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidl, &attribs)))
    {
    if (attribs & SFGAO_FILESYSTEM)
    {
    // create an absolute pidl with the pidl we got above
    LPITEMIDLIST abspidl = CPidl::Append(folderpidl, pidl);
    if (abspidl)
    {
    if (SHGetPathFromIDList(abspidl, buf))
    {
    m_selectedItems[std::wstring(buf)] = attribs;
    result.append(buf);
    result.append(L"\n");
    }
    CoTaskMemFree(abspidl);
    }
    if (attribs & SFGAO_FOLDER)
    m_bFolderSelected = true;
    else
    m_bFilesSelected = true;
    }
    }
    }
    CoTaskMemFree(pidl);
    }
    } while(fetched);
    pEnum->Release();
    }
    pShellFolder->Release();
    }
    CoTaskMemFree(folderpidl);
    }
    pPersistFolder->Release();
    }
    pFolderView->Release();
    }
    pShellView->Release();
    } //if(m_bFolderSelected || m_bFilesSelected)
    // MessageBoxA(0, "Selected", "", 0); OutputDebugStringW(m_currentDirectory.c_str());
    if(!m_selectedItems.empty())
    OutputDebugStringW(m_selectedItems.begin()->first.c_str());
    } }
    catch (...)
    { } if(type != IS_SHELLBROWSER_SELECTION && !result.empty())
    result.append(L"\n"); return result;
    }
      

  5.   

    IShellBrowser* p_SB = (IShellBrowser*)(m_explorer.SendMessage(WM_USER+7,0,0));
    if (p_SB == NULL)
    {
    return;
    }
    IShellView* p_SV;
    HRESULT hr = p_SB->QueryActiveShellView(&p_SV);
    if (!SUCCEEDED(hr))
    {
    return;
    }
    IFolderView* p_FV;
    hr = p_SV->QueryInterface(IID_IFolderView, (void**)&p_FV);
    if (!SUCCEEDED(hr))
    {
    return;
    }然后通过p_FV->GetSelectionMarkedItem()
    p_FV->Item()得到pidl
    然后用SHGetPathFromIDList得到路径
      

  6.   

    对于你这种办法,一个list只能得到文件名,路径你需要自己记住