[R] ResourcesEx() et _GUIImageList_Create()

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
jpascal
Niveau 6
Niveau 6
Messages : 226
Enregistré le : jeu. 16 oct. 2008 16:21
Status : Hors ligne

[R] ResourcesEx() et _GUIImageList_Create()

#1

Message par jpascal »

Bonjour,

J'utilise l'UDF ResourcesEx pour intégrer des icônes à mes exécutables.

Je souhaite également intégrer les icônes dans une liste d'images GUI mais je n'y arrive pas.

J'ai tenté différents codes sans succès :
#AutoIt3Wrapper_Res_File_Add=img\type_user.ico, RT_ICON, ICO_TYPE_USER
#AutoIt3Wrapper_Res_File_Add=img\type_group.ico, RT_ICON, ICO_TYPE_GROUP

Global $g_hImage = _GUIImageList_Create(16, 16, 5, 5)
_GUIImageList_AddEx($g_hImage, 'ICO_TYPE_USER')
_GUIImageList_AddEx2($g_hImage, 'ICO_TYPE_GROUP')

Func _GUIImageList_AddEx($hImageList, $sResource)
;~    Local $hImage = _Resource_GetAsBitmap($sResource, $RT_ICON)
   Local $hImage = _Resource_GetAsBitmap($sResource, $RT_RCDATA)
;~    Local $hImage = _Resource_GetAsImage($sResource, $RT_RCDATA)
   _GUIImageList_AddBitmap($hImageList, $hImage)
   _WinAPI_DeleteObject($hImage)
EndFunc   ;==>_GUIImageList_AddEx

Func _GUIImageList_AddEx2($hImageList, $sResource)
;~    Local $hImage = _Resource_GetAsIcon($sResource, $RT_ICON)
   Local $hImage = _Resource_GetAsIcon($sResource, $RT_RCDATA)
   _GUIImageList_AddIcon($hImageList, $hImage)
   _WinAPI_DeleteObject($hImage)
EndFunc   ;==>_GUIImageList_AddEx2

Merci par avance pour votre aide.
Modifié en dernier par jpascal le mer. 30 oct. 2019 17:28, modifié 1 fois.
AutoIt 3.3.16.1 - AutoIt3Wrapper 21.316.1639.1
Avatar du membre
walkson
Modérateur
Modérateur
Messages : 1020
Enregistré le : ven. 12 août 2011 19:49
Localisation : Hurepoix
Status : Hors ligne

Re: [..] ResourcesEx() et _GUIImageList_Create()  

#2

Message par walkson »

Bonjour,
Vous n'êtes pas très bavard en code et en plus ResourcesEx bugue avec autoit 3.3.14.5 car il n'est plus mis à jour
J'ai bidouillé ce code qui marche mais il y a peut être plus simple

Code : Tout sélectionner

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=..\..\..\..\..\Program Files\AutoIt3\Examples\Helpfile\Extras\Script.ico
#AutoIt3Wrapper_Outfile=exemple_imagelist.exe
#AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Examples\GUI\merlin.gif, RT_RCDATA, MERLIN_1, 0
#AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Examples\Helpfile\Extras\Exclamation.png, RT_RCDATA, EX_1
#AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Examples\Helpfile\Extras\AutoIt.bmp, RT_RCDATA, AUTOIT_1
#AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Aut2Exe\Icons\SETUP01.ico, RT_ICON, ICO_1, 0
#AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Examples\Helpfile\Extras\Arrow.ico, RT_ICON, ICO_2, 0
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiImageList.au3>
#include <GuiListView.au3>
#include <SendMessage.au3>
#include <WinAPIHObj.au3>
#include <ButtonConstants.au3>
#include '..\ResourcesEx.au3'
Global $hImage, $hHBITMAP, $nMsg, $iI, $hBitmap, $wnd, $hDC, $hGraphic
Global $iStylesEx = BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES)
#Region ### START Koda GUI section ### Form=C:\Program Files\AutoIt3\SciTE\Koda\Templates\Form1.kxf
If Not @Compiled Then Exit MsgBox(16, "Error","N'est pas compilé !")
Global $Form1 = GUICreate("Form1", 453, 435, 691, 192)
GUISetBkColor(0x000030)
Global $ListView1 = GUICtrlCreateListView("", 160, 40, 265, 365, BitOR($LVS_SHOWSELALWAYS, $LVS_NOSORTHEADER, $LVS_REPORT))
_GUICtrlListView_SetBkColor($ListView1, 0xDDFFFF)
 _GUICtrlListView_SetTextBkColor($ListView1, 0xDDFFFF)
