_GUIImageList_AddBitmapEx() v0.80 Build 2011-11-05 beta - fügt ein Bild in den _GUIImageList Control hinzu

  • _GUIImageList_AddBitmap() kann "nur" BMP Bilder in die _GUIImageList hinzufügen.

    Hier eine Erweiterung, damit auch andere Bild Formate, wie z.B. JPG, PNG, ICO, etc., hinzugefügt werden können.

    _GUIImageList_AddBitmapEx.au3:

    Spoiler anzeigen
    [autoit]


    #include-once

    [/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GUIImageList_AddBitmapEx
    ; Description ...: Creates a bitmap from an image file (JPG, PNG, BMP, GIF, ICO, etc.) and adds it to the _GUIImageList control
    ; Syntax.........: _GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth, $iHeight)
    ; Parameters ....: $hWnd - file name where the icon should be extracted from (*.dll, *.ico, *.exe)
    ; $sFile - path to the bitmap that contains the image
    ; $iWidth - width of the _GUIImageList
    ; $iHeight - height of the _GUIImageList
    ; $iIndex = 0 - icon index which should be extracted
    ; Return values .: Success - Returns 1
    ; Failure - @error
    ; Author ........: UEZ
    ; Version .......: 0.80 Build 2011-11-05 beta
    ; Remarks .......: <GDIPlus.au3> must be included and _GDIPlus_Startup() must be started to use GDI+ functions.
    ; Don't forget the _GDIPlus_Shutdown() when closing!
    ; ===============================================================================================================================
    Func _GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth = 16, $iHeight = 16, $iIndex = 0)
    If Not IsHWnd($hWnd) = 0 Or $sFile = "" Or Not FileExists($sFile) Or IsDeclared("ghGDIPDll") <> 1 Then Return SetError(1, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $ext = StringRight($sFile, 3)
    If $ext = "ico" Or $ext = "exe" Or $ext = "dll" Then
    Local $aRet = DllCall("Shell32", "long", "ExtractAssociatedIcon", "int", 0, "str", $sFile, "word*", $iIndex)
    If @error Then Return SetError(1, @extended, 0)
    Local $hIcon = $aRet[0]
    Local $hDC = _WinAPI_GetDC(0) ;thanks to Yashied
    Local $hBackDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBitmap = _WinAPI_CreateSolidBitmap(0, _WinAPI_GetSysColor($COLOR_MENU), $iWidth, $iHeight)
    Local $hBackSv = _WinAPI_SelectObject($hBackDC, $hBitmap)
    _WinAPI_DrawIconEx($hBackDC, 0, 0, $hIcon, $iWidth, $iHeight, 0, 0, 3)
    _WinAPI_SelectObject($hBackDC, $hBackSv)
    _WinAPI_ReleaseDC(0, $hDC)
    _WinAPI_DeleteDC($hBackDC)
    _GUIImageList_Add($hWnd, $hBitmap)
    _WinAPI_DeleteObject($hBitmap)
    Else
    Local $aProcessIcons = _GDIPlus_BitmapCreateFromFile($sFile)
    If @error Then Return SetError(2, @extended, 0)
    Local $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(3, @extended, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hClone = $aResult[6]
    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hClone)
    ;~ DllCall($ghGDIPDll, "int", "GdipSetInterpolationMode", "handle", $hGraphics, "int", 5)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $aProcessIcons, 0, 0, $iWidth, $iHeight)

    [/autoit] [autoit][/autoit] [autoit]

    Local $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hClone)
    _GUIImageList_Add($hWnd, $hBmp)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($hClone)
    _WinAPI_DeleteObject($hBmp)
    _GDIPlus_BitmapDispose($aProcessIcons)
    EndIf
    Return 1
    EndFunc

    [/autoit]



    Im Beispiel (Anhang) den Pfad zu den Bildern enstprechend anpassen!

    Gruß,
    UEZ

  • Hallo UEZ,

    leider funktioniert das bei mir nicht. Egal was ich versuche einzufügen, es werden immer nur einfarbige (das 1. blau, alle anderen schwarz) Quadrate eingefügt. Benötigen die Bilder (jpg;ico) eine bestimmte Grösse :?: Im Anhang 2 Beispiele

    mfg (Auto)Bert


    Danke für dein Feedback!

    Ich hatte vergessen, den kompletten Pfad zu übergeben! -> _GUIImageList_AddBitmapEx($hImage, $image_path & "\" & $count[$i], $iCX, $iCY)

    *.ico Dateien werden noch nicht unterstützt! Nächste Version. Sollte jetzt auch mit ICO Dateien funzen!

    Gruß,
    UEZ

    Auch am Arsch geht ein Weg vorbei...

    ¯\_(ツ)_/¯

    Einmal editiert, zuletzt von UEZ (23. Juni 2010 um 00:38)

  • Hallo UEZ,

    ich habe jetzt die eine Skiptzeile im Example ausgetauscht und es mit JPG's getestet. Klappt wunderbar :thumbup:

    Edit: habe dein Beispiel etwas abgeändert, so dass man nach drücken des Select-Buttons das Bild angezeigt bekommt:

    Spoiler anzeigen
    [autoit]

    #include <Array.au3>
    #include <GDIPlus.au3>
    #include <GuiComboBoxEx.au3>
    #include <GuiImageList.au3>
    #include <GuiConstantsEx.au3>
    #include <File.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $image_path = "C:\Dokumente und Einstellungen\Bert\Eigene Dateien"
    Global $hPic = -99
    $count = _FileListToArray($image_path, "*.jpg", 1)

    [/autoit] [autoit][/autoit] [autoit]

    If Not IsArray($count) Then Exit MsgBox(16, "Error", "No JPG images found in " & $image_path, 10)

    [/autoit] [autoit][/autoit] [autoit]

    $hGUI = GUICreate("Image to _GUIImageList", 540, 590)
    $hCombo = _GUICtrlComboBoxEx_Create($hGUI, "", 20, 20, 70, 335, $CBS_DROPDOWNLIST)
    Dim $bitmap_from_file[$count[0] + 1]
    _GDIPlus_Startup()
    $iCX = 48
    $iCY = 48
    $hImage = _GUIImageList_Create(48, 48, 5, 1)
    For $i = 1 To $count[0]
    _GUIImageList_AddBitmapEx($hImage, $image_path & "\" & $count[$i], $iCX, $iCY)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlComboBoxEx_SetImageList($hCombo, $hImage)
    For $i = 0 To $count[0] - 1
    _GUICtrlComboBoxEx_AddString($hCombo, $image_path & "\" & $count[$i], $i, $i)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    _GUICtrlComboBoxEx_SetItemHeight($hCombo, 0, 51)
    _GUICtrlComboBoxEx_SetCurSel($hCombo, 0)
    $botton = GUICtrlCreateButton("Select", 120, 20, 100, 25)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $gui_event_close
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
    Case $msg = $botton
    $iItem = _GUICtrlComboBoxEx_GetCurSel($hCombo)+1
    $aItem = _GUICtrlComboBoxEx_GetItem($hCombo,$iItem)
    $hImage = _GDIPlus_BitmapCreateFromFile ($aItem[0])
    $iX = _GDIPlus_ImageGetWidth($hImage)
    $iY = _GDIPlus_ImageGetHeight($hImage)
    _GDIPlus_BitmapDispose($hImage)
    $iRatio = $iX / $iY
    ;ConsoleWrite("Orig.: " & $iX & " " & $iY & " " & $iRatio & @CRLF)
    if $iRatio >= 1 Then
    $iX = 500
    $iY = $iX / $iRatio
    Else
    $iY = 300
    $iX =$iY * $iRatio
    EndIf
    ;ConsoleWrite("Neu: " & $iX & " " & $iY & " " & $iRatio & @CRLF)
    if $hPic <> -99 then GUICtrlDelete($hPic)
    $hPic = GUICtrlCreatePic($aItem[0],10,80,$iX,$iY)
    EndSelect
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ; #FUNCTION# ====================================================================================================================
    ; Name...........: _GUIImageList_AddBitmapEx
    ; Description ...: Creates a bitmap from an image file (JPG, PNG, BMP, GIF, etc.) and adds it to the _GUIImageList control
    ; Syntax.........: _GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth, $iHeight)
    ; Parameters ....: $hWnd - file name where the icon should be extracted from (*.dll, *.ico, *.exe)
    ; $sFile - path to the bitmap that contains the image
    ; $iWidth - width of the _GUIImageList
    ; $iHeight - height of the _GUIImageList

    [/autoit] [autoit][/autoit] [autoit]

    ; Return values .: Success - Returns 1
    ; Failure - @error
    ; Author ........: UEZ
    ; Version .......: 0.5 Build 2010-06-22
    ; Remarks .......: <GDIPlus.au3> must be included and _GDIPlus_Startup() must be started to use GDI+ functions.
    ; Don't forget the _GDIPlus_Shutdown() when closing
    ; ===============================================================================================================================
    Func _GUIImageList_AddBitmapEx($hWnd, $sFile, $iWidth = 48, $iHeight = 48)
    If $hWnd = 0 Or $sFile = "" Or Not IsDeclared("ghGDIPDll") Then Return SetError(1, 0, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $iStride = 0, $iPixelFormat = 0x0026200A, $pScan0 = 0
    Local $bitmap_from_file = _GDIPlus_BitmapCreateFromFile($sFile)
    If @error Then Return SetError(@error, @extended, 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iWidth, "int", $iHeight, "int", $iStride, "int", $iPixelFormat, "ptr", $pScan0, "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Local $hClone = $aResult[6]

    [/autoit] [autoit][/autoit] [autoit]

    $hGraphics = _GDIPlus_ImageGetGraphicsContext($hClone)
    _GDIPlus_GraphicsDrawImageRect($hGraphics, $bitmap_from_file, 0, 0, $iWidth, $iHeight)

    [/autoit] [autoit][/autoit] [autoit]

    $hBmp = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hClone)
    _GUIImageList_Add($hWnd, $hBmp)

    [/autoit] [autoit][/autoit] [autoit]

    _GDIPlus_GraphicsDispose($hGraphics)
    _GDIPlus_BitmapDispose($bitmap_from_file)
    _GDIPlus_BitmapDispose($hClone)
    _WinAPI_DeleteObject($hBmp)

    [/autoit] [autoit][/autoit] [autoit]

    Return SetError(0, 0, 1)
    EndFunc

    [/autoit]

    mfg (Auto)Bert

    Einmal editiert, zuletzt von AutoBert (22. Juni 2010 um 14:22)


  • Danke auch euch beiden für das Feedback!

    Funzt der Code auch unter WinXP x32/x64, Win7 x32/x64?

    Gruß,
    UEZ

    Funzt ohne Probleme bei mir unter Win7 x64 :thumbup: