![]() |
||||
This article describes how to create custom pictures for Visual Studio .NET add-ins commands, buttons and toolwindows. More Information There are at least three cases in which you will need to create custom, transparent, pictures for your Visual Studio .NET add-in features. This article explains how to do it in each case: 1) Custom pictures for add-in commands When you call the Commands.AddNamedCommand function to create a command, if you pass the False value in the MSOButton parameter (the 5th), then the command will use a custom picture instead of a picture taken from Microsoft Office. The custom picture is specified through the Bitmap parameter (the 6th) which is an integer, and it indicates the resource ID of the bitmap in a satellite DLL. The satellite DLL can be of two kinds, depending on the type of the add-in (COM add-in, managed XML-based add-in)
Note: a COM add-in can't use managed (.NET) satellite DLLs and a XML add-in can't use native (Win32 Visual C++) satellite DLLs. 1.1) Creating a command with a custom picture using a native Win32 (Visual C++) satellite DLL for a COM-based Visual Studio add-in The steps are the following:
After all this, your commands will be created with a custom, transparent picture. If you need to reset your commands so they are created again, see HOWTO: Reset a Visual Studio add-in. 1.2) Creating a command with a custom picture using a managed (VB.NET / C#) satellite DLL for a XML-based Visual Studio add-in (Visual Studio 2005 or higher) See the article HOWTO: Create a command with a custom picture using a managed satellite DLL for a XML-based Visual Studio add-in.1.3) Creating a command with a custom picture without using a managed (VB.NET / C#) satellite DLL for a XML-based Visual Studio add-in (Visual Studio 2008 or higher) See the article HOWTO: Create a command with a custom picture without using a managed satellite DLL for a XML-based Visual Studio add-in.2) Custom pictures for add-in buttons without a command In the previous case we have seen how to create a command with a custom picture. CommandBarButtons created from this command (using the Command.AddControl method) will show that custom picture too, but sometimes you may need to create buttons that don't have a command behind, using the CommandBar.Controls.Add method instead. In this case, to set a custom, transparent, picture for the CommandBarControl created, you need to cast it to CommandBarButton and then set its Picture and Mask properties (in this order):
You can get an Stdole.IPictureDisp object from a managed System.Drawing.Image object using this class: Public Class ImageToPictureDispConverter
Inherits System.Windows.Forms.AxHost
Public Sub New()
MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
End Sub
Public Function GetIPictureDispFromImage(ByVal objImage As System.Drawing.Image) _
As stdole.IPictureDisp
Dim objPicture As stdole.IPictureDisp
objPicture = CType(MyBase.GetIPictureDispFromPicture(objImage), _
stdole.IPictureDisp)
Return objPicture
End Function
End Class
You use this class as follows: Dim objImageToPictureDispConverter As ImageToPictureDispConverter Dim objImage As System.Drawing.Image Dim objIPictureDisp As stdole.IPictureDisp objImage = (get the managed image from somewhere) objImageToPictureDispConverter = New ImageToPictureDispConverter() objIPictureDisp=objImageToPictureDispConverter.GetIPictureDispFromImage(objImage) objImageToPictureDispConverter.Dispose() Other ways of creating an Stdole.IPictureDisp instead of using a managed image is using a file on disk or a bitmap resource from a satellite DLL. In the former case you would use this function exported by oleaut32.dll: <DllImport("oleaut32.dll", =CharSet.Unicode, =True, ="OleLoadPictureFile")> _
Public Shared Sub OleLoadPictureFile(ByVal varFileName As Object,_
<MarshalAs(UnmanagedType.IDispatch)> ByRef lpIPictureDisp As Object)
End Sub
As you can see, the function receives a file name as first parameter, and it returns an IPictureDisp in the second parameter. In the latter case you can get a bitmap handle from a resource DLL handle using this function: <DllImport("user32.dll", ="LoadBitmapA")> _
Public Shared Function LoadBitmap(ByVal hDLLInstance As IntPtr, _
ByVal iBitmapIndex As Integer) As Integer
End Function
and then you would use the OleCreatePictureIndirect function to get an IPictureDisp object from the bitmap handle: Public 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
<DllImport("olepro32.dll", ="OleCreatePictureIndirect")> _
Public Shared Function OleCreatePictureIndirect(ByRef pPictDesc As PICTDESC, _
ByRef riid As Guid, ByVal fOwn As Integer, _
<MarshalAs(UnmanagedType.IDispatch)> ByRef ppvObj As Object) As Integer
End Function
That function is used as follows: Public Function GetIPictureDispFromBitmapHandle(ByVal hBitmapHandle As IntPtr) _
As stdole.IPictureDisp
Dim objPicture As Object
Dim objGuid As New Guid("00020400-0000-0000-C000-000000000046")
Dim iResult As Integer
Dim tPICTDESC As New PICTDESC(hBitmapHandle)
iResult = OleCreatePictureIndirect(tPICTDESC, objGuid, 1, objPicture)
Return CType(objPicture, stdole.IPictureDisp)
End Function
3) Custom pictures for toolwindows When you create a toolwindow using the Windows.CreateToolWindow method and the window is linked (docked) together with other toolwindows, its tab shows a picture. If you have not specified a picture, by default a Visual Studio picture logo is shown. You can specify a custom picture using the Window.SetTabPicture method, which must be called before setting the Visible property of the toolwindow to true. 3.1) Custom toolwindow picture for Visual Studio .NET 2002/2003
3.2) Custom toolwindow picture for Visual Studio 2005
3.2) Custom toolwindow picture for Visual Studio 2008 or higher
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
| Copyright © 2000-2013 MZTools Software. All Rights Reserved. Legal Notice |