Icons aus exe, dll oder ico extrahieren/verwenden (auch .NET)

    • VB6

      Icons aus exe, dll oder ico extrahieren/verwenden (auch .NET)

      Mit folgendem API-Call lassen sich Icons aus .exe-, .dll- und .ico-Dateien holen:

      VB.NET-Quellcode

      1. Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" _
      2. (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
      Vorhanden seit WinNT 3.1 und Win95.


      Als ersten Parameter kann man Null oder das hWnd übergeben, als 2ten den Pfad zur Datei und als 3ten die Icon-Nummer. Die Funktion gibt die Anzahl der enthaltenen Icons zurück, wenn als IconIndex -1 übergeben wird, 1, wenn es sich nicht um eine passende Datei handelt und 0, wenn die Datei keine Icons enthält.
      Quelle: API-Guide


      Code-Beispiel für VB6:

      Visual Basic-Quellcode

      1. Private Declare Function DrawIcon Lib "user32" Alias "DrawIcon" _
      2. (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
      3. Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" _
      4. (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
      5. Private Sub Form_Load()
      6. Dim hIcon As Long
      7. hIcon = ExtractIcon(me.hWnd, "%SystemRoot%\System32\shell32.dll", 3) 'Icon laden
      8. If hIcon <> 0 and hIcon <> 1 then
      9. Picture1.AutoRedraw = true 'autoredraw anschalten
      10. Picture1.Picture = LoadPicture() 'PicBox leeren
      11. DrawIcon Picture1.hDC, 0, 0, hIcon 'Icon zeichnen
      12. end if
      13. End Sub


      Code-Beispiel für vb.net

      VB.NET-Quellcode

      1. Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" _
      2. (ByVal hInst As IntPtr, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As IntPtr
      3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      4. Dim hIcon As IntPtr
      5. hIcon = ExtractIcon(Me.Handle, "%SystemRoot%\System32\shell32.dll", 3)
      6. If hIcon <> 0 and hIcon <> 1 then
      7. Dim ic As Icon = Icon.FromHandle(hIcon)
      8. PictureBox1.Image = ic.ToBitmap
      9. End If
      10. End Sub

      Danke an Lupus für das .net-Beispiel :)


      Beispielprojekt: [VB6] Alle Icons einer Datei einlesen


      Keywords: Visual Basic, VB6, VB.net, VB2005, Icon aus .exe, .dll, .ico extrahieren, ExtractIcon, DrawIcon

      Dieser Beitrag wurde bereits 8 mal editiert, zuletzt von „Marcus Gräfe“ ()