虚拟文件夹在Windows 7

时间:2017-11-02 23:47:41

标签: c++ windows registry shell-extensions shell-namespace-extension

------------------------- update 5th -------------------- -----------

            if (fIsFolder)
            {
                dwAttribs |= SFGAO_FOLDER;
            }
            else
            {
                dwAttribs |= SFGAO_SYSTEM;

                dwAttribs |= SFGAO_FILESYSTEM;
            }

            if (nLevel < g_nMaxLevel)
            {
                dwAttribs |= SFGAO_HASSUBFOLDER;

                dwAttribs |= SFGAO_FILESYSANCESTOR;
            }

现在这个在应用程序A,stackoverflow图像上传文件对话框和常规文件夹资源管理器中正常工作。

----------------------------------------------- ------更新第4 ------------------------------------------ ----------------- 添加dwAttribs |= SFGAO_SYSTEM;后,至少对于应用程序A,它在文件对话框中按预期工作。但该文件夹仍然无法在常规文件夹对话框或CWFileDialog中单击。

            if (fIsFolder)
            {
                dwAttribs |= SFGAO_FOLDER;

                dwAttribs |= SFGAO_FILESYSTEM;

                dwAttribs |= SFGAO_FILESYSANCESTOR;
            }
            else
            {
                dwAttribs |= SFGAO_SYSTEM;// this line of code works better

                dwAttribs |= SFGAO_FILESYSTEM;
            }

-----------------------------------更新第三---------- ------------------------------- 这是虚拟文件夹的ISHellFolder:

中的代码

