방법: 매크로를 사용하여 이벤트 처리

새 매크로를 만들면 EnvironmentEvents라는 모듈이 기본적으로 추가됩니다. 이 모듈은 자동화 이벤트에 응답에 나와 있는 테이블에서 설명하는 여러 가지 이벤트 개체를 미리 정의하고 있으므로 사용자가 직접 이를 정의할 필요가 없습니다. EnvironmentEvents 모듈에 없는 이벤트를 처리하려면 해당 이벤트를 추가하면 됩니다. 이를 수행하는 방법에 대한 자세한 내용은 방법: 매크로에서 환경 이벤트 처리를 참조하십시오.

아래 절차에서는 도구 창(이 경우 작업 목록)과 관련된 이벤트를 처리하기 위해 매크로를 사용하는 방법을 보여 주니다.

참고

표시되는 대화 상자와 메뉴 명령은 활성 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 설정에 대한 작업을 참조하십시오.

도구 창과 관련된 이벤트를 처리하려면

  1. 매크로 IDE에서 EnvironmentEvents 모듈에 다음 코드를 추가합니다.

  2. 매크로를 실행합니다.

    매크로가 작업 목록에서 항목을 추가하거나 제거할 때 작업 목록 이벤트가 처리됩니다.

예제

다음 매크로 예제에서는 Visual Studio 자동화 모델 이벤트 개체를 사용하여 이벤트에 응답하는 방법을 보여 줍니다. 이 예제는 이벤트 처리기의 이벤트에 대한 응답으로 작업 목록에서 작업을 추가 또는 제거합니다.

' Macro code.
Public Module EnvironmentEvents

   Public Sub TaskListEvents_TaskAdded(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskAdded
      MsgBox("A task named '" & TaskItem.Description & "' was added _
      to the Task List.")
      ' Put other code here that you want to execute when this 
      ' event occurs.
   End Sub

   Public Sub TaskListEvents_TaskRemoved(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskRemoved
      MsgBox("A task named '" & TaskItem.Description & "' was _
      removed from the Task List.")
      ' Put other code here that you want to execute when this 
      ' event occurs.
   End Sub
End Module

Sub EventsExample()
    ' Add items to and remove items from the Task List.
    Dim TL As TaskList = dte.ToolWindows.TaskList
    Dim TLItem As taskitem

    ' Add a couple of tasks to the Task List.
    TLItem = TL.TaskItems.Add(" ", " ", "Test task 1.", _
    vsTaskPriority.vsTaskPriorityHigh, vsTaskIcon.vsTaskIconUser, _
    True, , 10, , )
    TLItem = TL.TaskItems.Add(" ", " ", "Test task 2.", _
    vsTaskPriority.vsTaskPriorityLow, vsTaskIcon.vsTaskIconComment, _
    , , 20, , )

    ' List the total number of task list items after adding the new 
    ' task items.
    MsgBox("Task Item 1 description: " & _
    TL.TaskItems.Item(2).Description)
    MsgBox("Total number of task items: " & TL.TaskItems.Count)

    ' Remove the second task item. The items list in reverse numeric 
    ' order.
    MsgBox("Deleting the second task item")
    TL.TaskItems.Item(1).Delete()
    MsgBox("Total number of task items: " & TL.TaskItems.Count)
End Sub

추가 기능에서 이벤트에 응답하려면 OnConnectionMethod 이벤트에서 이벤트 처리기를 초기화합니다. 예를 들면 다음과 같습니다.

Public Class Class1
   Implements IDTExtensibility2
   Dim objEventsClass As EventsClass

   Public Sub OnConnection(ByVal Application As Object, ByVal _
   ConnectMode As EnvDTE.ext_ConnectMode, ByVal AddInInst As _
   Object, ByRef custom() As Object) Implements _
   EnvDTE.IDTExtensibility2.OnConnection
      objEventsClass = New EventsClass()
      objEventsClass.TaskListEvents = _
      _applicationObj.Events.TaskListEvents
   End Sub
End Class

Public Class EventsClass
   Public WithEvents TaskListEvents As EnvDTE.TaskListEvents

   Private Sub TaskListEvents_TaskAdded(ByVal TaskItem As _
   EnvDTE.TaskItem) Handles TaskListEvents.TaskAdded
      MsgBox("A task named '" & TaskItem.Description & "' was added _
      to the Task List.")
      ' Put any other code here that you want to execute when this 
      ' event occurs.
   End Sub
End Class

참고 항목

기타 리소스

이벤트에 응답(Visual Basic 및 Visual C# 프로젝트)