출처 : http://blog.naver.com/techshare/220138680455
Visual Studio 2013에서 가장 아쉬운 부분이 바로 매크로인데요. 다행히 "Visual Commander" 확장 도구를 이용하면 그와 같은 제약에서 벗어날 수는 있습니다.
Visual Commander
; http://vlasovstudio.com/visual-commander/
현재 Free, Professional 제품으로 나누어서 판매하고 있습니다.
Free 버전 다운로드 (Visual Commander v1.5 - December 23, 2013)
; http://vlasovstudio.com/visual-commander/VisualCommander_15.vsix
Professional Edition 구매 ($39)
; http://vlasovstudio.com/visual-commander/professional_edition.html
사용법도 간단합니다. vsix 설치 후 Visual Studio를 실행하면 "VCMD" 메뉴가 생기는데 거기서 "Commands"를 선택한 후 "Add" 버튼을 누르면 다음과 같이 기본 매크로 함수를 위한 뼈대가 생성됩니다.
예를 들어, 제가 Visual Studio 2010에서 사용했던 매크로 함수는 다음과 같은데요.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE100
Imports System.Diagnostics
Public Module InsertTag
Sub InsertCenterHr()
Dim selection As TextSelection
If (ActiveDocument() Is Nothing) Then
Exit Sub
End If
selection = DTE.ActiveDocument.Selection()
Dim str As String
str = "<hr style='width: 50%' />" & Environment.NewLine
SetText(selection, str)
End Sub
Sub SetText(ByVal selection As TextSelection, ByVal txt As String)
Dim prop As EnvDTE.Property
prop = DTE.Properties("TextEditor", "PlainText").Item("IndentStyle")
Dim previousIndent = prop.Value
prop.Value = 0
selection.Text = txt
prop.Value = previousIndent
End Sub
End Module
이를 Visual Commander에서는 다음과 같이 동일하게 코드를 복사해서 사용할 수 있습니다.
Imports EnvDTE
Imports EnvDTE80
Imports System
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
Dim selection As TextSelection
Dim activeDocument As EnvDTE.Document
activeDocument = DTE.ActiveDocument
If (activeDocument Is Nothing) Then
Exit Sub
End If
selection = activeDocument.Selection()
Dim str As String
str = "<hr style='width: 50%' />" & Environment.NewLine
SetText(DTE, selection, str)
End Sub
Sub SetText(DTE As EnvDTE80.DTE2, ByVal selection As TextSelection, ByVal txt As String)
Dim prop As EnvDTE.Property
prop = DTE.Properties("TextEditor", "PlainText").Item("IndentStyle")
Dim previousIndent = prop.Value
prop.Value = 0
selection.Text = txt
prop.Value = previousIndent
End Sub
End Class
보시는 바와 같이 DTE 변수를 넘겨준다는 점을 제외하고는 거의 차이가 없습니다.
불편한 점은 사용법에 있습니다. 이전에는 매크로 탐색기에서 해당 매크로를 더블-클릭하면 실행이 가능했지만, "Visual Commander"의 경우 "Commands" 창에서 더블-클릭해 실행하는 것을 지원하지 않고 있습니다. 매번 "VCMD" 메뉴를 펼쳐서 새롭게 등록된 명령어를 선택해 줘야 하는데요. 물론 이 부분은 기본적으로 단축키 등록을 통해 우회적으로 해결할 수는 있지만 어쨌든 불편한 것은 사실입니다.
(이 부분을 제가 요청했는데 최신 버전에서 개발자가 그 의견을 받아들여 수정했습니다. 이제 예전의 Macro Explorer처럼 편리하게 사용할 수 있습니다.)
매크로가 그리웠던 분들... ^^ 지금 설치해 보세요.
참고로 아래 코드는 현재 제가 사용하고 있는 _T("") 자동으로 해 주는 매크로 소스입니다.
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
'DESCRIPTION: This macro will automatically put "_T( )" around
' your strings, Author: Orin Walker, 1999, Version 1.1
' Last change - Acidpop(http://acidpop.tistory.com) (2012.09.07)
' Supported Visual Studio 2010 Macro
Dim iCount As Integer
Dim bFoundAQuote As Boolean
Dim strTemp As String
Dim strStuffAtEnd As String
Dim bDone As Boolean
Dim str As String
Dim strBuildString As String
Dim Selection As TextSelection
Dim win as Window
win = DTE.ActiveWindow
If (win.type <> EnvDTE.vsWindowType.vsWindowTypeDocument) And (win.type <> EnvDTE.vsWindowType.vsWindowTypeCodeWindow) Then
' MsgBox( "type=" & win.type & " Doc=" & EnvDTE.vsWindowType.vsWindowTypeDocument & " CodeWin=" & EnvDTE.vsWindowType.vsWindowTypeCodeWindow )
Exit Sub
End If
iCount = 0
bFoundAQuote = False
DTE.ActiveDocument.Selection.SelectLine()
strTemp = DTE.ActiveDocument.Selection.Text
Selection = DTE.ActiveDocument.Selection
strStuffAtEnd = ""
While bDone <> True
str = ParseString(strTemp, bFoundAQuote, strStuffAtEnd)
strBuildString = strBuildString + str
If bFoundAQuote = True Then
strTemp = strStuffAtEnd
Else
bDone = True
'DTE.ActiveDocument.Selection.Delete()
'DTE.ActiveDocument.Selection = strBuildString
Selection.Text = strBuildString
End If
iCount = iCount + 1
If iCount > 100 Then ' safety valve
bDone = True
End If
End While
'End If
End Sub
Function ParseString(ByVal strTemp, ByRef bFoundAQuote, _
ByRef strStuffAtEnd)
Dim strSpace As String
Dim iLen As Integer
Dim iPos As Integer
Dim x As Integer
Dim strCheck As String
Dim iUnderscoreTPos As Integer
Dim strBeforeFirstQuote As String
Dim strNewTempStr As String
Dim strRemaining As String
Dim strStuffInQuotes As String
'DESCRIPTION: This is a helper function for the UnderscoreT macro,
' Author: Orin Walker, 1999, Version 1.1
' Comment in/out whatever style you prefer
strSpace = "" ' NO space before or after "_T("
'strSpace = " " ' Add a space before and after "_T("
iLen = Len(strTemp)
bFoundAQuote = False
' Get the position of the first quote on the line
iPos = InStr(strTemp, Chr(34))
If iPos > 0 Then 'a quote was found
' Go back and see if we have an existing
' _T( defined for this quote
x = iPos - 5 ' Go back up to 5 characters
If x <= 0 Then ' If we have reached the
' beginning of our string
x = 1 ' Set x to start at the first character
End If
strCheck = Mid(strTemp, x, iPos)
iUnderscoreTPos = InStr(strCheck, "_T(")
' If we found one grab everything before the first quote
strBeforeFirstQuote = Mid(strTemp, 1, iPos - 1)
If iUnderscoreTPos > 0 Then ' we found an "_T("
' Do NOT add the "_T(" to our temporary string
strNewTempStr = strBeforeFirstQuote
Else
' Now create our new temporary string and append "_T("
strNewTempStr = strBeforeFirstQuote + "_T(" + strSpace
End If
' Get the remaining string
strRemaining = Mid(strTemp, iPos + 1, iLen)
iLen = Len(strRemaining)
' Now find the second quote
iPos = InStr(strRemaining, Chr(34))
If iPos > 0 Then
' If we found one save the stuff in quotes
strStuffInQuotes = Chr(34) + Mid(strRemaining, 1, iPos)
' And grab the stuff after the quotes
strStuffAtEnd = Mid(strRemaining, iPos + 1, iLen)
If iUnderscoreTPos > 0 Then ' we found an _T(
' Do NOT add the final ")" to our parsed string,
' because it alreasy exists
ParseString = strNewTempStr + strStuffInQuotes
Else
' Create our parsed string
ParseString = strNewTempStr + strStuffInQuotes + _
strSpace + ")"
End If
bFoundAQuote = True
Else
' No SECOND quote was found so just return
' what was passed in
ParseString = strTemp
End If
Else
' No quote was found so just return what was passed in
ParseString = strTemp
End If
End Function
End Class
다음은 CPP 파일과 .H 파일을 서로 왔다 갔다 해 주는 소스입니다.
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Function GetFilenameFromPath(ByVal strPath As String) As String
' Returns the rightmost characters of a string upto but not including the rightmost '\'
' e.g. 'c:\winnt\win.ini' returns 'win.ini'
If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
End If
End Function
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
'////////////////////////////////////////////
'Nooruddin Kapasi 1998.
'Pavel Sokolov , CEZEO software , http://www.cezeo.com , Adaptation for .NET
'DESCRIPTION: Switch Between Header and cpp
'////////////////////////////////////////////
Dim a As String
Dim b As String
Dim Flag As Integer
Dim tmp As String
Flag = 0
a = DTE.ActiveDocument.FullName()
tmp = InStr(a, ".cpp")
If tmp Then
b = Left(a, Len(a) - 3) + "h"
Flag = 1
Else
tmp = InStr(a, ".h")
If tmp Then
b = Left(a, Len(a) - 1) + "cpp"
Flag = 1
End If
End If
If Flag Then
Try
DTE.Documents.Open(b, "Text")
Catch
a = GetFilenameFromPath(DTE.ActiveDocument.FullName())
tmp = InStr(a, ".cpp")
If tmp Then
b = Left(a, Len(a) - 3) + "h"
Flag = 1
Else
tmp = InStr(a, ".h")
If tmp Then
b = Left(a, Len(a) - 1) + "cpp"
Flag = 1
End If
End If
a = DTE.Solution.FindProjectItem(b).FileNames(0)
DTE.Documents.Open(a, "Text")
End Try
End If
End Sub
End Class
다음은 선택 영역을 //로 주석 처리 해 주는 소스입니다.
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
Dim win
win = DTE.ActiveWindow
If (win.type <> EnvDTE.vsWindowType.vsWindowTypeDocument) And (win.type <> EnvDTE.vsWindowType.vsWindowTypeCodeWindow) Then
MsgBox( "This macro can only be run when a text editor window is active." )
Exit Sub
else
if InStr( DTE.ActiveDocument.Selection.Text, vbCr ) > 0 then
DTE.ActiveDocument.Selection.ReplaceText( "^", "//", EnvDTE.DsTextSearchOptions.dsMatchRegExp )
End If
End If
End Sub
End Class
다음은 선택 영역의 //로 된 주석을 해제 해 주는 소스입니다.
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
Dim win
win = DTE.ActiveWindow
If (win.type <> EnvDTE.vsWindowType.vsWindowTypeDocument) And (win.type <> EnvDTE.vsWindowType.vsWindowTypeCodeWindow) Then
MsgBox( "This macro can only be run when a text editor window is active." )
Exit Sub
else
DTE.ActiveDocument.Selection.ReplaceText( "^//", "", EnvDTE.DsTextSearchOptions.dsMatchRegExp )
End If
End Sub
End Class
출처 : http://blog.naver.com/techshare/220138680455