⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workspaceinfo.cpp

📁 一个统计文件大小和程序信息的插件程序(vc或vc.net下使用)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }

    bool IsAFile(DTEProjectItem& pItem)
    {
        // How do you distinguish between a file item and a non-file item 
        // (such as a folder)?  Good question!  (Wish I has a good answer)...
        // Here I do it by:
        // 1. Making sure there is only 1 file associated with the item at
        //    this level (otherwise it is a folder).
        // 2. Comparing the name to the file name.  If they are the same, 
        //    this is not a file.  This is because the "name" of a file item
        //    is the name of the file without the path, and the "file-name"
        //    is the name of the file with the path.
        // 3. If all the previous checks passed, verify that the file-name
        //    is not pointing to a physical directory.
        //
        // I have considered other ways, but these involve item properties,
        // which I am not sure will be named the same in localized versions
        // of DevEnv..
        
        ASSERT(pItem != NULL);

        // 1 //
        short cFiles;
        if (!SUCCEEDED(pItem->get_FileCount(&cFiles))  ||  cFiles != 1)
        {
            return false;
        }

        // 2 //
        CComBSTR bStrName, bStrFileName;
        if (!SUCCEEDED(pItem->get_Name(&bStrName))              ||
            !SUCCEEDED(pItem->get_FileNames(1, &bStrFileName))  ||
            wcscmp(bStrName, bStrFileName) == 0)
        {
            return false;
        }

        // 3 //
        CString sPath(bStrFileName);
        if (CHK_FLAG(GetFileAttributes(sPath), FILE_ATTRIBUTE_DIRECTORY))
        {
            return false;
        }

        return true;
    }

    virtual CString GetName() const
    {
        CString cStr = "Error!";
        CComBSTR bStr;
        if (SUCCEEDED(m_pPrj->get_FileName(&bStr)))
        {
            cStr = bStr;
        }
        return cStr;
    }

    virtual int GetFileCount() const
    {
        return m_Files.GetSize();
    }

    virtual IProjectFile *GetFile(int iIndex) const
    {
        ASSERT(iIndex >= 0);
        ASSERT(iIndex < GetFileCount());
        return new CProjectFile_VC7(m_Files[iIndex]);
    }
};


class CWorkspaceInfo_VC7 : public IWorkspaceInfo
{
protected:
    EnvDTE::_DTE *m_pDTE;

    enum 
    { 
        FILTER_NDX_SOLUTION = 0,
        FILTER_NDX_STARTUP  = 1 
    };

    typedef CComPtr<EnvDTE::Project> DTEProject;
    CArray<CAdapt<DTEProject>, CAdapt<DTEProject> > m_arrDTEProjects;


public:
    CWorkspaceInfo_VC7(EnvDTE::_DTE *pDTE) : m_pDTE(pDTE)
    {
        ASSERT(pDTE != NULL);
    }

    virtual ~CWorkspaceInfo_VC7() 
    {
    };

    virtual void Refresh()
    {
        m_arrDTEProjects.RemoveAll();

        LONG lCount;
        HRESULT hr;
        CComPtr<EnvDTE::_Solution> pSolution;
        CComPtr<EnvDTE::Projects> pProjects;

        hr = m_pDTE->get_Solution(&pSolution);
        if (SUCCEEDED(hr))
        {
            hr = pSolution->get_Projects(&pProjects);
        }
        if (SUCCEEDED(hr))
        {
            hr = pProjects->get_Count(&lCount);
        }
        if (SUCCEEDED(hr)  &&  lCount)
        {
            // We know there are lCount projects in all, however .NET
            // actually creates virtual project for files that are
            // open but don't belong to any physical project.
            // We will scan through all the projects, making sure
            // that they have a physical file.
            for (LONG i = 0; i < lCount; ++i)
            {
                DTEProject pPrj;
                if (SUCCEEDED(pProjects->Item(CComVariant(i + 1), &pPrj)))
                {
                    CComBSTR bStr;
                    if (SUCCEEDED(pPrj->get_FileName(&bStr))  &&
                        bStr.Length() > 0)
                    {
                        // the project has a physical file, save it 
                        // in our list
                        m_arrDTEProjects.Add(pPrj);
                    }
                }
            }
        }
        ASSERT(SUCCEEDED(hr));
    }

