65.9K
CodeProject 正在变化。 阅读更多。
Home

在.h文件中打开相关的C++ .h/.cpp文件并调用编译器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.44/5 (3投票s)

2007年2月7日

CPOL

2分钟阅读

viewsIcon

89266

downloadIcon

229

用于查找相关.h/.cpp文件并执行某些操作的宏。

引言

在Visual Studio .NET 2003/2005中,如果你想在编辑.h文件时打开相关的.cpp文件,或者你想在编辑.cpp文件时打开相关的.h文件,或者你想在编辑.h文件时编译相关的.cpp文件,有没有简单的方法?

通常,程序员在解决方案资源管理器中打开相关文件,或者使用Ctrl+Tab查找相关文件。这不是很简单。所以我写了一个宏来查找和打开相关文件。将宏导入到宏资源管理器后,您只需双击即可运行它。

当然,还有更简单的方法。按照菜单“工具->选项->环境->键盘”打开快捷键配置对话框。然后找到OpenRelatedFile宏,给它一个快捷方式,例如Ctrl+F6,然后找到CompileImplFile宏,给它一个快捷方式,例如Ctrl+F7。好的,现在您可以使用快捷方式来调用宏了。

基本观点

要在项目中找到相关文件,需要几个步骤

  1. 获取当前打开的源文件的文件名,并生成相关的文件名。例如,如果当前文件是bla.h,我们应该寻找bla.cppbla.c等。
  2. 创建一个包含我们要查找的所有文件的映射。
  3. 在项目中查找文件。
  4. 如果找到目标文件,则打开或编译它。

源代码

有必要遍历项目中的所有文件夹以递归方式查找文件。为此,我们需要一个递归函数

' Find and open file in specified project item and it's children
Private Sub FindAndOpenFile(ByRef item As ProjectItem)
    ' The item maybe a file or a folder.
    If filenames.IndexOf(item.Name.ToLower()) >= 0 Then
        ' If the item's document is not nothing, it should be a file
        ' This condition cannot be merged outer one, becuase if the document
        ' is not nothing, the file should be activated by using Document property.
        ' However, VB calculates all condition in 'and' chain.
        If Not item.Document Is Nothing Then
            DTE.ItemOperations.OpenFile(item.FileNames(0))
            Exit Sub
        End If
    End If

    ' To process in childrens of current item.
    ' If current item has no children, the for each loop should be ignored by interpreter
    Dim i As ProjectItem
    For Each i In item.ProjectItems
        FindAndOpenFile(i)
    Next
End Sub

全局变量filenames包含一些我们要查找的文件名。如果该函数找到其中一个文件名,则打开它并尝试查找另一个文件名。

该函数以一个ProjectItems对象开始。对象中的每个项目可能是一个文件名或一个文件夹。如果它是一个文件夹,它的ProjectItems属性不应该为空。因此,该函数以该项目的ProjectItems属性作为参数递归地调用自身。

Private Sub FindAndOpenFileInProject()
    ' Get current project
    Dim projs As System.Array
    Dim proj As Project
    projs = DTE.ActiveSolutionProjects()

    If projs.Length = 0 Then
        Exit Sub
    End If

    proj = CType(projs.GetValue(0), EnvDTE.Project)

    ' To look for the related file and activate it.
    Dim item As ProjectItem
    For Each item In proj.ProjectItems
        FindAndOpenFile(item)
    Next
End Sub

OpenRelatedFile函数中,应该搜索所有相关的.cpp/.c/.h/.hpp文件。

' Create a set to contains the filename which would be looked for.
filenames.Clear()
filenames.Add(curName)
filenames.Add(curName & ".h")
filenames.Add(curName & ".cpp")
filenames.Add(curName & ".c")
filenames.Add(curName & ".hpp")
filenames.Remove(curFilename)

CompileImplFile函数中,只应搜索相关的.cpp/.c文件,因此当我们设置filenames列表时,存在一些差异

' Create a set to contains filenames which would be looked for.
filenames.Clear()
filenames.Add(curName & ".cpp")
filenames.Add(curName & ".c")

如果找到并打开了.cpp/.c文件,该函数将执行Build.Compile命令以编译它。

curFilename = DTE.ActiveDocument.Name
If (curFilename.EndsWith(".cpp") Or curFilename.EndsWith(".c")) Then
    DTE.ExecuteCommand("Build.Compile")
End If

更新日志

  • 2007-07-25:优化了代码。
  • 2007-07-12:修复了有时我们无法打开相关的.h文件的错误。
© . All rights reserved.