Visual Studio里在.H文件和.CPP文件之间切换

首先Tools>Macros,打开Macro Explorer。新建一个宏。


Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module CppUtilities

    '=====================================================================
    ' If the currently open document is a CPP or an H file, attempts to
    ' switch between the CPP and the H file.
    '=====================================================================
    Public Sub SwitchBetweenSourceAndHeader()
        Dim currentDocument As String
        Dim targetDocument As String
        currentDocument = ActiveDocument.FullName
        If currentDocument.EndsWith(".cpp", StringComparison.InvariantCultureIgnoreCase) Then
            targetDocument = Left(currentDocument, Len(currentDocument) - 3) + "h"
            OpenDocument(targetDocument)
        ElseIf currentDocument.EndsWith(".h", StringComparison.InvariantCultureIgnoreCase) Then
            targetDocument = Left(currentDocument, Len(currentDocument) - 1) + "cpp"
            OpenDocument(targetDocument)
        End If
    End Sub

    '=====================================================================
    ' Given a document name, attempts to activate it if it is already open,
    ' otherwise attempts to open it.
    '=====================================================================
    Private Sub OpenDocument(ByRef documentName As String)
        Dim document As EnvDTE.Document
        Dim activatedTarget As Boolean
        activatedTarget = False

        For Each document In Application.Documents
            If document.FullName = documentName And document.Windows.Count > 0 Then
                document.Activate()
                activatedTarget = True
                Exit For
            End If
        Next
        If Not activatedTarget Then
            Application.Documents.Open(documentName, "Text")
        End If
    End Sub

End Module

然后加载宏,绑定快捷键。

1. Click on Tools | Options then go to the Environment | Keyboard tab.

2. In the Show commands containing: box, type CppUtilities. This should filter the list down to one entry, Macros.MyMacros.CppUtilitities.SwitchBetweenSourceAndHeader.

3. Click on the Press shortcut keys: text box and then press the keystroke you would like to use to run the macro. If the keystroke is already used, it will show you below in the Shortcut currently used by: dropdown. When you find one that is unused, click the Assign button to use it. I use Ctrl+Shift+Alt+Bkspce.

4. Click OK then open a CPP or H file and give it a try.

方法转自:http://www.alteridem.net/2008/02/26/visual-studio-macro-to-switch-between-cpp-and-h-files/

Advertisements