Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module Chapter07

    Sub WalkCommands()
        Dim cmd As EnvDTE.Command
        For Each cmd In DTE.Commands
            'use the EnvDTE.Command object here
        Next
    End Sub

    Sub FindFileOpenCommand()
        Dim cmd As EnvDTE.Command
        cmd = DTE.Commands.Item("{5EFC7975-14BC-11CF-9B2B-00AA00573819}", 222)
    End Sub

    Sub FindFileOpenCommandByName()
        Dim command As EnvDTE.Command
        command = DTE.Commands.Item("File.OpenFile")
    End Sub

    Sub FindGuidIDPair()
        Dim guid As String
        Dim id As Integer
        Dim command As EnvDTE.Command
        command = DTE.Commands.Item("File.OpenFile")
        guid = command.Guid
        id = command.ID
        MsgBox(guid + ", " + id.ToString())
    End Sub

    Sub CreateCommandList()
        Dim command As EnvDTE.Command
        Dim output As New InsideVSNET.Utilities.OutputWindowPaneEx(DTE, "Create Command List")

        For Each command In DTE.Commands
            If (command.Name <> Nothing) Then
                output.WriteLine(command.Name)
            End If
        Next
    End Sub

    Sub RunFileOpenCommand()
        DTE.ExecuteCommand("File.OpenFile")
    End Sub

    Sub RunCommand()
        DTE.ExecuteCommand("Macros.Samples.Utilities.TurnOnLineNumbers")
    End Sub

    Sub CheckAvailability()
        If (DTE.Commands.Item("Build.BuildSolution").IsAvailable = True) Then
            DTE.ExecuteCommand("Build.BuildSolution")
        End If
    End Sub

    Sub GetCommandBars()
        Dim commandBars As Microsoft.VisualStudio.CommandBars.CommandBars
        commandBars = DTE.CommandBars
    End Sub

    Sub GetCommandBarButton()
        Dim commandBarBtn As Microsoft.VisualStudio.CommandBars.CommandBarButton
        Dim commandBarCtl As Microsoft.VisualStudio.CommandBars.CommandBarControl
        Dim commandBarCtls As Microsoft.VisualStudio.CommandBars.CommandBarControls

        'Find the View command bar
        commandBarCtls = DTE.CommandBars.Item("View").Controls
        'Retrieve the first control on the menu
        commandBarCtl = commandBarCtls.Item(1)
        'Convert the CommandBarControl to a CommandBarButton object
        commandBarBtn = CType(commandBarCtl, _
            Microsoft.VisualStudio.CommandBars.CommandBarButton)
        MsgBox(commandBarBtn.Caption)
    End Sub

    Sub GetCommandBarPopup()
        Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
        Dim cmdBarControl As Microsoft.VisualStudio.CommandBars.CommandBarControl
        Dim cmdBarPopup As Microsoft.VisualStudio.CommandBars.CommandBarPopup

        'Find the "Standard" command bar
        commandBar = DTE.CommandBars.Item("Standard")
        'Find the first control on the command bar
        cmdBarControl = commandBar.Controls.Item(1)
        'Convert the CommandBarControl to a CommandBarPopup
        cmdBarPopup = CType(cmdBarControl, _
            Microsoft.VisualStudio.CommandBars.CommandBarPopup)
        MsgBox(cmdBarPopup.Controls.Item(1).Caption)
    End Sub

    Sub GetView()
        Dim cmdbars As Microsoft.VisualStudio.CommandBars.CommandBars
        Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar

        cmdbars = DTE.CommandBars
        commandBar = cmdbars.Item("View")
    End Sub

    Sub GetMenuCommandBar()
        Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar
        Dim cmdBarControl As Microsoft.VisualStudio.CommandBars.CommandBarControl
        Dim cmdBarPopupView As Microsoft.VisualStudio.CommandBars.CommandBarPopup
        Dim cmdBarView As Microsoft.VisualStudio.CommandBars.CommandBar

        'Retrieve the MenuBar command bar
        commandBar = DTE.CommandBars.Item("MenuBar")
        'Find the View menu
        cmdBarControl = commandBar.Controls.Item("Ansicht")
        'Convert to a CommandBarPopup
        cmdBarPopupView = CType(cmdBarControl, _
            Microsoft.VisualStudio.CommandBars.CommandBarPopup)
        'Get the CommandBar object for the view menu
        cmdBarView = cmdBarPopupView.CommandBar
        MsgBox(cmdBarView.Name)
    End Sub

    Sub AddControl()
        Dim command As EnvDTE.Command
        Dim commandBar As Microsoft.VisualStudio.CommandBars.CommandBar

        'Find the File.OpenFile command
        command = DTE.Commands.Item("File.OpenFile")
        'Find the Tools CommandBar
        commandBar = DTE.CommandBars.Item("Tools")
        'Add a control to the Tools menu that when 
        ' clicked will invoke the File.OpenFile command
        command.AddControl(commandBar, commandBar.Controls.Count + 1)
    End Sub

End Module


Namespace InsideVSNET
    Namespace Utilities

        Class OutputWindowPaneEx
            Dim pane As OutputWindowPane
            Dim dte As EnvDTE.DTE

            Public Sub New(ByVal application As _DTE)
                dte = application
                InitPane("(Default)")
            End Sub

            Public Sub New(ByVal application As _DTE, ByVal windowName As String)
                dte = application
                InitPane(windowName)
            End Sub

            Private Sub InitPane(ByVal windowName As String)
                Dim win As Window = dte.Windows.Item(Constants.vsWindowKindOutput)
                Dim window As OutputWindow = CType(win.Object, OutputWindow)

                Try
                    pane = window.OutputWindowPanes.Item(windowName)
                Catch
                    pane = window.OutputWindowPanes.Add(windowName)
                End Try
            End Sub

            Public Sub WriteLine()
                pane.OutputString(vbLf)
            End Sub

            Public Sub WriteLine(ByVal format As String, ByVal args As Object())
                pane.OutputString(String.Format(format, args) + vbLf)
            End Sub

            Public Sub WriteLine(ByVal text As String)
                pane.OutputString(text + vbLf)
            End Sub

            Public Sub WriteLine(ByVal obj As Object)
                pane.OutputString(obj.ToString() + vbLf)
            End Sub

            Public Sub Write(ByVal format As String, ByVal args As Object())
                pane.OutputString(String.Format(format, args))
            End Sub

            Public Sub Write(ByVal text As String)
                pane.OutputString(Text)
            End Sub

            Public Sub Write(ByVal obj As Object)
                pane.OutputString(obj.ToString())
            End Sub

        End Class

    End Namespace

End Namespace