Jump to content

Set image png to ListView


Go to solution Solved by johnmcloud,

Recommended Posts

hello!
I have a little problem.
Can someone help me to insert the two PNG images in a simple way. ?

Thank you!

#include <GuiImageList.au3>
#include <GDIPlus.au3>
#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>

_GDIPlus_Startup()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 623, 442, 192, 124)
$ListView1 = GUICtrlCreateListView("#COUNTRY|#name|#address", 0, 0, 618, 438, -1, BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_SUBITEMIMAGES,$LVS_EX_CHECKBOXES,$LVS_EX_FULLROWSELECT))
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 100)


$hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Flag_ROM.png")
$hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
$hFlags = _GUIImageList_Create(40, 27)
_GUIImageList_Add($hFlags, $hHBitmap)
_GUICtrlListView_SetImageList($ListView1, $hFlags, 1)
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hHBitmap)

_GUICtrlListView_AddItem($ListView1, "RO")
_GUICtrlListView_AddSubItem($ListView1, 0, "Place to eat", 1)
_GUICtrlListView_AddSubItem($ListView1, 0, "address", 2)



$hBitmap = _GDIPlus_BitmapCreateFromFile(@ScriptDir & "\Flag_EN.png")
$hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
$hFlags = _GUIImageList_Create(40, 27)
_GUIImageList_Add($hFlags, $hHBitmap)
_GUICtrlListView_SetImageList($ListView1, $hFlags, 1)
_GDIPlus_BitmapDispose($hBitmap)
_WinAPI_DeleteObject($hHBitmap)

_GUICtrlListView_AddItem($ListView1, "RO")
_GUICtrlListView_AddSubItem($ListView1, 1, "Place to eat", 1)
_GUICtrlListView_AddSubItem($ListView1, 1, "address", 2)


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GDIPlus_Shutdown()
            Exit

    EndSwitch
WEnd

post-75717-0-17782500-1397558603.png

post-75717-0-32342200-1397558603.png

Link to comment
Share on other sites

  • Solution

; Johnmcloud - 2014
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIImageList.au3>
#include <GUIListView.au3>
#include <GDIPlus.au3>

$hGUI = GUICreate("Johnmcloud Test GUI", 620, 440, -1, -1)
$iListView = GUICtrlCreateListView("County|Name|Address", 0, 0, 620, 440, -1, BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_SUBITEMIMAGES, $LVS_EX_CHECKBOXES, $LVS_EX_FULLROWSELECT))
$hListView = GUICtrlGetHandle($iListView)
$hImageList = _GUIImageList_Create(40, 40)
_GUICtrlListView_SetImageList($hListView, $hImageList, 1)

_GUICtrlListView_InsertItem($hListView, 'RO', 0)
_GUICtrlListView_SetItemImageEx($hListView, 0, @ScriptDir & "\img1.png")
_GUICtrlListView_AddSubItem($hListView, 0, "Place to eat in Romania", 1)
_GUICtrlListView_AddSubItem($hListView, 0, "7722 Dusty Cider Highlands", 2)

_GUICtrlListView_InsertItem($hListView, 'RO', 1)
_GUICtrlListView_SetItemImageEx($hListView, 0, @ScriptDir & "\img2.png")
_GUICtrlListView_AddSubItem($hListView, 1, "Place to eat in England", 1)
_GUICtrlListView_AddSubItem($hListView, 1, "3042 High Branch Freeway", 2)

GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, -1)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, -1)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, -1)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func _GUICtrlListView_SetItemImageEx($hWnd, $iIndex, $sFile) ; Orginal function by Yashied
    Local $Size = _GUIImageList_GetIconSize($hImageList), $W, $H, $hGraphic, $hPic, $hImage, $hIcon
    If (Not $Size[0]) Or (Not $Size[1]) Then Return 0
    _GDIPlus_Startup()
    $hPic = _GDIPlus_ImageLoadFromFile($sFile)
    $W = _GDIPlus_ImageGetWidth($hPic)
    $H = _GDIPlus_ImageGetHeight($hPic)
    If ($W < 0) Or ($H < 0) Then
        _GDIPlus_Shutdown()
        Return 0
    EndIf
    If $W < $H Then
        $W = $Size[0] * $W / $H
        $H = $Size[1]
    Else
        $H = $Size[1] * $H / $W
        $W = $Size[0]
    EndIf
    $hImage = DllCall($ghGDIPDll, 'int', 'GdipGetImageThumbnail', 'ptr', $hPic, 'int', $Size[0], 'int', $Size[1], 'ptr*', 0, 'ptr', 0, 'ptr', 0)
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage[4])
    _GDIPlus_GraphicsClear($hGraphic, 0)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hPic, ($Size[0] - $W) / 2, ($Size[1] - $H) / 2, $W, $H)
    $hIcon = DllCall($ghGDIPDll, 'int', 'GdipCreateHICONFromBitmap', 'ptr', $hImage[4], 'ptr*', 0)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_ImageDispose($hImage[4])
    _GDIPlus_ImageDispose($hPic)
    _GDIPlus_Shutdown()
    If Not $hIcon[2] Then Return 0
    _GUIImageList_ReplaceIcon($hImageList, -1, $hIcon[2])
    _GUICtrlListView_SetItemImage($hWnd, $iIndex, _GUIImageList_GetImageCount($hImageList) - 1)
    _WinAPI_DestroyIcon($hIcon[2])
    Return 1
EndFunc   ;==>_GUICtrlListView_SetItemImageEx

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...