Hi Speedy86 .
Das was du suchst sind Arrays. Ich hab dein Script mal umgeschrieben, sodass es arrays nutzt. Du musst theoretisch nurnoch die globalen variablen anpassen und es sollte tun, was du möchtest und du kannst beliebig viele TVs und ContextMenuItems hinzufügen. Außerdem kannst du während das Tool läuft neue Tvs hinzufügen.
Alle Positionen der Controls werden dynamisch berechnet, sodass es erweiterbar ist und die größe geändert werden kann.
C
#include <GUIConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#Region global variables
;TV information
Global $iDefaultPort=8080
Global $arTvList=[["192.168.0.10",$iDefaultPort],["192.168.0.11",$iDefaultPort],["192.168.0.12",$iDefaultPort]] ;All client IP´s
;Gui control variables
Global $iWidth=1000,$iHeight=750
; [x position,y position]
Global $arPointTvListStart=[10,10]
Global $iTvListItemWidth=60, $iTvListButtonHeight=20, $iTvListButtonSpace=10
; [FB_Button,Ch_Button,Ch_Context,Item 1,Item 2,Item 3,Item 4,Item 5, Item 6]
Global $sRestart="Restart"
Global $arContextMenuButtons=[ _
["Test 1","Telnet command 1"], _
["Test 2","Telnet command 2"], _
["Test 3","Telnet command 3"], _
["Test 4","Telnet command 4"], _
["Test 5","Telnet command 5"], _
["",0], _
[$sRestart,"Telnet restart"] _
]
Global $iStartOfContextMenuButtons=3
Global $arTvListControls[0][0][2]
#EndRegion global variables
$hGUI = GUICreate("Test",$iWidth,$iHeight)
$iButtonAddTV=GUICtrlCreateButton("Add TV",100,100)
$iButtonRemoveTV=GUICtrlCreateButton("Remove TV",100,130)
_refreshTvButtons()
GUISetState(@SW_SHOW)
While 1 ;Sleep(10) not needed, because guigetmsg sleeps
$nMsg = GUIGetMsg()
If $nMsg<>0 Then
If $nMsg=$GUI_EVENT_CLOSE Then Exit
Switch $nMsg
case $iButtonAddTV
_addTV("192.168.2.111")
case $iButtonRemoveTV
_removeTV("192.168.2.111")
EndSwitch
For $i=0 to UBound($arTvListControls)-1 step 1
For $j=0 to UBound($arTvListControls,2)-1 step 1
If $arTvListControls[$i][$j][0]=$nMsg Then
Switch $j
case 0 ; FB Button not used
case 1 ; CH Button
$arTvListControls[$i][$j][1]($hGUI,$nMsg,$arTvListControls[$i][$j+1][0])
case Else ;all contextmenu items
telnet_send($arTvList[$i][0],$arTvList[$i][1],$arTvListControls[$i][$j][1])
GUICtrlSetData($arTvListControls[$i][1][0],$arContextMenuButtons[$j-$iStartOfContextMenuButtons][0])
If $arContextMenuButtons[$j-$iStartOfContextMenuButtons][0]=$sRestart Then
GUICtrlSetData($arTvListControls[$i][1][0],"")
EndIf
EndSwitch
ExitLoop 2
EndIf
Next
Next
EndIf
WEnd
#Region Func
Func _addTV($sIp,$iPort=8080)
_ArrayAdd($arTvList,$sIp&"|"&$iPort)
_refreshTvButtons()
EndFunc
Func _removeTV($sIp,$iPort=8080)
For $i=UBound($arTvList)-1 to 0 step -1
If $arTvList[$i][0]=$sIp and $arTvList[$i][1]=$iPort Then
_ArrayDelete($arTvList,$i)
EndIf
Next
_refreshTvButtons()
EndFunc
Func _refreshTvButtons()
;delete all controls
For $i=0 to UBound($arTvListControls)-1 step 1
For $j=0 to UBound($arTvListControls,2)-1 step 1
GUICtrlDelete($arTvListControls[$i][$j][0])
Next
Next
;resize control array
ReDim $arTvListControls[UBound($arTvList)][$iStartOfContextMenuButtons+UBound($arContextMenuButtons)][2]
;create new controls
For $i=0 to UBound($arTvList)-1 step 1
$arTvListControls[$i][0][0]=GUICtrlCreateButton("TV "&($i+1),$arPointTvListStart[0]+($i*($iTvListItemWidth+$iTvListButtonSpace)),$arPointTvListStart[1],$iTvListItemWidth,$iTvListButtonHeight)
GUICtrlSetFont($arTvListControls[$i][0][0], 8.5, 700, 0, "")
$arTvListControls[$i][1][0]=GUICtrlCreateButton("",$arPointTvListStart[0]+($i*($iTvListItemWidth+$iTvListButtonSpace)),$arPointTvListStart[1]+$iTvListButtonHeight,$iTvListItemWidth,$iTvListButtonHeight)
$arTvListControls[$i][1][1]=ShowMenu
GUICtrlSetFont($arTvListControls[$i][1][0], 8.5, 500, 0, "")
$arTvListControls[$i][2][0]=GUICtrlCreateContextMenu($arTvListControls[$i][1][0])
For $j=0 to UBound($arContextMenuButtons)-1 step 1
$arTvListControls[$i][$iStartOfContextMenuButtons+$j][0]=GUICtrlCreateMenuItem($arContextMenuButtons[$j][0],$arTvListControls[$i][2][0])
$arTvListControls[$i][$iStartOfContextMenuButtons+$j][1]=$arContextMenuButtons[$j][1]
Next
Next
EndFunc
;Telnet_send
Func telnet_send($sIp,$iPort,$sCommand)
TCPStartup ()
$Conection=TCPConnect($sIp,$iPort)
If @error Then
MsgBox (0,"Error","Could not establish connection to: "&$sIp&":"&$iPort)
return;
EndIf
TCPSend($Conection,$sCommand&Chr(10))
If @error Then
MsgBox(0,"Error","Could not send the command: "&$sCommand&" to "&$sIp&":"&$iPort)
EndIf
EndFunc
; End Telnet_send
Func ShowMenu($hWnd, $CtrlID, $nContextID)
Local $arPos, $x, $y
Local $hMenu = GUICtrlGetHandle($nContextID)
$arPos = ControlGetPos($hWnd, "", $CtrlID)
$x = $arPos[0]
$y = $arPos[1]+$arPos[3]
ClientToScreen($hWnd, $x, $y)
TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc ;ShowMenu
; Convert the client GUI coordinates to screen desktop coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
Local $stPoint = DllStructCreate("int;int")
DllStructSetData($stPoint, 1, $x)
DllStructSetData($stPoint, 2, $y)
DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
$x = DllStructGetData($stPoint, 1)
$y = DllStructGetData($stPoint, 2)
; release Struct not really needed as it is a local
$stPoint = 0
EndFunc ;==>ClientToScreen
; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc ;==>TrackPopupMenu
#EndRegion Func
Alles anzeigen
MfG Kanashius