http://www.autoitscript.fr/forum/viewto ... =4&t=10166
J'entends d'ici les cris à la réinvention de roue, mais pour ceux qui rament avec les lourdeurs des _GUICtrlToolbar* et qui préfèrent rester en terrain connu, voilà un exemple très simple de pseudo-toolbar dynamique avec des boutons texte et construite juste avec des machins compréhensibles
Dans l'array de départ on ne définit pour chaque bouton que le texte et la fonction à exécuter, la taille du bouton s'ajuste automatiquement
Clic droit => on peut enlever des boutons ou les remettre à leur place, la gui se redimensionne automatiquement
Ce n'est qu'une base de script mais le code est très souple et facilement adaptable aux envies ou besoins
► Afficher le texte
Code : Tout sélectionner
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>
Global $boutons[5][7] = [[0, "Exit", "_Exit"], [0, "OK", "_OK"], _
[0, "Enregistrer", "_Enregistrer"], [0, "Tester", "_Tester"], _
[0, "Lire le journal du Dimanche", "_Newspapa"]]
; 0 id ; 1 texte ; 2 fonction ; 3 left pos ; 4 largeur ; 5 menuitem ajouter ; 6 menuitem enlever
$gui = GUICreate("my_amazing_toolbar", 0, 36, -1, 30, $WS_POPUP, 0x00020008)
$label = GUICtrlCreateLabel(" ici y en avoir du texte", 0, 0, 100, 15, 0x1200, $GUI_WS_EX_PARENTDRAG)
GUICtrlSetBkColor(-1, 0xd5e0ff)
GUICtrlSetFont(-1, 8.5, 400, 0, "Courier New")
For $i = 0 To UBound($boutons) - 1
$boutons[$i][4] = _GetZeSizeOfZisText($boutons[$i][1])
If $i = 0 Then
$boutons[$i][3] = 2
Else
$boutons[$i][3] = $boutons[$i-1][3] + $boutons[$i-1][4]
EndIf
$boutons[$i][0] = GUICtrlCreateButton($boutons[$i][1], $boutons[$i][3], 15, $boutons[$i][4], 22, -1, $WS_EX_STATICEDGE)
GUICtrlSetBkColor($boutons[$i][0], 0xbadaff)
GUICtrlSetResizing($boutons[$i][0], 256 + 512 + 2)
$context = GUICtrlCreateContextMenu($boutons[$i][0])
$boutons[$i][5] = GUICtrlCreateMenuItem("Ajouter", $context)
$boutons[$i][6] = GUICtrlCreateMenuItem("Enlever", $context)
Next
GUICtrlSetState($label, $GUI_FOCUS)
$guipos1 = WinGetPos($gui)
ControlMove($gui, "", $label, 0, 0, $boutons[UBound($boutons)-1][3]+$boutons[UBound($boutons)-1][4]-2, 15)
WinMove($gui, "", $guipos1[0], $guipos1[1], $boutons[UBound($boutons)-1][3]+$boutons[UBound($boutons)-1][4])
GUISetState()
;_ArrayDisplay($boutons, "0 id ; 1 texte ; 2 action ; 3 left ; 4 largeur ; 5 id ajouter ; 6 id enlever")
While 1
$msg = GUIGetMsg()
For $i = 0 To UBound($boutons) - 1
Switch $msg
Case $boutons[$i][0] ; actions
Call($boutons[$i][2])
GUICtrlSetState($label, $GUI_FOCUS)
Case $boutons[$i][6] ; menu enlever
If $boutons[$i][1] <> "Exit" Then
$decal = $boutons[$i][4] ;size
$guipos = WinGetPos($gui)
ControlMove($gui, "", $label, 0, 0, $guipos[2]-$decal-2, 15)
WinMove($gui, "", $guipos[0], $guipos[1], $guipos[2] - $decal)
For $j = $i + 1 To UBound($boutons) - 1
$boutons[$j][3] = $boutons[$j][3] - $decal
ControlMove($gui, "", $boutons[$j][0], $boutons[$j][3], 15)
GUICtrlSetResizing($boutons[$j][0], 256 + 512 + 2)
Next
GUICtrlSetState($boutons[$i][0], $GUI_HIDE)
ExitLoop
EndIf
Case $boutons[$i][5] ; menu ajouter
$gui2 = GUICreate("titre", 120, 80, -1, -1, $WS_POPUP)
GUISetBkColor(0xbadaff)
$itext = GUICtrlCreateCombo("OK", 10, 10, 100, 18, 0x0003)
For $i = 2 To UBound($boutons) - 1
GUICtrlSetData($itext, $boutons[$i][1] & "|")
Next
$ibtn = GUICtrlCreateButton("OK", 10, 40, 40, 25)
$iexit = GUICtrlCreateButton("Annuler", 60, 40, 50, 25)
GUISetState()
While 1
$msg2 = GUIGetMsg()
Switch $msg2
Case $iexit
GUIDelete($gui2)
ExitLoop
Case $ibtn ; ok
$itexte = GUICtrlRead($itext)
GUIDelete($gui2)
For $i = 0 To UBound($boutons) - 1
If $itexte = $boutons[$i][1] Then
If BitAND(GUICtrlGetState($boutons[$i][0]), $GUI_SHOW) = $GUI_SHOW Then
ExitLoop 2
Else
ExitLoop
EndIf
EndIf
Next
$guipos2 = WinGetPos($gui)
ControlMove($gui, "", $label, 0, 0, $guipos2[2]+$boutons[$i][4]-2, 15)
WinMove($gui, "", $guipos2[0], $guipos2[1], $guipos2[2] + $boutons[$i][4])
GUICtrlSetState($boutons[$i][0], $GUI_SHOW)
For $j = $i + 1 To UBound($boutons) - 1
$boutons[$j][3] += $boutons[$i][4]
ControlMove($gui, "", $boutons[$j][0], $boutons[$j][3], 15)
GUICtrlSetResizing($boutons[$j][0], 256 + 512 + 2)
Next
_WinAPI_RedrawWindow($gui)
ExitLoop
EndSwitch
WEnd
$msg = ""
EndSwitch
Next
WEnd
Func _GetZeSizeOfZisText($mytext)
$hDC = _WinAPI_GetDC($gui)
$tSize = _WinAPI_GetTextExtentPoint32($hDC, $mytext)
_WinAPI_ReleaseDC($gui, $hDC)
Return DllStructGetData($tSize, "X") - StringLen($mytext)*2 + 30
EndFunc
Func _Exit()
Exit
EndFunc
Func _OK()
MsgBox(0, "", "toi y en a cliqué OK")
EndFunc
Func _Enregistrer()
MsgBox(0, "", "toi y en a cliqué Enregistrer")
EndFunc
Func _Tester()
MsgBox(0, "", "toi y en a cliqué Tester")
EndFunc
Func _Newspapa()
MsgBox(0, "", "toi y en a vouloir lire les nouvelles ?")
EndFunc