Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. Public Shared Function ExtractAssociatedIconArray(ByVal File As String, ByVal Sizes() As Size) As Icon()
  2. Dim ReturnArray(Sizes.Length) As Icon
  3. Dim Index As Integer = 0
  4.  
  5. For Each s As Size In Sizes
  6. 'IconPtr is always zero for some reason.
  7. Dim IconPtr As IntPtr = NativeMethods.LoadImage(Nothing, File, NativeMethods.Enumrations.IMAGE_ICON, s.Width, s.Height, NativeMethods.Enumrations.LR_DEFAULTCOLOR Or NativeMethods.Enumrations.LR_LOADFROMFILE)
  8. ReturnArray(Index) = Icon.FromHandle(IconPtr)
  9. Index += 1
  10. Next
  11.  
  12. Return ReturnArray
  13. End Function
  14.  
  15. Public Class NativeMethods
  16. <DllImport("user32.dll", SetLastError:=True)> _
  17. Public Shared Function LoadImage(ByVal hInst As IntPtr, _
  18. ByVal lpszName As String,
  19. ByVal uType As UInt32, _
  20. ByVal cxDesired As Integer, _
  21. ByVal cyDesired As Integer, _
  22. ByVal fuLoad As UInt32) As IntPtr
  23. End Function
  24.  
  25. Public Enum Enumrations As UInteger
  26. '' LoadImage ''
  27. IMAGE_BITMAP = 0
  28. IMAGE_ICON = 1
  29. IMAGE_CURSOR = 2
  30. LR_CREATEDIBSECTION = &H2000
  31. LR_DEFAULTCOLOR = &H0
  32. LR_DEFAULTSIZE = &H40
  33. LR_LOADFROMFILE = &H10
  34. LR_LOADMAP3DCOLORS = &H1000
  35. LR_LOADTRANSPARENT = &H20
  36. LR_MONOCHROME = &H1
  37. LR_SHARED = &H8000
  38. LR_VGACOLOR = &H80
  39. End Enum
  40. End Class
  41.  
  42. Dim Icons() As Icon = ExtractAssociatedIconArray("C:MyApp.exe", New Size() {New Size() {48, 48}})
  43.  
  44. Public Shared Function ExtractAssociatedIcons(ByVal File As String) As AssemblyIconCollcetion
  45. Dim IconCount As Integer = NativeMethods.ExtractIconEx(File, -1, Nothing, Nothing, 0)
  46. Dim AssemblyIcons As New AssemblyIconCollcetion
  47.  
  48. 'The 'Icon handle' arrays.
  49. Dim LargeIcons(IconCount) As IntPtr
  50. Dim SmallIcons(IconCount) As IntPtr
  51.  
  52. 'Extract icons into the two arrays of handlers.
  53. NativeMethods.ExtractIconEx(File, 0, LargeIcons, SmallIcons, IconCount)
  54.  
  55. 'Add each large icon to the "LargeIcons" list.
  56. For Each ptr As IntPtr In LargeIcons
  57. If ptr = IntPtr.Zero Then Continue For
  58.  
  59. Dim Ico As Icon = Icon.FromHandle(ptr)
  60. If Ico.Width < 25 Or Ico.Height < 25 Then Continue For
  61. AssemblyIcons.LargeIcons.Add(Ico)
  62. Next
  63.  
  64. 'Add each small icon to the "SmallIcons" list.
  65. For Each ptr As IntPtr In SmallIcons
  66. If ptr = IntPtr.Zero Then Continue For
  67.  
  68. Dim Ico As Icon = Icon.FromHandle(ptr)
  69. If Ico.Width > 24 Or Ico.Height > 24 Then Continue For
  70. AssemblyIcons.SmallIcons.Add(Ico)
  71. Next
  72.  
  73. 'Return the output class.
  74. Return AssemblyIcons
  75. End Function
  76.  
  77. Public NotInheritable Class AssemblyIconCollection
  78. ''' <summary>
  79. ''' Gets or sets the large icons found in the assembly.
  80. ''' </summary>
  81. ''' <remarks></remarks>
  82. Public Property LargeIcons As List(Of Icon)
  83.  
  84. ''' <summary>
  85. ''' Gets or sets the small icons found in the assembly.
  86. ''' </summary>
  87. ''' <remarks></remarks>
  88. Public Property SmallIcons As List(Of Icon)
  89.  
  90. Public Sub New()
  91. Me.LargeIcons = New List(Of Icon)
  92. Me.SmallIcons = New List(Of Icon)
  93. End Sub
  94. End Class
  95.  
  96. <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
  97. Shared Function ExtractIconEx(ByVal szFileName As String, _
  98. ByVal nIconIndex As Integer, _
  99. ByVal phiconLarge() As IntPtr, _
  100. ByVal phiconSmall() As IntPtr, _
  101. ByVal nIcons As UInteger) As UInteger
  102. End Function
  103.  
  104. Dim Icons As AssemblyIconCollection = ExtractAssociatedIcons("C:myfile.exe")
  105.  
  106. 'Iterating every large icon.
  107. For Each LargeIcon As Icon In Icons.LargeIcons
  108. 'Do stuff.
  109. Next
  110.  
  111. 'Iterating every small icon.
  112. For Each SmallIcon As Icon In Icons.SmallIcons
  113. 'Do stuff.
  114. Next
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement