Muponder

为者常成,行者常至。

C++博客 首页 新随笔 联系 聚合 管理
  1 Posts :: 7 Stories :: 7 Comments :: 0 Trackbacks
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.

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.
Right click on the MyMacros project and select Add | Add Module. Name it CppUtilities.  The CppUtilities should open in the editor window.
Add the code from below into the module and save the project.
view plaincopy to clipboardprint?
‘=====================================================================  
‘ 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  
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.

Click on Tools | Options then go to the Environment | Keyboard tab.
In the Show commands containing: box, type CppUtilities. This should filter the list down to one entry,Macros.MyMacros.CppUtilitities.SwitchBetweenSourceAndHeader.
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.
Click OK then open a CPP or H file and give it a try.
If you found this post helpful, please "Kick" it so others can find it too:
posted on 2013-06-18 22:17 盛源博 阅读(189) 评论(0)  编辑 收藏 引用 所属分类: visual stdio

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理