Microsoft KB Archive/260850

= How To Add a Transparent Icon to a Toolbar Button =

Article ID: 260850

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Outlook 97 Standard Edition
 * Microsoft Outlook 98 Standard Edition
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



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 &quot;user32&quot; (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 &quot;gdi32&quot; Alias &quot;CreateICA&quot; (ByVal lpDriverName As String, ByVal lpDeviceName As String,     ByVal lpOutput As String, ByVal lpInitData As String) As Long Public Declare Function CreateCompatibleDC Lib &quot;gdi32&quot; (ByVal hdc As Long) As Long Public Declare Function DeleteDC Lib &quot;gdi32&quot; (ByVal hdc As Long) As Long Public Declare Function SelectObject Lib &quot;gdi32&quot; (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 &quot;Tools&quot;, 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(&quot;DISPLAY&quot;, 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

Keywords: kbhowto kbmsg kboutlookobj KB260850

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.