Option Explicit On Option Strict Off Option Compare Text '============================================================================================================= ' ' cStdOle.vb ' ---------- ' ' Created By : Kevin Wilson ' http://www.TheVBZone.com ( The VB Zone ) ' http://www.TheVBZone.net ( The VB Zone .NET ) ' ' Created On : September 13, 2002 ' Last Update : September 13, 2002 ' ' VB Versions : VB.NET 1.1 (VS.NET 2003) ' (Reference System.Drawing.dll) ' (Reference System.Windows.Forms.dll) ' ' Requires : The .NET Framework v1.1 ' ' Description : This module makes it easy to convert back and forth between the OLE Pictures, Fonts, and Colors ' used in Visual Basic 5/6 and the Pictures, Fonts, and Colors used in VB.NET. You can keep using ' the old style Pictures, Fonts, and Colors if you include the file "STDOLE2.TLB" as a reference ' to your project, but using this module makes it so you don't have to include that file. ' ' NOTE : If the VB.NET development environment gives you an error while trying to open this file, simply ' select it in the "Solution Explorer" and right click on it and select "View Code". You can also ' type "cStdOle.ConvertFrom_OleColor" somewhere in your project and right click on that ' and select "Go To Definition" ' ' Example Use : ' ' Dim objCursor As Cursor ' Dim objIcon As Icon ' Dim objBitmap As Bitmap ' Dim strPath As String ' Dim objPic As Object ' Dim objCur As Object ' Dim objCur1 As Object ' strPath = Application.ExecutablePath ' If strPath.EndsWith("\") = True Then strPath = strPath.Substring(Len(strPath) - 1) ' strPath = strPath & "\Images\TEST.BMP" ' If Dir(strPath, FileAttribute.Archive Or FileAttribute.Hidden Or FileAttribute.Normal Or FileAttribute.ReadOnly Or FileAttribute.System) = "" Then Exit Sub ' objBitmap = Bitmap.FromFile(strPath) ' If Not objBitmap Is Nothing Then ' objCursor = CreateCursorFromBMP(objBitmap) ' objIcon = CreateIconFromBMP(objBitmap) ' If Not objCursor Is Nothing Then picDisplay1.Cursor = objCursor ' If Not objIcon Is Nothing Then picDisplay1.Image = objIcon.ToBitmap ' Me.Icon = objIcon ' objPic = cStdOle.ConvertTo_StdPicture(picDisplay1.Image) ' picDisplay2.Image = Bitmap.FromHbitmap(New System.IntPtr(CInt(objPic.Handle))) ' objCur1 = cStdOle.ConvertTo_StdPicture(Me.Cursor) ' objCur = cStdOle.ConvertFrom_StdPicture(objCur1, cStdOle.ReturnTypes.rt_Cursor) ' picDisplay2.Cursor = objCur ' objPic = Nothing ' objCur = Nothing ' objCur1 = Nothing ' End If ' '------------------------------------------------------------------------------------------------------------- ' ' "IPictureDisp" object has the following: ' [Handle] As Integer ' Height As Integer ' hPal As Integer ' Render Sub Render(ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal xSrc As Integer, ByVal ySrc As Integer, ByVal cxSrc As Integer, ByVal cySrc As Integer, ByVal prcWBounds As IntPtr) ' Type As Short ' Width As Integer ' '"IFontDisp" object has the following: ' Bold As Boolean ' Charset As Short ' Italic As Boolean ' [Name] As String ' Size As Decimal ' Strikethrough As Boolean ' Underline As Boolean ' Weight As Short ' '============================================================================================================= ' ' LEGAL: ' ' You are free to use this code as long as you keep the above heading information intact and unchanged. Credit ' given where credit is due. Also, it is not required, but it would be appreciated if you would mention ' somewhere in your compiled program that that your program makes use of code written and distributed by ' Kevin Wilson (www.TheVBZone.com). Feel free to link to this code via your web site or articles. ' ' You may NOT take this code and pass it off as your own. You may NOT distribute this code on your own server ' or web site. You may NOT take code created by Kevin Wilson (www.TheVBZone.com) and use it to create products, ' utilities, or applications that directly compete with products, utilities, and applications created by Kevin ' Wilson, TheVBZone.com, or Wilson Media. You may NOT take this code and sell it for profit without first ' obtaining the written consent of the author Kevin Wilson. ' ' These conditions are subject to change at the discretion of the owner Kevin Wilson at any time without ' warning or notice. Copyright© by Kevin Wilson. All rights reserved. ' '============================================================================================================= Imports System.Drawing Imports System.Windows.Forms Class cStdOle Inherits System.Windows.Forms.AxHost #Region "Public Enumerations" Public Enum PictureTypeConstants vbPicTypeNone = 0 vbPicTypeBitmap = 1 vbPicTypeMetafile = 2 vbPicTypeIcon = 3 vbPicTypeEMetafile = 4 End Enum Public Enum ReturnTypes rt_Image = 0 rt_Bitmap = 1 rt_Icon = 2 rt_Cursor = 3 rt_Metafile = 4 End Enum #End Region #Region "Constructors" Public Sub New() MyBase.New("59EE46BA-677D-4D20-BF10-8D8067CB8B33") End Sub #End Region #Region "Destructors" Protected Overrides Sub Finalize() MyBase.Finalize() End Sub #End Region #Region "Public Methods" Public Shared Function ConvertFrom_OleColor(ByVal intColor As UInt32) As Color On Error Resume Next Return AxHost.GetColorFromOleColor(intColor) Err.Clear() End Function Public Shared Function ConvertTo_OleColor(ByVal objColor As Color) As UInt32 On Error Resume Next Return AxHost.GetOleColorFromColor(objColor) Err.Clear() End Function Public Shared Function ConvertTo_StdFont(ByRef objFont As Font) As Object On Error Resume Next If objFont Is Nothing Then Exit Function ConvertTo_StdFont = AxHost.GetIFontFromFont(objFont) Err.Clear() End Function Public Shared Function ConvertFrom_StdFont(ByRef objIFont As Object) As Font On Error Resume Next If objIFont Is Nothing Then Exit Function ConvertFrom_StdFont = AxHost.GetFontFromIFont(objIFont) Err.Clear() End Function Public Overloads Shared Function ConvertTo_StdPicture(ByRef objIcon As Icon) As Object On Error Resume Next If objIcon Is Nothing Then Exit Function Dim objCursor As Cursor = New Cursor(objIcon.Handle) ConvertTo_StdPicture = AxHost.GetIPictureFromCursor(objCursor) Err.Clear() End Function Public Overloads Shared Function ConvertTo_StdPicture(ByRef objCursor As Cursor) As Object On Error Resume Next If objCursor Is Nothing Then Exit Function ConvertTo_StdPicture = AxHost.GetIPictureFromCursor(objCursor) Err.Clear() End Function Public Overloads Shared Function ConvertTo_StdPicture(ByRef objBitmap As Bitmap) As Object On Error Resume Next If objBitmap Is Nothing Then Exit Function Dim objImage As Image = Image.FromHbitmap(objBitmap.GetHbitmap) ConvertTo_StdPicture = AxHost.GetIPictureFromPicture(objImage) Err.Clear() End Function Public Overloads Shared Function ConvertTo_StdPicture(ByRef objImage As Image) As Object On Error Resume Next If objImage Is Nothing Then Exit Function ConvertTo_StdPicture = AxHost.GetIPictureFromPicture(objImage) Err.Clear() End Function Public Shared Function ConvertFrom_StdPicture(ByRef objIPicture As Object, Optional ByVal ReturnType As ReturnTypes = ReturnTypes.rt_Image) As Object On Error Resume Next ConvertFrom_StdPicture = Nothing If objIPicture Is Nothing Then Exit Function Select Case ReturnType Case ReturnTypes.rt_Image Select Case objIPicture.Type Case PictureTypeConstants.vbPicTypeNone Case Else ConvertFrom_StdPicture = AxHost.GetPictureFromIPicture(objIPicture) End Select Case ReturnTypes.rt_Bitmap Select Case objIPicture.Type Case PictureTypeConstants.vbPicTypeBitmap ConvertFrom_StdPicture = AxHost.GetPictureFromIPicture(objIPicture) End Select Case ReturnTypes.rt_Icon Select Case objIPicture.Type Case PictureTypeConstants.vbPicTypeIcon ConvertFrom_StdPicture = Icon.FromHandle(New System.IntPtr(CInt(objIPicture.Handle))) End Select Case ReturnTypes.rt_Cursor Select Case objIPicture.Type Case PictureTypeConstants.vbPicTypeIcon ConvertFrom_StdPicture = New Cursor(New System.IntPtr(CInt(objIPicture.Handle))) End Select Case ReturnTypes.rt_Metafile Select Case objIPicture.Type Case PictureTypeConstants.vbPicTypeMetafile, PictureTypeConstants.vbPicTypeEMetafile ConvertFrom_StdPicture = New System.Drawing.Imaging.Metafile(New System.IntPtr(CInt(objIPicture.Handle)), True) End Select End Select Err.Clear() End Function #End Region End Class