编辑以下代码以添加dwAttribs |= SFGAO_FILESYSTEM;后,当我在文件夹视图中双击该文件夹时,无法浏览该文件夹。但是可以通过在树视图中左键单击它来打开它。

 HRESULT CFolderViewImplFolder::GetAttributesOf(UINT cidl, PCUITEMID_CHILD_ARRAY apidl, ULONG *rgfInOut)
{
// If SFGAO_FILESYSTEM is returned, GetDisplayNameOf(SHGDN_FORPARSING) on that item MUST
// return a filesystem path.
HRESULT hr = E_INVALIDARG;

DWORD dwAttribs = 0;

dwAttribs |= SFGAO_FILESYSTEM;

if (1 == cidl)
{
    int nLevel = 0;
    hr = _GetLevel(apidl[0], &nLevel);
    if (SUCCEEDED(hr))
    {
        BOOL fIsFolder = FALSE;
        hr = _GetFolderness(apidl[0], &fIsFolder);
        if (SUCCEEDED(hr))
        {

            if (fIsFolder)
            {
                dwAttribs |= SFGAO_FOLDER;
            }
            if (nLevel < g_nMaxLevel)
            {
                dwAttribs |= SFGAO_HASSUBFOLDER;
            }

        }
    }
}

*rgfInOut &= dwAttribs;

return hr;
}` 

或者

DWORD dwAttribs = 0;

if (1 == cidl)
{
    int nLevel = 0;
    hr = _GetLevel(apidl[0], &nLevel);
    if (SUCCEEDED(hr))
    {
        BOOL fIsFolder = FALSE;
        hr = _GetFolderness(apidl[0], &fIsFolder);
        if (SUCCEEDED(hr))
        {

            if (fIsFolder)
            {
                dwAttribs |= SFGAO_FOLDER;

                dwAttribs |= SFGAO_FILESYSTEM;

                dwAttribs |= SFGAO_FILESYSANCESTOR;
            }
            else
            {
                dwAttribs |= SFGAO_SYSTEM;
            }

            if (nLevel < g_nMaxLevel)
            {
                dwAttribs |= SFGAO_HASSUBFOLDER;
            }
        }
    }
 }

*rgfInOut &= dwAttribs;

其他代码与update 2nd中的代码相同。 奇怪的是,在我编辑这些代码之后,正常使用CFileDialog的应用程序A(不包装它,而不是将m_bPickNonFileSysFoldersMode设置为true)可以正常显示所有虚拟文件夹,如我所料。 但是在所有其他应用程序中(包括使用CWFileDilao设置m_bPickNonFileSysFoldersMode为true的应用程序仍然无法查看虚拟文件夹。

---------------------------------------更新第二------ -------------------

我编写了一个派生自CFileDialog的简单类:

class CWFileDlg : public CFileDialog
{
public:
CWFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
    LPCTSTR lpszDefExt = NULL,
    LPCTSTR lpszFileName = NULL,
    DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    LPCTSTR lpszFilter = NULL,
    CWnd* pParentWnd = NULL,
    DWORD dwSize = 0,
    BOOL bVistaStyle = TRUE);

~CWFileDlg();
};

CWFileDlg::CWFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
                    LPCTSTR lpszDefExt,
                    LPCTSTR lpszFileName,
                    DWORD dwFlags,
                    LPCTSTR lpszFilter,
                    CWnd* pParentWnd,
                    DWORD dwSize,
                    BOOL bVistaStyle) : CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd, dwSize, bVistaStyle)
{
    m_bPickNonFileSysFoldersMode = TRUE;
}

CWFileDlg::~CWFileDlg()
{

}

然后我称之为:

CWFileDlg dlg(TRUE, NULL, 0, OFN_SHAREAWARE | OFN_ENABLESIZING | OFN_ALLOWMULTISELECT, L"all(*.*)|*.*||", this);

BOOL IsPickNonFileSysFoldersMode = dlg.IsPickNonFileSysFoldersMode();
//the value it gets is 1, which is TRUE

INT_PTR result = dlg.DoModal();

但是在弹出对话框中,虚拟文件​​夹是不可见的。它的树视图也是看不见的。 - - - - - - - - - - - - - - - - - - - - - 更新 - - - - ---------------------

protected:

BOOL m_bVistaStyle;
BOOL m_bPickFoldersMode;
BOOL m_bPickNonFileSysFoldersMode;
DWORD m_dwCookie;
void* m_pIFileDialog;
void* m_pIFileDialogCustomize;

m_bPickNonFileSysFoldersMode不是公共类型。我怎么能把它设置为TRUE?

Error   1   error C2248: 'CFileDialog::m_bPickNonFileSysFoldersMode' : cannot access protected member declared in class 'CFileDialog'   c:\users\liyuan.liu\documents\dp-dll\testvirtualfolder\browser2\browser2\browser2dlg.cpp    166 1   browser2

通过使用Microsoft Windows示例代码,我使用shell命名空间扩展在Windows 7上安装了一个虚拟文件夹。 以下屏幕截图使用常规文件夹浏览器打开它: enter image description here

然后我写了一个可以创建文件对话框的项目:

CFileDialog dlg(TRUE, NULL, 0, OFN_SHAREAWARE | OFN_ENABLESIZING | OFN_ALLOWMULTISELECT, L"all(*.*)|*.*||", this);
INT_PTR result = dlg.DoModal();

但是,在文件浏览器中,虚拟文件​​夹是不可见的:

enter image description here

但是!当我尝试将屏幕截图上传到StackOverflow(我正在使用的Web浏览器是chrome)时,用于选择文件的文件对话框可以显示虚拟文件夹的树视图(仅树视图): enter image description here

通过谷歌搜索,似乎CFileDialog不支持显示虚拟文件夹,因为它实际上并不存在于系统中。有什么解决方案可以解决吗 我也试过

bi.pidlRoot = pidlVirtual;
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS | BIF_BROWSEINCLUDEFILES | BIF_BROWSEFILEJUNCTIONS;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);

也不起作用。

1 个答案:

答案 0 :(得分:1)

不幸的是,文件对话框显示的是打开它的应用程序的功能。从Vista开始,它是所使用的IFileDialog界面,它通过IFileDialog::SetOptions method定义了一组选项。

FOS_FORCEFILESYSTEM标志确保返回的项目是文件系统项目(SFGAO_FILESYSTEM),这取决于您编写扩展程序的方式。 SFGAO_FILESYSTEM应响铃,它是IShellFolder::GetAttributesOf method可以返回的标志之一。如果你所做的事情是虚拟的(即:如果你不返回这个标志),那么当像这样配置IFileDialog时,它们就不会被显示出来。

如果您使用不同的应用程序(记事本,单词,Excel,浏览器等)测试您的扩展程序,您会发现有时会看到它,有时您也不会。

深入了解MFC的代码(dlgfile.cpp),你会发现:

    // We only expect and handle file system paths (for compatibility with GetOpenFileName functionality), so set the
    // "force file system" flag which enables GetOpenFileName-like download behavior for non file system paths, unless
    // the m_bPickNonFileSysFoldersMode is set to allow picking non-file system folders (like libraries in Windows 7).
    dwFlags |= FOS_FORCEFILESYSTEM;
    if (m_bPickNonFileSysFoldersMode)
    {
        dwFlags &= ~FOS_FORCEFILESYSTEM;
    }

因此,您需要将m_bPickNonFileSysFoldersMode设置为TRUE,以便CFileDialog显示您的扩展程序。

但它不会修复你不会使用此旗帜的其他应用......