问题:头文件和源文件之间使用快捷键切换,可以提高工作效率。本例,设置快捷键:Alt+F2
操作步骤如下:
1.打开:工具
—》宏—》宏 IDE,如图1:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img1.ph.126.net/Oj82zATAAGOsG323Jj9klw==/6597762458983827154.jpg)
图1
2.添加模块,这里模块名称为
CppUtilities,
如图2,
图3:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img1.ph.126.net/Je18I69vuwXCfMZXSXCbUg==/6597603029797791058.jpg)
图2
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img2.ph.126.net/TGWZRMDEOzFeqFdNfLs4yw==/3256665480642564116.jpg)
图3
添加模块
CppUtilities,结果为图4:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img2.ph.126.net/IVgMrYXUm2ZM1w0c5AW5Ww==/2268406837411244318.jpg)
图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:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img2.ph.126.net/k9btim2hmxR5EiUdFZgTOA==/6597664602448948787.jpg)
图5
打开
宏资源管理器,结果如图6:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img0.ph.126.net/JiAfHGoLwlKPy8gaC228lQ==/2459246871621045129.jpg)
图6
6.开始设置快捷键。
打开:工具
—》
选项—》环境
—》
键盘,在显示命令里包含输入:
CppUtilities,列表中
至少
会筛选一项。如图7:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img2.ph.126.net/y-cKkxzU1okQR6VA-b-URA==/708753991457501057.jpg)
图7
在按快捷键输入框中,按下:Alt+F2,点击分配,分配结果,如图8:
![VS2008 头文件和源文件切换 - lys_19850716 - 娃哈哈,营养快线,小Q!](http://img1.ph.126.net/rIHMWL1jxmIYQBh2oftnSQ==/300896750203862599.jpg)
图8
点击确定,在VS2008中,试试
Alt+F2的效果。