Torben said:
Hi
Can anyone tell me what is the short-cut to switch between a .h and .cpp
file, thanks
Torben Laursen
I have one for Visual Studio .NET 2003 that I wrote (the API's changed
from 2002 so it will take some serious changes to work with the older
version). It needs to be imported into the Macro IDE and bound to a
hotkey or button.
It could probably be optimized to be faster but it does the job (esp if
the files are already open).
Imports EnvDTE
Imports System.Windows.Forms
Imports Microsoft.VisualStudio.VCProjectEngine
'Note: You may have to manually go into the 'References' of this macro project and add Microsoft.VisualStudio.VCProjectEngine to it manually
Public Module gbxUtilityMacros
Private Function SearchProjectForFiles(ByRef proj As VCProject, ByRef file1 As String, ByRef file2 As String, ByRef file3 As String, ByRef file4 As String) As Boolean
Dim fileColl As IVCCollection = proj.Files
Dim filecount As Integer = fileColl.Count
Dim counter As Integer
For counter = 1 To filecount
Dim file As VCFile = fileColl.Item(counter)
Dim origname As String = file.Name
Dim fullname As String = file.FullPath
If (String.Compare(origname, file1, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file2, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file3, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
ElseIf (String.Compare(origname, file4, True) = 0) Then
DTE.Documents.Open(fullname)
Return True
End If
Next
Return False
End Function
Sub HeaderSourceSwitch()
On Error Resume Next
Dim sExtOffset As String = InStrRev(ActiveDocument.Name, ".")
Dim sExt As String
If sExtOffset > 0 Then
sExt = Right(ActiveDocument.Name, ActiveDocument.Name.Length - sExtOffset)
End If
sExt = LCase(sExt)
Dim sFileBase As String = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - Len(sExt) - 1)
Dim PossibleMatch1 As String
Dim PossibleMatch2 As String
Dim PossibleMatch3 As String
Dim PossibleMatch4 As String
' valid extension?
If sExt = "h" Or sExt = "hpp" Or sExt = "hxx" Then
PossibleMatch1 = sFileBase & ".cpp"
PossibleMatch2 = sFileBase & ".cxx"
PossibleMatch3 = sFileBase & ".cc"
PossibleMatch4 = sFileBase & ".c"
ElseIf sExt = "c" Or sExt = "cpp" Or sExt = "cxx" Then
PossibleMatch1 = sFileBase & ".hpp"
PossibleMatch2 = sFileBase & ".hxx"
PossibleMatch3 = sFileBase & ".hh"
PossibleMatch4 = sFileBase & ".h"
Else
Exit Sub
End If
' is the needed file already opened?
Dim doc As Document
For Each doc In DTE.Documents
If (String.Compare(doc.Name, PossibleMatch1, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch2, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch3, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
If (String.Compare(doc.Name, PossibleMatch4, True) = 0) Then
If doc.ActiveWindow() Is Nothing Then
DTE.Documents.Open(doc.FullName)
Else
doc.Activate()
End If
Exit Sub
End If
Next
Dim ThisProj As VCProject = DTE.ActiveDocument.ProjectItem.ContainingProject().Object
If SearchProjectForFiles(ThisProj, PossibleMatch1, PossibleMatch2, PossibleMatch3, PossibleMatch4) Then
Exit Sub
End If
Dim sol As Solution = DTE.Solution
Dim loopproj As Project
For Each loopproj In sol.Projects
Dim Proj As VCProject = loopproj.Object
'Minor optimization: Skip the project we already searched above
If Not Proj Is ThisProj Then
If SearchProjectForFiles(Proj, PossibleMatch1, PossibleMatch2, PossibleMatch3, PossibleMatch4) Then
Exit Sub
End If
End If
Next
End Sub
End Module