How To Add a Transparent Icon to a Toolbar Button (260850)



The information in this article applies to:

  • Microsoft Outlook 97
  • Microsoft Outlook 98
  • Microsoft Outlook 2000
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q260850

SUMMARY

This article demonstrates how to use Command bars and WIN32 application programming interfaces (APIs) to add a transparent icon to a custom menu or toolbar command.

MORE INFORMATION

The following Visual Basic sample demonstrates how to convert an icon to a bitmap using the current user's system color settings for the background of the icon, and then add the bitmap to a custom menu command or toolbar button:
Public Declare Function DrawIconEx Lib "user32" 
     (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal 
     hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, 
     ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, 
     ByVal diFlags As Long) As Long
Public Declare Function CreateIC Lib "gdi32" Alias "CreateICA" 
     (ByVal lpDriverName As String, ByVal lpDeviceName As String, 
     ByVal lpOutput As String, ByVal lpInitData As String) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long

'The call to the below function adds an item to the Tools menu using a 
'bitmap in a resource file with the resource ID of 102. This ID number
'should be changed to match the ID of your bitmap if you are using
'resource file.
'All items added to resource files have a resource identifier number.

'You can replace the resource file with a bitmap from any source.

AddIconToCommandBar "Tools", 102

Public Sub AddIconToCommandBar(BarName As String, IconId As Integer)
Dim cbMenu As CommandBar
Dim cbButton As CommandBarButton
Dim oBMP As StdPicture
Dim oICO As StdPicture
 ' Set to the commandbar you want to add to:
 Set cbMenu = Application.CommandBars(BarName)
 If Not cbMenu Is Nothing Then
  Set cbButton = cbMenu.Controls.Add(msoControlButton)
  If Not cbButton Is Nothing Then
   ' Get the existing face.
   cbButton.CopyFace
   Set oBMP = Clipboard.GetData
   ' Load the icon from the resource file.
   Set oICO = LoadResPicture(IconId, vbResIcon)
   ' Copy the icon over the existing face to preserve the background color.

   CopyIconToBmp oICO, oBMP
   Clipboard.Clear
   Clipboard.SetData oBMP, ccCFBitmap
   Clipboard.SetData oBMP, ccCFDIB
   ' Draw the icon on the menu.
   cbButton.PasteFace
   'cbButton.BeginGroup = True
   cbButton.Caption = MENUCAPTION

  End If ' cbButton
 End If ' cbMenu
End Sub

Private Sub CopyIconToBmp(oIcon As StdPicture, oBMP As StdPicture)
Dim Rc As Long
Dim hdc As Long
Dim hdcMem As Long
Dim hBmOld As Long
 hdc = CreateIC("DISPLAY", vbNullChar, vbNullChar, vbNullChar)
 hdcMem = CreateCompatibleDC(hdc)
 hBmOld = SelectObject(hdcMem, oBMP.Handle)
 Rc = DrawIconEx(hdcMem, 0, 0, oIcon.Handle, 16, 16, 0, 0, DI_NORMAL)
 SelectObject hdcMem, hBmOld
 DeleteDC hdc
 DeleteDC hdcMem
End Sub
				

Modification Type:MinorLast Reviewed:6/29/2004
Keywords:kbhowto kbMsg kbOutlookObj KB260850