我最近写了很多C++程序,头文件与源文件之间的切换实在是件痛苦的事。在vc6.0中有一个宏可以实现这个切换,但是我发现这个宏在vs2008中不好用。像其他出色的程序员一样,我决定自己写一个宏来实现这个功能。
如果你之前没有写过这个宏,那么请参考以下步骤:
1.在vs中选择Tools | Macros | Macros IDE,打开宏窗口;
2.右键点击左侧目录的MyMacros,选择Add | Add Module新建一个文件,将其重命名为CppUtilities,此时这个文件会在编辑器中打开;
3.在public Module CppUtilities和End Module两行之间添加如下代码:
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
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
4.ctrl+S保存,点击Tools | Options,在弹出的窗口中选择Environment | Keyboard,
在Show commands containing下面的文本框中输入CppUtilities;
5.点击Press shortcut keys下面文本框,按你想设置的快捷键(博主用的是Alt+F8);
6.点击OK,回到工程中测试一下吧!
原文地址:http://blog.csdn.net/u011421608/article/details/45149963