This might be over complicated but here goes.
Declare
Friend Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Integer, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer
Friend Declare Function ExtractIconEx Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Int32, ByRef phiconLarge As Int32, ByRef phiconSmall As Int32, ByVal nIcons As Int32) As Int32
Friend Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Integer) As Integer
Now have this function.
Note OMReg is a Class that reads the Registry, but you get the idea.
Friend Function FindIconA(ByVal Handle As Integer, ByVal Ext As String) As Integer
Dim a, xName As String, i, r As Integer
Try
xName = OMReg.ReadKey(Ext, "", "", False, IniReader.RegKeys.ClassesRoot)
a = OMReg.ReadKey(xName & "\defaulticon", "", "", False, IniReader.RegKeys.ClassesRoot)
If a.StartsWith("%%") Then
xName = OMReg.ReadKey(xName & "\defaulticon", "old", "", False, IniReader.RegKeys.ClassesRoot)
i = InStr(xName, ",")
If i = 0 Then i = 1
r = CInt(xName.Substring(i))
ElseIf xName.Substring(0, 1) = "%" Then
r = CInt(a.Substring(1))
Else
xName = a
i = InStr(xName, ",")
If i = 0 Then i = 1
r = CInt(xName.Substring(i))
End If
xName = xName.Substring(0, i - 1) & vbNullChar
i = ExtractIconEx(xName, -1, 0&, 0&, 0&)
If r > i Then r = 1
i = ExtractIcon(Handle, xName, r)
Catch ex As Exception
r = ExtractIcon(Handle, "Shell32.dll", 0)
End Try
Return i
End Function
Bear with me.
Dim i As Integer = FindIconA(Handle, ".dwg")
Dim icn As Icon = Icon.FromHandle(New IntPtr(i))
Button.Image = icn.ToBitmap
DestroyIcon(i)
Nearly there.
Button has property
Public Property Image() As Image
Get
Return gImg
End Get
Set(ByVal value As Image)
gImg = value
Dim e As New System.PaintEventArgs
OnPaint(e)
RaiseEvent ImageChanged(Me, e)
End Set
End Property
and finally
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawImage(gImg, 0, 0, Width, Width)
MyBase.OnPaint(e)
End Sub
Of course I may have answered the wrong question.