如何从VS2010中的宏更改工具栏上的命令标题?

时间:2011-10-27 22:35:05

标签: vb.net visual-studio-2010 macros vs-extensibility

从宏我正在访问工具栏上的命令:

Dim name As String = "Macros.MyMacros.MyMacros.ToggleExceptions"
Dim cmd As EnvDTE.Command = DTE.Commands.Item(name)

我现在如何更改工具栏上命令的标题?它似乎没有必要的属性。我是否需要将其转换为其他内容?

1 个答案:

答案 0 :(得分:3)

我已经实现了它:

Private Sub Main()
    Const BAR_NAME As String = "MenuBar"
    Const CTL_NAME = "Foo"

    ChangeCommandCaption(BAR_NAME, CTL_NAME, "Bar")
End Sub

Private Sub ChangeCommandCaption(ByVal cmdBarName As String, ByVal ctlName As String, ByVal caption As String)
    Dim bars As Microsoft.VisualStudio.CommandBars.CommandBars

    bars = DirectCast(DTE.CommandBars, Microsoft.VisualStudio.CommandBars.CommandBars)
    If bars Is DBNull.Value Then Exit Sub

    Dim menuBar As CommandBar = bars.Item(cmdBarName)
    If menuBar Is DBNull.Value Then Exit Sub

    Dim cmdBarCtl As CommandBarControl

    Try
        cmdBarCtl = menuBar.Controls.Item(ctlName)
        If cmdBarCtl Is DBNull.Value Then Exit Sub
    Catch ex As Exception
        Exit Sub
    End Try

    cmdBarCtl.Caption = caption
End Sub