_GUICtrlListView_SetExtendedListViewStyle($ListView1, $iStylesEx)
_GUICtrlListView_AddColumn($ListView1, "Items", 120)
_GUICtrlListView_AddColumn($ListView1, "Items_1", 120)
Global $button = GUICtrlCreateButton("", 5, 150, 95,95, $BS_BITMAP)
GUICtrlSetTip(-1, "Click me")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$hImage = _GUIImageList_Create(80, 80,5,3)
;============================================================================== 0
$hHBITMAP = _Resource_GetAsImage('MERLIN_1', $RT_RCDATA)
$hHBITMAP = _GDIPlus_ImageResize ($hHBITMAP, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBITMAP)
$iI = _GUIImageList_Add($hImage,$hHBITMAP)
MsgBox(0,"merlin",$iI &"--"& $hHBITMAP)
;============================================================================= 1
$hHBITMAP = _Resource_GetAsImage('AUTOIT_1')
$hHBITMAP = _GDIPlus_ImageResize ($hHBITMAP, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBITMAP)
$iI = _GUIImageList_Add($hImage,$hHBITMAP)
MsgBox(0,"autoit",$iI  &" 2 --"& $hHBITMAP)
;================================================================================= 2
$hHBITMAP = _Resource_GetAsIcon('ICO_2')
MsgBox(0,"ICO_2",$hHBITMAP)
$hBitmap = _GDIPlus_BitmapCreateFromHICON($hHBITMAP)
$hBitmap = _GDIPlus_ImageResize ($hBitmap, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_GUIImageList_Add($hImage,$hHBITMAP)
;============================================================================== 3
$hHBITMAP = _Resource_GetAsIcon('ICO_1')
MsgBox(0,"ICO_1",$hHBITMAP)
$hBitmap = _GDIPlus_BitmapCreateFromHICON($hHBITMAP)
$hBitmap = _GDIPlus_ImageResize ($hBitmap, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_GUIImageList_Add($hImage,$hHBITMAP)
;================================================================================ 4
$hHBITMAP = _Resource_GetAsImage('EX_1', $RT_RCDATA)
$hHBITMAP = _GDIPlus_ImageResize ($hHBITMAP, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hHBITMAP)
$iI = _GUIImageList_Add($hImage,$hHBITMAP)
MsgBox(0,"EX_1",$iI &"--"& $hHBITMAP)
;======================================================================== imagelist sur item
_GUICtrlListView_SetImageList($ListView1, $hImage, 1)
;MsgBox(0,"",_GUIImageList_GetImageCount ( $hImage ))
Global $ListView1_0 = _GUICtrlListView_AddItem($ListView1, "1", 0)
Global $ListView1_1 = _GUICtrlListView_AddItem($ListView1, "2", 1)
Global $ListView1_2 = _GUICtrlListView_AddItem($ListView1, "3", 2)
Global $ListView1_3 = _GUICtrlListView_AddItem($ListView1, "4", 3)
_GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 2", 1, 4)
_GUICtrlListView_SetItemImage($ListView1, 1, 2, 1)
_GUICtrlListView_SetItemImage($ListView1, 2, 3, 1)
;================================================================================== imagelist sur button
Global $ahIcons = _GUIImageList_GetIcon($hImage, 4)
$hBitmap = _GDIPlus_BitmapCreateFromHICON($ahIcons)
$hBitmap = _GDIPlus_ImageResize ($hBitmap, 80, 80)
$hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
_WinAPI_DeleteObject(_SendMessage(GUICtrlGetHandle($button), $BM_SETIMAGE, $IMAGE_BITMAP, $hHBITMAP))
_WinAPI_DeleteObject($hHBITMAP)
_GDIPlus_BitmapDispose($hBitmap)
;MsgBox(0,"button",$hHBITMAP)
;========================================================================= imagelist sur Gui
		$hDC = _WinAPI_GetDC($Form1)
		_GUIImageList_Draw($hImage, 0, $hDC, 40, 20, 1)
		_WinAPI_ReleaseDC($Form1, $hDC)
While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
	 Case $GUI_EVENT_CLOSE
		 Exit
	 Case $GUI_EVENT_RESTORE
		$hDC = _WinAPI_GetDC($Form1)
		_GUIImageList_Draw($hImage, 0, $hDC, 40, 20, 1)
		_WinAPI_ReleaseDC($Form1, $hDC)
	 Case $button
		SplashTextOn("Splash Screen", "", 100, 100, 500,200, 1)
		$wnd = WinGetHandle("Splash Screen","")
		WinSetTrans($wnd,"",200)
		$hDC = _WinAPI_GetDC($wnd)
		$hGraphic = _GDIPlus_GraphicsCreateFromHDC($hDC)
		_GDIPlus_GraphicsClear($hGraphic, 0xAA6AB5FF)
		_GUIImageList_Draw($hImage, 1, $hDC, 10, 10)
		_GDIPlus_GraphicsDrawString($hGraphic, "Hello world", 0, 40, "Algerian", 11)
		_WinAPI_ReleaseDC($wnd, $hDC)
		 _GDIPlus_GraphicsDispose($hGraphic)
		Sleep(4000)
		SplashOff()


 EndSwitch
WEnd
Ne pas oublier de compiler le code
Vous remarquerez que les Gif, PNG ou BMP sont tous sous RT_RCDATA , ce qui simplifie.
Il y a une multitudes de MsgBox pour contrôler les Handles mais ils ne sont pas indispensables
Vérifiez le chemin des images dans le haut du code #AutoIt3Wrapper_Res_File_Add=C:\Program Files\AutoIt3\Examples\GUI\.....
Je joints ResourcesEx.zip avec l'UDF mis à jour. Le code ci-dessus est dans Example_1.au3 du ZIP
Personnellement, je n'utilise plus cette méthode que je trouve peu pratique. Je préfère la méthode UEZ à cette adresse https://www.autoitscript.com/forum/topi ... es-easily/ qui convertit images ou autres en binaire. Il suffit de rajouter un include contenant l'image convertie qui peut se travailler en "dur" ou en mémoire
Fichiers joints
ResourcesEx.zip
(34.37 Kio) Téléchargé 290 fois
Cordialement,
Walkson
"Horas non numero nisi serenas " Le canon de midi
(Je ne compte que les heures heureuses)
jpascal
Niveau 6
Niveau 6
Messages : 226
Enregistré le : jeu. 16 oct. 2008 16:21
Status : Hors ligne

Re: [..] ResourcesEx() et _GUIImageList_Create()

#3

Message par jpascal »

Bonjour walkson,

Je suis désolé de répondre avec autant de retard mais je n'avais pas vu votre réponse.

Je vous confirme que cela fonctionne désormais. Mon code était en effet incomplet.
Un grand merci à vous.

Avant modification :
Func GUIImageList_AddEx($hImageList, $sResource)
        Local $hImage = _Resource_GetAsBitmap($sResource, $RT_RCDATA)
        _GUIImageList_AddBitmap($hImageList, $hImage)
        _WinAPI_DeleteObject($hImage)
EndFunc   ;==>GUIImageList_AddEx

Après modification :
Func GUIImageList_AddEx($hImageList, $sResource)
   Local $hHBITMAP = _Resource_GetAsIcon($sResource)
   Local $hBitmap = _GDIPlus_BitmapCreateFromHICON($hHBITMAP)
   $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
   _GUIImageList_Add($hImageList, $hHBITMAP)
   _WinAPI_DeleteObject($hHBITMAP)
   _GDIPlus_BitmapDispose($hBitmap)
EndFunc   ;==>GUIImageList_AddEx

J'ai ajouté les lignes suivantes pour libérer la mémoire :
_WinAPI_DeleteObject($hHBITMAP)
_GDIPlus_BitmapDispose($hBitmap)

Sachant que ces "objets" sont créés dans une fonction, est-ce bien nécessaire ?
AutoIt 3.3.16.1 - AutoIt3Wrapper 21.316.1639.1
Répondre