Customizing icon of commandbarbutton - Problems!

G

Guest

I am trying to customize the icon of a commandbarbutton, but can't seem to figure out how to do it. I have found some code samples, but they are not too helpful. Most refer to using the 'LoadPicture' function, but I don't have this function available. I am referencing the stdole class, but 'LoadPicture' is not a member of it.
I have a bitmap file that I want to set as the face of my commandbarbutton. Can anyone show me some code to do this? If you tell me to use the 'LoadPicture' function, then please also tell me where I am supposed to find this.

Thanks.
 
D

Dmitry Streblechenko

LoadPicture() is a VB function, if you are using a different language (what
is it?), OleCreatePictureIndirect() Win API function is the way to go.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Brian said:
I am trying to customize the icon of a commandbarbutton, but can't seem to
figure out how to do it. I have found some code samples, but they are not
too helpful. Most refer to using the 'LoadPicture' function, but I don't
have this function available. I am referencing the stdole class, but
'LoadPicture' is not a member of it.
I have a bitmap file that I want to set as the face of my
commandbarbutton. Can anyone show me some code to do this? If you tell me
to use the 'LoadPicture' function, then please also tell me where I am
supposed to find this.
 
G

Guest

I am working with VB.NET. I have not worked with Win API functions, can you give me an example? Do I add a reference to the Olepro32.dll library?
Thanks much.

----- Dmitry Streblechenko wrote: -----

LoadPicture() is a VB function, if you are using a different language (what
is it?), OleCreatePictureIndirect() Win API function is the way to go.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Brian said:
I am trying to customize the icon of a commandbarbutton, but can't seem to
figure out how to do it. I have found some code samples, but they are not
too helpful. Most refer to using the 'LoadPicture' function, but I don't
have this function available. I am referencing the stdole class, but
'LoadPicture' is not a member of it.
I have a bitmap file that I want to set as the face of my
commandbarbutton. Can anyone show me some code to do this? If you tell me
to use the 'LoadPicture' function, then please also tell me where I am
supposed to find this.
 
G

Guest

I found some example code, but am getting a 'Catastrophic failure' when I run it.
Here is my code

Dim WithEvents btnItemProfiler As CommandBarButto
...
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll"
(ByRef pPictDesc As PICTDESC, ByRef riid As Guid, ByVal fOwn As Integer,
<MarshalAs(UnmanagedType.IDispatch)> ByRef ppvObj As Object) As Intege

Private Structure PICTDES
Friend SizeOfStruct As Intege
Friend PicType As Intege
Friend Hbitmap As IntPt
Friend Hpal As IntPt
Friend Padding As Intege

Friend Sub New(ByVal hBmp As IntPtr
Me.SizeOfStruct = Marshal.SizeOf(Me.GetType
Me.picType =
Me.hbitmap = hBm
Me.hpal = IntPtr.Zer
Me.padding =
End Su
End Structur

Friend Shared Function GetIPictureDispFromHandle(ByVal hIntPtr As IntPtr) As stdole.IPictureDis
Dim objIPictureDisp As Objec

Dim objGuid As New Guid("00020400-0000-0000-C000-000000000046"
Dim iResult As Intege
Dim tPICTDESC As New PICTDESC(hIntPtr
iResult = OleCreatePictureIndirect(tPICTDESC, objGuid, 1, objIPictureDisp
Return CType(objIPictureDisp, stdole.IPictureDisp
End Functio

Private Sub m_olInspectors_NewInspector(ByVal Inspector As Outlook.Inspector) Handles m_olInspectors.NewInspecto
...
'Create command bar button on the new mail item that was opene
...
Dim bmp As System.Drawing.Bitmap = CType(System.Drawing.Bitmap.FromFile("C:\icon.bmp"), System.Drawing.Bitmap
Dim pic As stdole.IPictureDisp = GetIPictureDispFromHandle(bmp.GetHbitmap()
Me.btnItemProfiler.Picture = pi
End Su

The error that I get when I assign my 'pic' variable to my commandbarbutton is
'Catastrophic failure

Any idea what I am doing wrong?
Thanks.
 
D

Dmitry Streblechenko

I don't know if it'll work in VB.Net (which I don't use) -
CommandBarButton.Picture must be set by reference, so try to change the line
Me.btnItemProfiler.Picture = pic
to
Set Me.btnItemProfiler.Picture = pic

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool


Jessica K said:
I found some example code, but am getting a 'Catastrophic failure' when I run it.
Here is my code:

Dim WithEvents btnItemProfiler As CommandBarButton
...
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" _
(ByRef pPictDesc As PICTDESC, ByRef riid As Guid, ByVal fOwn As Integer, _
<MarshalAs(UnmanagedType.IDispatch)> ByRef ppvObj As Object) As Integer

Private Structure PICTDESC
Friend SizeOfStruct As Integer
Friend PicType As Integer
Friend Hbitmap As IntPtr
Friend Hpal As IntPtr
Friend Padding As Integer

Friend Sub New(ByVal hBmp As IntPtr)
Me.SizeOfStruct = Marshal.SizeOf(Me.GetType)
Me.picType = 1
Me.hbitmap = hBmp
Me.hpal = IntPtr.Zero
Me.padding = 0
End Sub
End Structure

Friend Shared Function GetIPictureDispFromHandle(ByVal hIntPtr As IntPtr) As stdole.IPictureDisp
Dim objIPictureDisp As Object

Dim objGuid As New Guid("00020400-0000-0000-C000-000000000046")
Dim iResult As Integer
Dim tPICTDESC As New PICTDESC(hIntPtr)
iResult = OleCreatePictureIndirect(tPICTDESC, objGuid, 1, objIPictureDisp)
Return CType(objIPictureDisp, stdole.IPictureDisp)
End Function

Private Sub m_olInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector) Handles m_olInspectors.NewInspector
...
'Create command bar button on the new mail item that was opened
...
Dim bmp As System.Drawing.Bitmap =
CType(System.Drawing.Bitmap.FromFile("C:\icon.bmp"), System.Drawing.Bitmap)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top