Icon einer Datei ändern ?

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von n1nja.

    Icon einer Datei ändern ?

    Hallo ich wollte fragen, wie es möglich ist, das Icon einer bzw. mehrerer Dateien zu ändern in eine .ico-Datei aus den Ressourcen.
    Ich habe die Materialien schon (1x Button, 1x Textbox), da soll man in der Textbox den Pfad einer Datei eintragen und dessen
    Symbol soll in eine Datei aus den Resourcen geändert werden (Die ico-datei aus den resourcen heißt regenbogen.ico)^^
    ^^ ^^ ^^

    Weiß jemand wie man das macht ??? ?( ?( ?( ?(
    Das Icon gehört zu den Ressourcen, d.h. es ist während der Laufzeit ReadOnly.
    Du kannst es nur zur Design-Zeit verändern.
    (Von Hacker-Mist rede ich hier nicht.)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Soweit ich weiß ist das unter Windows schwer möglich, da die Icons einem Dateityp, nicht einer Datei zugeordnet sind.
    Diese Zuordnung findet sich vermutlich irgendwo in der Registry, aber für das, was du da realisieren willst, wird dir das sowieso wohl kaum was bringen.
    Fang lieber mit was einfacherem an ;)


    Mfg, jmb.96 :)

    EDIT: @RodFromGermany: Er will nicht das Icon des Programms ändern, sondern das einer anderen Datei iwo auf der Festplatte.
    "People assume that time is a strict progression of cause to effect, but actually, from a non-linear, non-subjective viewpoint, it's more like a big ball of wibbly wobbly, ...timey wimey ...stuff."

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „jmb.96“ ()

    2 min Google:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Imports System.Security
    3. Public Class IconInjector
    4. <SuppressUnmanagedCodeSecurity()> _
    5. Private Class NativeMethods
    6. <DllImport("kernel32")> _
    7. Public Shared Function BeginUpdateResource( _
    8. ByVal fileName As String, _
    9. <MarshalAs(UnmanagedType.Bool)> ByVal deleteExistingResources As Boolean) As IntPtr
    10. End Function
    11. <DllImport("kernel32")> _
    12. Public Shared Function UpdateResource( _
    13. ByVal hUpdate As IntPtr, _
    14. ByVal type As IntPtr, _
    15. ByVal name As IntPtr, _
    16. ByVal language As Short, _
    17. <MarshalAs(UnmanagedType.LPArray, SizeParamIndex:=5)> _
    18. ByVal data() As Byte, _
    19. ByVal dataSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
    20. End Function
    21. <DllImport("kernel32")> _
    22. Public Shared Function EndUpdateResource( _
    23. ByVal hUpdate As IntPtr, _
    24. <MarshalAs(UnmanagedType.Bool)> ByVal discard As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
    25. End Function
    26. End Class
    27. <StructLayout(LayoutKind.Sequential)> _
    28. Private Structure ICONDIR
    29. Public Reserved As UShort
    30. Public Type As UShort
    31. Public Count As UShort
    32. End Structure
    33. <StructLayout(LayoutKind.Sequential)> _
    34. Private Structure ICONDIRENTRY
    35. Public Width As Byte
    36. Public Height As Byte
    37. Public ColorCount As Byte
    38. Public Reserved As Byte
    39. Public Planes As UShort
    40. Public BitCount As UShort
    41. Public BytesInRes As Integer
    42. Public ImageOffset As Integer
    43. End Structure
    44. <StructLayout(LayoutKind.Sequential)> _
    45. Private Structure BITMAPINFOHEADER
    46. Public Size As UInteger
    47. Public Width As Integer
    48. Public Height As Integer
    49. Public Planes As UShort
    50. Public BitCount As UShort
    51. Public Compression As UInteger
    52. Public SizeImage As UInteger
    53. Public XPelsPerMeter As Integer
    54. Public YPelsPerMeter As Integer
    55. Public ClrUsed As UInteger
    56. Public ClrImportant As UInteger
    57. End Structure
    58. <StructLayout(LayoutKind.Sequential, Pack:=2)> _
    59. Private Structure GRPICONDIRENTRY
    60. Public Width As Byte
    61. Public Height As Byte
    62. Public ColorCount As Byte
    63. Public Reserved As Byte
    64. Public Planes As UShort
    65. Public BitCount As UShort
    66. Public BytesInRes As Integer
    67. Public ID As UShort
    68. End Structure
    69. Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String)
    70. InjectIcon(exeFileName, iconFileName, 1, 1)
    71. End Sub
    72. Public Shared Sub InjectIcon(ByVal exeFileName As String, ByVal iconFileName As String, ByVal iconGroupID As UInteger, ByVal iconBaseID As UInteger)
    73. Const RT_ICON = 3UI
    74. Const RT_GROUP_ICON = 14UI
    75. Dim iconFile As IconFile = iconFile.FromFile(iconFileName)
    76. Dim hUpdate = NativeMethods.BeginUpdateResource(exeFileName, False)
    77. Dim data = iconFile.CreateIconGroupData(iconBaseID)
    78. NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_GROUP_ICON), New IntPtr(iconGroupID), 0, data, data.Length)
    79. For i = 0 To iconFile.ImageCount - 1
    80. Dim image = iconFile.ImageData(i)
    81. NativeMethods.UpdateResource(hUpdate, New IntPtr(RT_ICON), New IntPtr(iconBaseID + i), 0, image, image.Length)
    82. Next
    83. NativeMethods.EndUpdateResource(hUpdate, False)
    84. End Sub
    85. Private Class IconFile
    86. Private iconDir As New ICONDIR
    87. Private iconEntry() As ICONDIRENTRY
    88. Private iconImage()() As Byte
    89. Public ReadOnly Property ImageCount As Integer
    90. Get
    91. Return iconDir.Count
    92. End Get
    93. End Property
    94. Public ReadOnly Property ImageData(ByVal index As Integer) As Byte()
    95. Get
    96. Return iconImage(index)
    97. End Get
    98. End Property
    99. Private Sub New()
    100. End Sub
    101. Public Shared Function FromFile(ByVal filename As String) As IconFile
    102. Dim instance As New IconFile
    103. Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename)
    104. Dim pinnedBytes = GCHandle.Alloc(fileBytes, GCHandleType.Pinned)
    105. instance.iconDir = DirectCast(Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject, GetType(ICONDIR)), ICONDIR)
    106. instance.iconEntry = New ICONDIRENTRY(instance.iconDir.Count - 1) {}
    107. instance.iconImage = New Byte(instance.iconDir.Count - 1)() {}
    108. Dim offset = Marshal.SizeOf(instance.iconDir)
    109. Dim iconDirEntryType = GetType(ICONDIRENTRY)
    110. Dim size = Marshal.SizeOf(iconDirEntryType)
    111. For i = 0 To instance.iconDir.Count - 1
    112. Dim entry = DirectCast(Marshal.PtrToStructure(New IntPtr(pinnedBytes.AddrOfPinnedObject.ToInt64 + offset), iconDirEntryType), ICONDIRENTRY)
    113. instance.iconEntry(i) = entry
    114. instance.iconImage(i) = New Byte(entry.BytesInRes - 1) {}
    115. Buffer.BlockCopy(fileBytes, entry.ImageOffset, instance.iconImage(i), 0, entry.BytesInRes)
    116. offset += size
    117. Next
    118. pinnedBytes.Free()
    119. Return instance
    120. End Function
    121. Public Function CreateIconGroupData(ByVal iconBaseID As UInteger) As Byte()
    122. Dim sizeOfIconGroupData As Integer = Marshal.SizeOf(GetType(ICONDIR)) + Marshal.SizeOf(GetType(GRPICONDIRENTRY)) * ImageCount
    123. Dim data(sizeOfIconGroupData - 1) As Byte
    124. Dim pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned)
    125. Marshal.StructureToPtr(iconDir, pinnedData.AddrOfPinnedObject, False)
    126. Dim offset = Marshal.SizeOf(iconDir)
    127. For i = 0 To ImageCount - 1
    128. Dim grpEntry As New GRPICONDIRENTRY
    129. Dim bitmapheader As New BITMAPINFOHEADER
    130. Dim pinnedBitmapInfoHeader = GCHandle.Alloc(bitmapheader, GCHandleType.Pinned)
    131. Marshal.Copy(ImageData(i), 0, pinnedBitmapInfoHeader.AddrOfPinnedObject, Marshal.SizeOf(GetType(BITMAPINFOHEADER)))
    132. pinnedBitmapInfoHeader.Free()
    133. grpEntry.Width = iconEntry(i).Width
    134. grpEntry.Height = iconEntry(i).Height
    135. grpEntry.ColorCount = iconEntry(i).ColorCount
    136. grpEntry.Reserved = iconEntry(i).Reserved
    137. grpEntry.Planes = bitmapheader.Planes
    138. grpEntry.BitCount = bitmapheader.BitCount
    139. grpEntry.BytesInRes = iconEntry(i).BytesInRes
    140. grpEntry.ID = CType(iconBaseID + i, UShort)
    141. Marshal.StructureToPtr(grpEntry, New IntPtr(pinnedData.AddrOfPinnedObject.ToInt64 + offset), False)
    142. offset += Marshal.SizeOf(GetType(GRPICONDIRENTRY))
    143. Next
    144. pinnedData.Free()
    145. Return data
    146. End Function
    147. End Class
    148. End Class