Extending Code to Add a Custom Graphic to the Toolbar Button

You can extend this code to add a custom graphic rather than the built-in graphic (faceID 34). To do this, you first create two bitmap files. The first bitmap will contain the graphic you want to use. The second bitmap will contain the same graphic, but with a white background and with the entire foreground set to black. This second bitmap, known as a mask, indicates which area of the original graphic (the background) should be transparent so that it takes on the color of the toolbar. The first step is to add the bitmap files to your project.

1. Create a bitmap file similar to the one in Figure 8.2, and name it BoldItalic.bmp.

2. Create a second bitmap file with the graphic you just created, but with the entire foreground black and the background white. Name this graphic Mask.bmp.

3. Right-click the solution node in Solution Explorer, and click Properties.

4. On the Resources tab of the Property Pages, click the drop-down arrow next to the Add Resource button, and then click Add Existing File.

5. Navigate to BoldItalic.bmp, and click Open.

6. Add Mask.bmp as a second resource.

Figure 8.2. The BoldItalic.bmp and Mask.bmp files in Visual Studio

Now add code to retrieve this resource. You cannot assign a bitmap file directly to the Picture property of an Office button; instead, you must use an IPictureDisp interface. To convert your bitmap to the correct type, add a class to your project that converts a bitmap to an IPictureDisp.

1. Right-click Sheet1.vb in Solution Explorer, and click Code.

2. Add the class in Listing 8.3 after the Sheet1 class code (after the line End Class). The code in this class converts a bitmap to the type that is required for the Picture property of the button (IPictureDisp).

Listing 8.3. Adding a class to convert bitmaps to IPictureDisp ' Visual Basic 2005

Public Class BitmapToIPicture

Inherits System.Windows.Forms.AxHost

Public Sub New()

MyBase.New(Nothing) End Sub

Public Shared Function Convert(ByVal _

Image As System.Drawing.Image) As stdole.IPictureDisp

Convert = GetIPictureFromPicture(Image)

End Function

End Class

Next, you modify the code in the Startup event handler to add the Boldltalic bitmap to the button initially, and then add code to the ButtonClick method to change the button state according to the state of the formatting. The state determines whether the button has a border and is shaded orange (indicating that the button is pressed). Listing 8.4 shows this new code in bold. Also, notice that the code for setting the facelD has been commented out because you will use the Picture property instead.

Listing 8.4. Adding a custom picture to a toolbar button ' Visual Basic 2005

Dim commandBar As Office.CommandBar

WithEvents formatButton As Office.CommandBarButton

Private Sub Sheet1_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

' If the button already exists, remove it.

formatButton = Application.CommandBars("Formatting") _ .FindControl(Tag:="FormatButton")

If formatButton IsNot Nothing Then formatButton.Delete()

End If

Catch Ex As Exception


End Try commandBar = Application.CommandBars("Formatting") formatButton = CType(commandBar.Controls.Add(1), _ Office.CommandBarButton)

With formatButton

.Caption = "Bold and Italic" .Tag = "FormatButton" '.FaceId = 34

•Picture = BitmapToIPicture.Convert(My.Resources.BoldItalic) .Mask = BitmapToIPicture.Convert(My.Resources.Mask) .State = Microsoft.Office.Core.MsoButtonState.msoButtonUp

End With

End Sub

Private Sub formatButton_Click(ByVal Ctrl As _

Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault _ As Boolean) Handles formatButton.Click

Dim Selection As Object = Me.Application.Selection

If Selection.Font.Bold = True And _ Selection.Font.Italic = True Then formatButton.State = Microsoft.Office.Core _ .MsoButtonState.msoButtonUp

Selection.Font.Bold = False Selection.Font.Italic = False

Else formatButton.State = Microsoft.Office.Core _ .MsoButtonState.msoButtonDown

Selection.Font.Bold = True Selection.Font.Italic = True

End If

End Sub

You should also add code to the worksheet's SelectionChange event handler so that the button's correct state appears when you select text that is formatted with bold and italic versus when you select text that does not have this formatting applied. In other words, you want to mimic the behavior of the built-in italic and bold buttons in Excel. Right-click Sheetl.vb in Solution Explorer, select View Code, and then add the code in Listing 8.5.

Listing 8.5. Changing a custom picture on a toolbar button when the selection changes

Private Sub Sheet1_SelectionChange(ByVal Target As _ Microsoft.Office.Interop.Excel.Range) Handles _ Me.SelectionChange

If Target.Font.Bold = True And _ Target.Font.Italic = True Then formatButton.State = Microsoft.Office.Core _ .MsoButtonState.msoButtonDown

Else formatButton.State = Microsoft.Office.Core _ .MsoButtonState.msoButtonUp

End If End Sub

Now when you press F5, a new BoldItalic button with a custom picture (BoldItalic.bmp) is added to the toolbar, and the button's state is updated depending on the formatting that is applied to the selected cells. Figure 8.3 shows the button state when the selected text has bold and italic applied.

Figure 8.3. The state of the BoldItalic button is set to msoButtonDown when selected text is bold and italic.

Figure 8.4 shows the button's state when the selected text does not have bold and italic formatting applied. This behavior matches Excel's default behavior for toggle buttons on the toolbar.

When you run this code and apply bold and italic formatting to a cell, you'll see that the state of the new button matches the state of the standard Bold and Italic buttons; this is the way you can customize any button that you want to act as a toggle button. For buttons that perform only one action (rather than having on and off states), you would not need this code. To see a Word example or to learn how to override a built-in Word command, see Chapter 6.

Figure 8.4. The state of the BoldItalic button is set to msoButtonUp when selected text is not formatted.
0 0

Post a comment