    virtual CString GetWorkspaceName() const
    {
        CString sName = "Error!";
        CComPtr<EnvDTE::_Solution> pSolution;
        if (SUCCEEDED(m_pDTE->get_Solution(&pSolution)))
        {
            CComBSTR bStr;
            pSolution->get_FullName(&bStr);
            sName = bStr;
        }
        return sName;
    }

    virtual int GetProjectCount() const
    {
        return m_arrDTEProjects.GetSize();
        return 0;
    }

    virtual IWorkspaceProject *GetProject(int iIndex) const 
    {
        ASSERT(iIndex >= 0);
        ASSERT(iIndex < GetProjectCount());
        return new CWorkspaceProject_VC7(m_arrDTEProjects[iIndex]);
    }

    virtual IWorkspaceProject *GetProject(CComVariant id) const 
    {
        CComPtr<EnvDTE::_Solution> pSolution;
        if (SUCCEEDED(m_pDTE->get_Solution(&pSolution)))
        {
            CComPtr<EnvDTE::Projects> pProjects;
            if (SUCCEEDED(pSolution->get_Projects(&pProjects)))
            {
                CComPtr<EnvDTE::Project> pProject;
                if (SUCCEEDED(pProjects->Item(id, &pProject)))
                {
                    return new CWorkspaceProject_VC7(pProject);
                }
            }
        }
        ASSERT(FALSE);
        return NULL;
    }

    
    HRESULT GetStartupProjectsSafeArray(VARIANT& v) const
    {
        CComPtr<EnvDTE::_Solution> pSolution;
        HRESULT hr;
        hr = m_pDTE->get_Solution(&pSolution);
        if (SUCCEEDED(hr))
        {
            CComPtr<EnvDTE::SolutionBuild> pSolutionBuild;
            hr = pSolution->get_SolutionBuild(&pSolutionBuild);
            if (SUCCEEDED(hr))
            {
                hr = pSolutionBuild->get_StartupProjects(&v);
            }
        }
        return hr;
    }

    virtual int GetWorkspaceFilterCount() const
    {
        return 2;
    }

    virtual LPCTSTR GetWorkspaceFilterName(int iFilterIndex) const
    {
        switch (iFilterIndex)
        {
            case FILTER_NDX_SOLUTION:
                return "Solution";
                
            case FILTER_NDX_STARTUP:
                return "Startup Projects";

            default:
                ASSERT(0);
        }
        return "";
    }

    virtual int GetWorkspaceFilterProjectCount(int iFilterIndex) const
    {
        switch (iFilterIndex)
        {
            case FILTER_NDX_SOLUTION:
                return GetProjectCount();
                
            case FILTER_NDX_STARTUP:
            {
                VARIANT v;
                v.vt = VT_EMPTY;
                if (SUCCEEDED(GetStartupProjectsSafeArray(v)))
                {
                    LRESULT lCount = 0;
                    SafeArrayGetUBound(v.parray, 1, &lCount);
                    VariantClear(&v);
                    return lCount + 1;
                }
                ASSERT(0);
                return 0;
            }

            default:
                ASSERT(0);
        }
        return 0;
    }

    virtual IWorkspaceProject *GetWorkspaceFilterProject(int iFilterIndex,
        int iProjectIndex) const
    {
        switch (iFilterIndex)
        {
            case FILTER_NDX_SOLUTION:
                return GetProject(iProjectIndex);
                
            case FILTER_NDX_STARTUP:
            {
                VARIANT v;
                v.vt = VT_EMPTY;
                IWorkspaceProject *pPrj = NULL;
                if (SUCCEEDED(GetStartupProjectsSafeArray(v)))
                {
                    LONG l = iProjectIndex;
                    VARIANT vElem;
                    vElem.vt = VT_EMPTY;
                    if (SUCCEEDED(SafeArrayGetElement(v.parray, &l, &vElem)))
                    {
                        pPrj = GetProject(vElem);
                        VariantClear(&vElem);
                    }
                    VariantClear(&v);
                }
                return pPrj;
            }

            default:
                ASSERT(0);
        }
        return NULL;
    }
};

void InitializeWorkspaceInfo_VC7(EnvDTE::_DTE *pDTE)
{
    ASSERT(g_pWorkspaceInfo == NULL);
    ASSERT(pDTE != NULL);
    g_pWorkspaceInfo = new CWorkspaceInfo_VC7(pDTE);
}

#endif // TARGET_VC7

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -