Setting Up the Menus

 < Free Open Study > 



Setting Up the Toolbar

The NET Desktop add-in is going to have its own toolbar. The toolbar will have several tool buttons on it. Each tool button will have its own drop-down menu. To set up the toolbar, you will add some new code to the OnConnection method of the Connect class. This code will make calls on methods to create the toolbar and to add the tool buttons to the toolbar. Listing 12-2 shows the updated OnConnection method.

Listing 12-2: OnConnection Method Updated to Create a Toolbar

start example
 Public Sub OnConnection(ByVal application As Object, _    ByVal connectMode As Extensibility.ext_ConnectMode, _    ByVal addInInst As Object, ByRef custom As System.Array) _    Implements Extensibility.IDTExtensibility2.OnConnection    oVB = CType(application, EnvDTE.DTE)    addInInstance = CType(addInInst, EnvDTE.AddIn)    Try       oUI = New CUIToolbar()       ' load the form with the pictures on it       frm.Show()       frm.Hide()       System.Windows.Forms.Application.DoEvents()       ' create the add-in's toolbar       CreateOfficeToolBar()       ' Add the tool buttons to the add-in toolbar       CreateOfficeToolBarButtons()       ' don't need the form anymore, destroy it       frm.Dispose()       Catch e As System.Exception          MsgBox(e.Message)       End Try    End Sub 
end example

Several lines of code have been added to the method. This code, which is highlighted in boldface font, accomplishes the following things:

  • Instantiates an instance of the CUserInterface class.

  • Loads a form that has the images in it. It is immediately hidden because it is used only to retrieve images.

  • Creates the toolbar by calling CreateOfficeToolBar.

  • Creates the tool buttons by calling CreateOfficeToolBarButtons.

  • Disposes of the image form as it is no longer needed and adds a call to the DoEvents method so that the form will have time to unload before execution resumes.

The frm referenced in the code in Listing 12-2 has been dimensioned in the declarations section of the Connect class with the following line of code:

 Dim frm As New frmPictures() 

Listing 12-3 shows the code for the methods that create the toolbar and the associated tool buttons. These two methods will call methods of the CUserInterface class to do the actual calls to the Microsoft Office Command methods.

Listing 12-3: Creating the Toolbar and Tool Buttons

start example
 Private Sub CreateOfficeToolBar()    ' This method creates the office toolbar    TBar = oUI.AddOfficeToolBar(oVB, _                            "NETDesktopTBar", _                            False) End Sub Private Sub CreateOfficeToolBarButtons()    ' This method calls the low level method that    ' adds the tool buttons to the toolbar    ' The paired commands do two things.    ' 1) create the toolbar or button    ' 2) link the command event to the event handler.    Try       ' note that the bitmap can come from a variety       ' of places. Here it is pulled from an imagelist       mcbComment = oUI.AddOfficeToolBarButton(oVB, _          TBar, "Comment Menus", frm.ImageList1.Images(78))       mnuCommentHandler = _          CType(oVB.Events.CommandBarEvents(mcbComment), _          EnvDTE.CommandBarEvents)       mcbWindows = oUI.AddOfficeToolBarButton(oVB, _          TBar, "Windows Menu", frm.ImageList1.Images(81))       mnuWindowsHandler = _          CType(oVB.Events.CommandBarEvents(mcbWindows), _       EnvDTE.CommandBarEvents)       mcbDesigner = oUI.AddOfficeToolBarButton(oVB, _          TBar, "Designers", frm.ImageList1.Images(80))       mnuDesignerHandler = _          CType(oVB.Events.CommandBarEvents(mcbDesigner), _          EnvDTE.CommandBarEvents)       mcbSetupAbout = oUI.AddOfficeToolBarIconAndCaption(oVB, _          TBar, "About NET Desktop", frm.ImageList1.Images(90))       mnuSetupAboutHandler = _          CType(oVB.Events.CommandBarEvents(mcbSetupAbout), _          EnvDTE.CommandBarEvents)    Catch e As System.Exception       MsgBox(e.Message)    End Try End Sub 
end example

The CreateOfficeToolBar method will cause a completely new toolbar to be added to the IDE. This is called an add-in toolbar, and it will house the four tool buttons that will be created by the CreateOfficeToolBarButtons method.

I have created two new classes that contain the methods for creating Microsoft Office toolbars, tool buttons, menus, and pop-up menus. I have placed the methods for creating toolbars and tool buttons in a class called CUIToolBar. This class is shown in Listing 12-4. The CUIToolBar class has three methods. The first method, AddOfficeToolBar, creates the add-in toolbar. The other two methods create the two different types of tool buttons used in this add-in. The three icon buttons are created using the AddOfficeToolBarButton method. This type of button contains an icon and displays a tool tip if the mouse pointer is held over it. The fourth tool button is called an icon and caption–type tool button. It not only displays an icon, but it also displays a caption. I like to use this type of button to display the About box for the add-in. The AddOfficeToolBarIconAndCaption method is used to create this type of tool button.

Listing 12-4: CUIToolBar Class

start example
 Imports Microsoft.Office.Core Imports Extensibility Imports System.Runtime.InteropServices Imports EnvDTE Imports System.Windows.Forms Public Class CUIToolBar    Public Function AddOfficeToolBar(ByVal VBE As EnvDTE.DTE, _       ByVal Caption As String, _       Optional ByVal Floating As Boolean = False) _       As Microsoft.Office.Core.CommandBar       ' This method adds an office commandbar (toolbar) to       ' the IDE. It will become the container for command buttons.       Dim Kind As Byte       Dim toolBar As Microsoft.Office.Core.CommandBar       Try       ' Set parameter for pos argument:       If Floating Then          Kind = _             Microsoft.Office.Core.MsoBarPosition.msoBarFloating       Else          Kind = Microsoft.Office.Core.MsoBarPosition.msoBarTop       End If       ' Add custom toolbar and display it:       toolBar = VBE.CommandBars.Add(Name:=Caption, _                                     Position:=Kind, _                                     Temporary:=True)       toolBar.Visible = True       Return toolBar    Catch e As System.Exception       Return toolBar    End Try End Function Public Function AddOfficeToolBarButton(    ByVal VBE As EnvDTE.DTE, _    ByVal ToolBar As Microsoft.Office.Core.CommandBar, _    ByVal Caption As String, _    ByVal Bitmap As Object, _    Optional ByVal pos As Byte = 0, _    Optional ByVal sep As Boolean = False) _    As Microsoft.Office.Core.CommandBarControl    ' Variables:    Dim cmdBtn As Microsoft.Office.Core.CommandBarControl    Try       If Caption = "" Or Bitmap Is Nothing Then Exit Function       Clipboard.SetDataObject(Bitmap)       ' Add button to Visual Studio IDE toolbar       If pos = 0 Then pos = ToolBar.Controls.Count + 1       cmdBtn = _           ToolBar.Controls.Add(Type:=Microsoft.Office.Core.           MsoControlType.msoControlButton, _                                         Before:=pos, _                                         Temporary:=True)       ' Set properties of button       cmdBtn.Caption = Caption       If sep Then cmdBtn.BeginGroup = True       cmdBtn.Style = _          Microsoft.Office.Core.MsoButtonStyle.msoButtonIcon       cmdBtn.PasteFace()       Return cmdBtn    Catch e As System.Exception       Return cmdBtn    End Try End Function Public Function AddOfficeToolBarIconAndCaption(    ByVal VBE As EnvDTE.DTE, _    ByVal ToolBar As Microsoft.Office.Core.CommandBar, _    ByVal Caption As String, _    ByVal Bitmap As Object, _    Optional ByVal pos As Byte = 0, _    Optional ByVal sep As Boolean = False) _    As Microsoft.Office.Core.CommandBarControl    Dim cmdBtn As Microsoft.Office.Core.CommandBarControl    Try       If Caption = "" Or Bitmap Is Nothing Then Exit Function       Clipboard.SetDataObject(Bitmap)       ' Add button to VB toolbar:       If pos = 0 Then pos = ToolBar.Controls.Count + 1          cmdBtn = _             ToolBar.Controls.Add(Type:=Microsoft.Office.Core.             MsoControlType.msoControlButton, _                                        Before:=pos, _                                        Temporary:=True)          ' Set properties of button:          cmdBtn.Caption = Caption          If sep Then cmdBtn.BeginGroup = True          cmdBtn.Style = _             Microsoft.Office.Core.MsoButtonStyle.             msoButtonIconAndCaption          cmdBtn.PasteFace()          Return cmdBtn       Catch e As System.Exception          Return cmdBtn       End Try    End Function End Class 
end example

The methods to create Microsoft Office menus and pop-up menus will not be used in this add-in, and I do not show the code for them in this chapter. I have placed the code for creating these type menus in a class named CUIMenus. This class is in the add-in and you can view it in the code for this chapter that is available for download from the Apress Web site (http://www.apress.com).

Toolbar Event Handlers

In addition to declaring and associating event handlers for each of the tool buttons, you must also create the event handlers. Listing 12-5 shows the code for the event handlers. Although there are four tool buttons, there is only one event handler. You are able to handle all of the tool button Click events with one event handler because of the Handles keyword. Regardless of the tool button that is clicked, the mnuCommentHandler Click event will receive control. In this handler, you use a Select Case construct to determine which tool button was clicked by examining the CommandBarControl.Name property. This code will be placed at the end of the Connect class.

Listing 12-5: Tool Button Event Handler

start example
 Private Sub mnuCommentHandler_Click(ByVal CommandBarControl As Object, _    ByRef handled As Boolean, _    ByRef CancelDefault As Boolean) _    Handles mnuCommentHandler.Click, _             mnuWindowsHandler.Click, _             mnuDesignerHandler.Click, _             mnuSetupAboutHandler.Click    Dim oFH As New CMenuHandler()    Try       Select Case CommandBarControl.caption          Case "Comment Menu"             Dim oFRM As New frmComment(oVB)             oFH.LoadMenuForm(oVB, mcbComment, oFRM)          Case "Windows Menu"             Dim oFRM As New frmWindowsMenu(oVB)             oFH.LoadMenuForm(oVB, mcbWindows, ofrm)          Case "Designers"             Dim oFRM As New frmDesignerMenu(oVB)             oFH.LoadMenuForm(oVB, mcbDesigner, ofrm)          Case "About NET Desktop"             Dim oFRM As New frmAbout(oVB)             oFH.LoadMenuForm(oVB, mcbClone, oFRM)       End Select       handled = True    Catch e As System.Exception       MsgBox(e.Message)    End Try End Sub 
end example



 < Free Open Study > 



Writing Add-Ins for Visual Studio  .NET
Writing Add-Ins for Visual Studio .NET
ISBN: 1590590260
EAN: 2147483647
Year: 2002
Pages: 172
Authors: Les Smith

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net