[转]VS2008 在h与cpp文件间切换快捷键

本文介绍了一个用于Visual Studio 2008的宏,该宏可以方便地在C++源文件(.cpp)与头文件(.h)之间进行切换。作者分享了具体的实现步骤及宏代码,读者可以轻松地将此功能添加到自己的IDE中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

I’ve been doing a lot of managed C++ programming lately and I had forgotten what a pain it is switching back and forth between the header file and source file.  Back in the days of Visual Studio 6 I had a macro that switched between the CPP and H file, so I went googling, but the macro I found didn’t work very well in VS2008.  Like any good coder, I decided to write it myself instead.

If you haven’t written a macro before, here are the steps.

  1. In Visual Studio, go to Tools | Macros | Macros IDE. A new window should open and in the Project Explorer, the MyMacros project should be open.
  2. Right click on the MyMacros project and select Add | Add Module. Name it CppUtilities.  The CppUtilities should open in the editor window.
  3. Add the code from below into the module and save the project.

view plaincopy to clipboardprint?

  1. ‘===================================================================== 
  2. ‘ If the currently open document is a CPP or an H file, attempts to 
  3. ‘ switch between the CPP and the H file. 
  4. ‘===================================================================== 
  5. Public Sub SwitchBetweenSourceAndHeader() 
  6. Dim currentDocument As String
  7. Dim targetDocument As String
  8.   currentDocument = ActiveDocument.FullName 
  9. If currentDocument.EndsWith(“.cpp”, StringComparison.InvariantCultureIgnoreCase) Then
  10.     targetDocument = Left(currentDocument, Len(currentDocument) - 3) + “h” 
  11.     OpenDocument(targetDocument) 
  12. ElseIf currentDocument.EndsWith(“.h”, StringComparison.InvariantCultureIgnoreCase) Then
  13.     targetDocument = Left(currentDocument, Len(currentDocument) - 1) + “cpp” 
  14.     OpenDocument(targetDocument) 
  15. End If
  16. End Sub
  17. ‘===================================================================== 
  18. ‘ Given a document name, attempts to activate it if it is already open, 
  19. ‘ otherwise attempts to open it. 
  20. ‘===================================================================== 
  21. Private Sub OpenDocument(ByRef documentName As String) 
  22. Dim document As EnvDTE.Document 
  23. Dim activatedTarget As Boolean
  24.   activatedTarget = False
  25. For Each document In Application.Documents 
  26. If document.FullName = documentName And document.Windows.Count > 0 Then
  27.       document.Activate() 
  28.       activatedTarget = True
  29. Exit For
  30. End If
  31. Next
  32. If Not activatedTarget Then
  33.     Application.Documents.Open(documentName, “Text”) 
  34. End If
  35. End Sub

If you switch back to Visual Studio and open the Macro Explorer, you should see the new module CppUtilities and the new macro SwitchBetweenSourceAndHeader in the tree.  You could run the macro from here, but it is much easier to bind it to a keystroke.

  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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值