问题:头文件和源文件之间使用快捷键切换,可以提高工作效率。本例,设置快捷键:Alt+F2
操作步骤如下:
1.打开:工具—》宏—》宏 IDE,如图1:

图1
2.添加模块,这里模块名称为CppUtilities,如图2,图3:

图2

图3
添加模块CppUtilities,结果为图4:

图4
3.添加模块代码。在CppUtilities文件中添加如下代码
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
4.保存。文件—》保存MyMacros
5.查看保存结果。打开:工具—》宏—》宏资源管理器,如图5:

图5
打开宏资源管理器,结果如图6:

图6
6.开始设置快捷键。打开:工具—》选项—》环境—》键盘,在显示命令里包含输入:CppUtilities,列表中至少会筛选一项。如图7:

图7
在按快捷键输入框中,按下:Alt+F2,点击分配,分配结果,如图8:

图8
点击确定,在VS2008中,试试Alt+F2的效果。