Word Hacks [Electronic resources]

Andrew Savikas

نسخه متنی -صفحه : 162/ 32
نمايش فراداده

Hack 11 Create a Custom Button Image

Customize your controls with any image you can bitmapeven a favorite photo.

Putting a company logo or even a picture onto a toolbar button can give your Word workspace a truly custom look. Turning any graphic into a toolbar button is easier than it sounds.

The image must be a .bmp, .jpg, or .gif file. The optimal dimensions follow:

16 16 pixels

24-bit color depth

Word will scale oversized or not perfectly square images, but you'll be more satisfied with the results if you scale and crop the image yourself.

If you want your image to appear cut out, like most of the Word button images, you'll also need a mask for your image. The mask is a second image that defines the boundaries of the first. To create the mask, blacken the area of the 16 16 square occupied by your image and leave the rest white. The white area of your mask will be filled in by the toolbar color. You can use an image-processing program such as PhotoShop to do this, but many other programs, including SnagIt from TechSmith (http://www.techsmith.com), offer inexpensive image-editing tools.

For example, to create a button image using the animal shown in Figure 2-29, first scale it down to 16 16 pixels and then create a mask, as shown (and magnified considerably) in Figure 2-30.

Figure 2-29. The base image for a new toolbar button

Figure 2-30. The "mask" image for a toolbar button

2.10.1 The Code

The following macro creates a new floating toolbar with just one button, containing our sample image. From there, you can use the Copy Button Image and Paste Button Image commands to place the image as needed.

Sub DisplayNewImage( )
Dim cbar As CommandBar
Dim cbarctrl As CommandBarControl
Dim pImage As IPictureDisp
Dim pMask As IPictureDisp
Dim sImageFile as String
Dim sMaskFile as String
sImageFile = "C:\Documents and Settings\My Documents\tarsier.bmp"
sMaskFile = "C:\Documents and Settings\My Documents\mask.bmp"
Set cbar = CommandBars.Add(Name:="My Picture", Position:=msoBarFloating)
Set cbarctrl = cbar.Controls.Add(Type:=msoControlButton)
Set pImage = stdole.StdFunctions.LoadPicture(sImageFile)
Set pMask = stdole.StdFunctions.LoadPicture(sMaskFile)
cbarctrl.Picture = pImage
cbarctrl.Mask = pMask
cbar.visible = True
End Sub

Running the macro produces the toolbar shown in Figure 2-31.

Figure 2-31. A button image created from a bitmapped image

2.10.2 Hacking the Hack

If you'd rather hack on the existing buttons, you can capture and save the button images to file.

The following macro goes through every button on all the toolbars and saves the images and their masks to a folder called C:\Buttons, which you should create before running the macro. Each file is named using the control's ID and saved as a .bmp file.

Sub GetButtonImageAndMask( )
Dim cbar As CommandBar
Dim cbarctrl As CommandBarControl
For Each cbar In Application.CommandBars
For Each cbarctrl In cbar.Controls
If cbarctrl.Type = msoControlButton Then
stdole.SavePicture cbarctrl.Picture, _
"c:\buttons\" & cbarctrl.ID & "_img.bmp"
stdole.SavePicture cbarctrl.Mask, _
"c:\buttons\" & cbarctrl.ID & "_mask.bmp"
End If
Next cbarctrl
Next cbar
End Sub

Once you've got the bitmap files, you can open and edit them as shown in Figure 2-32 (a screen shot from the SnagIt program mentioned earlier).

Figure 2-32. After saving the button images as files, you can edit them with an image-editing software program