Introduction

Visual Studio Macro 몇가지 예제를 통해 활용에 관한 내용을 다뤄 봅니다.

보시다시피 VS 매크로는 초딩 정광짱도 하는 VB 스크립트로 되어 있습니다.

Visual Studio .NET 2003을 기준으로 설명하겠습니다.

 

.h/.cpp 파일 전환

    ' if this file is a .cpp then open up the .h 
    '  or vice versa
    Sub OpenPartnerFile()
        Dim filename As String
        Dim partnerFilename As String
        filename = DTE.ActiveDocument.FullName
        If (filename.EndsWith(".h")) Then
            partnerFilename = filename.Substring(0, filename.Length() - 2) + ".cpp"
        End If
        If (filename.EndsWith(".cpp")) Then
            partnerFilename = filename.Substring(0, filename.Length() - 4) + ".h"
        End If
        DTE.ItemOperations.OpenFile(partnerFilename)
    End Sub

 

이 매크로는 Visual Assist Alt+O와 동일한 기능입니다.

'DTE'는 스크립트를 통해 Visual Studio IDE와 통신할 수 있는 자동화 객체입니다.

위의 예제에서 DTE.ActiveDocument.FullName는 현재 열린 파일의 풀패스를 가리킵니다.

DTE.ItemOperations.OpenFile 메써드로 지정한 파일을 열 수 있습니다.

 

다음과 같이 사용자 정의 매크로를 추가할 수 있습니다:

1. 도구->매크로->매크로 IDE(Alt+F11)

2. 프로젝트 탐색기에서 MyMacros를 선택합니다.

3. 오른쪽 버튼을 눌러서 추가->모듈 추가를 선택합니다. 모듈 이름은 알아서 입력합니다.

4. 위의 예제를 복사치기하고 저장합니다.

5. 매크로 탐색기에서 추가한 매크로(OpenPartnerFile)를 더블클릭하여 실행할 수 있습니다.

 

다음과 같이 매크로의 단축키를 지정할 수 있습니다:

1. 도구->사용자 지정->키보드

2. 그림과 같이 지정할 매크로를 선택합니다.

3. 바로 가기 키 누르기를 클릭하고 단축키를 입력합니다.

4. 할당 버튼을 눌러서 등록합니다.

 

Macros for Calling External Tools

간혹 현재 열린 파일을 아구먼트로 넘겨서 외부 프로그램을 실행해야할 경우가 있습니다. 있다고 칩시다.

다음 예제에서 메모장을 실행하는 방법을 알아 보겠습니다.

 

    Sub OpenNotepadCurFile()
        Dim filename As String = DTE.ActiveWindow.Document.FullName
        Dim commandString = "notepad.exe """ + filename + """"
        'MsgBox(commandString)
        ChDir("d:\windows\system32")
        Shell(commandString, AppWinStyle.NormalFocus, True, 100)
    End Sub

 

ChDir("d:\windows\system32")는 Notepad.exe가 있는 시스템 폴더로 이동합니다.

Shell 명령으로 메모장을 실행합니다. 파라메터는 보면 그냥 압니다.

 

Macros that Communicate with the Code Editor

소스 편집기의 내용을 조작하는 예제를 보시겠습니다.

다음은 선택 영역을 중괄호로 감싸고 들여쓰기하는 예제입니다.

 

    ' Takes the current selection, indents it, and surrounds with braces.
    Sub CreateCodeBlock()
        CreateCodeBlock_Helper("{", "}")
    End Sub

 

    ' Takes the current selection, indents it, and surrounds it with startOfBlock and endOfBlock
    Sub CreateCodeBlock_Helper(ByVal startOfBlock As String, ByVal endOfBlock As String)
        Dim sel As TextSelection = DTE.ActiveDocument.Selection
        Dim objAnchor As VirtualPoint = sel.AnchorPoint
        Dim bottomPoint As VirtualPoint = sel.BottomPoint

        Dim trimmedString As String = sel.Text.TrimStart()
        Dim numChars As Integer = sel.Text.Length - trimmedString.Length
        Dim spaceString As String = sel.Text.Substring(0, numChars)

        objAnchor.CreateEditPoint().Insert(spaceString + startOfBlock + vbCrLf)
        bottomPoint.CreateEditPoint().Insert(spaceString + endOfBlock + vbCrLf)
        sel.Indent()

        'sel.LineUp()
        'sel.Text = "{" + vbCrLf + sel.Text + "}" + vbCrLf
    End Sub

 

다음은 이름과 날짜를 입력하는 예제입니다:

 

    ' Inserts name and date
    Sub Signature()
        Dim sel As TextSelection = DTE.ActiveDocument.Selection
        sel.Insert("// Seungwoo Oh ")
        sel.Insert(Format(Date.Today, "yyyy-MM-dd"))
        sel.Insert(".")
    End Sub

 

다음은 수평 라인 주석을 입력하는 예제입니다:

 

    Sub InsertHorizontalLine()
        Dim lineString = "//----------------------------------------------------------------------------" + vbCrLf
        Dim sel As TextSelection = DTE.ActiveDocument().Selection()
        sel.Text() = lineString
    End Sub

 

System.Guid 객체로 GUID를 생성할 수도 있습니다.

 

    REM Insert a new Guid at whatever the cursor has selected
    Sub InsertGuid()
        Dim ts As TextSelection = DTE.ActiveDocument.Selection
        Dim guid As System.Guid = System.Guid.NewGuid()
        ts.Insert(guid.ToString().ToUpper())
    End Sub


Conclusion

Visual Studio는 매우 융통성있는 매크로 언어를 제공합니다.

파일 및 프로젝트 작업시 IDE의 내부 지식은 상당히 효과적입니다.

 

 

출처: Visual Studio Macros for the Game Programmer by Mike Cline

블로그 이미지

맨오브파워

한계를 뛰어 넘어서..........

,