Code : Tout sélectionner
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiMenu.au3>
#include <ListViewConstants.au3>
#include <Math.au3>
#include <StructureConstants.au3>
#include <WindowsConstants.au3>
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#Tidy_Parameters=/sf
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#Region ;**** Options AutoIt ****
Opt("MustDeclareVars", 1)
Opt("GUICloseOnESC", 1)
Opt("GUIOnEventMode", 0)
Opt("WinSearchChildren", 1)
Opt("WinTextMatchMode", 1)
Opt("WinTitleMatchMode", -2)
#EndRegion ;**** Options AutoIt ****
#Region ;**** Include AutoIt ****
#EndRegion ;**** Include AutoIt ****
#cs
UserCloneVDI est un petit gestionnaire d'utilisateurs pour virtualbox.
Auteur : Laddy
Version : 0.1
Utilisateur - Nom VM associé - Status.
Merci TommyDDR, GaRydelaMer
IniReadSectionName :
[Ex]http://www.autoitscript.fr/forum/viewtopic.php?f=6&t=6347&p=39579&hilit=inireadsectionnames#p39579
[A] viewtopic.php?f=3&t=6701
Projet : Démarrer la VM en fonction de l'utilisateur.
TODOlist :
Mettre des couleurs en fonction du status.
[En cours] --> Vert
[Teminer] --> Rouge
[Suspendu] --> Orange
[Annuler] -->Marron
Double Clic pour lancer la VM. [Ok]
#ce
Global $NomFichierIni = @ScriptDir & "\user.ini"
Global $GUI, $User, $VM, $Etat, $Path, $ListView1, $hWnd_ListView
Global $bt_Add, $bt_Delete, $bt_Update, $bt_Close, $bt_Path, $iCode, $iItem, $PathVirtualBox, $Osarch
Global Enum $idCmd_Delete = 10000, $idCmd_Print
If @OSArch = "X64" Then $Osarch = "64"
;If StringRight($vbox_path, 1) = "\" Then $vbox_path = StringRegExpReplace($vbox_path, "\\$", "")
If RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Oracle\VirtualBox", "InstallDir") Then
$PathVirtualBox = RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Oracle\VirtualBox", "InstallDir")
ElseIf RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Sun\VirtualBox", "InstallDir") Then
$PathVirtualBox = RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Sun\VirtualBox", "InstallDir")
ElseIf RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Sun\xVM VirtualBox", "InstallDir") Then
$PathVirtualBox = RegRead("HKEY_LOCAL_MACHINE" & $Osarch & "\SOFTWARE\Sun\xVM VirtualBox", "InstallDir")
Else
MsgBox(16, "VirtualBox", "Erreur d'installation ou le logiciel n'est pas installé sur ce poste.")
Exit
EndIf
#Region ### START Koda GUI section ### Form=D:\Téléchargements\koda_1.7.3.0\Forms\UserCloneVDI.kxf
$GUI = GUICreate("UserCloneVDI", 634, 474, 192, 124)
GUICtrlCreateLabel("Utilisateur :", 104, 24, 56, 17)
$User = GUICtrlCreateInput("", 184, 24, 185, 21)
GUICtrlCreateLabel("Nom de la machine virtuelle :", 16, 64, 143, 17)
$VM = GUICtrlCreateInput("", 184, 64, 185, 21)
GUICtrlCreateLabel("Status :", 408, 32, 40, 17)
$Etat = GUICtrlCreateCombo("", 472, 24, 145, 25, BitOR($GUI_SS_DEFAULT_COMBO, $CBS_SIMPLE))
GUICtrlCreateLabel("Chemin", 112, 112, 39, 17)
$Path = GUICtrlCreateInput("", 184, 104, 185, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_READONLY))
$bt_Path = GUICtrlCreateButton("...", 377, 104, 25, 25)
$ListView1 = GUICtrlCreateListView("", 8, 170, 610, 265)
$bt_Add = GUICtrlCreateButton("Ajouter", 293, 133, 100, 25)
$bt_Delete = GUICtrlCreateButton("Supprimer", 514, 139, 100, 25)
$bt_Update = GUICtrlCreateButton("Appliquer", 184, 134, 100, 25)
$bt_Close = GUICtrlCreateButton("Fermer", 551, 440, 75, 25)
_GUI_Init()
GUISetState(@SW_SHOW)
GUIRegisterMsg($WM_NOTIFY, "_Events_WM_NOTIFY")
#EndRegion ### END Koda GUI section ###
Local $nMsg
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $bt_Close, $GUI_EVENT_CLOSE
Exit
Case $bt_Path
GUI_BrowseDst()
Case $bt_Add
User_Add()
Case $bt_Delete
User_Delete()
Case $bt_Update
User_Update()
EndSwitch
Sleep(10)
WEnd
Func _Events_ContextMenu($hWnd, $iCode)
If $hWnd = $hWnd_ListView Then
Switch $iCode
Case $idCmd_Delete
User_Delete()
Case $idCmd_Print
;
EndSwitch
EndIf
EndFunc ;==>_Events_ContextMenu
Func _Events_WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
#forceref $hWnd, $Msg, $wParam, $lParam
Local $tInfo, $Code
$tInfo = DllStructCreate($tagNMHDR, $lParam)
$hWnd = HWnd(DllStructGetData($tInfo, "hWndFrom"))
$iCode = DllStructGetData($tInfo, "Code")
If $hWnd = $hWnd_ListView Then
Switch $iCode
Case $LVN_COLUMNCLICK
;
[color=#008000]Case $NM_CLICK
GUI_UserView()
Case $NM_DBLCLK
Start_Vm()[/color]
Case $NM_RCLICK
GUI_CreateContextMenu($hWnd)
EndSwitch
EndIf
Return $GUI_RUNDEFMSG
EndFunc ;==>_Events_WM_NOTIFY
Func _GUI_Init()
If Not FileExists($NomFichierIni) Then
IniWrite($NomFichierIni, "Utilisateur_001", "Nom", "A définir")
EndIf
[color=#FF0000]GUICtrlSetData($Etat, "Ouverte|Fermer|Suspendu|Annuler")[/color]
$hWnd_ListView = GUICtrlGetHandle($ListView1)
_GUICtrlListView_AddColumn($hWnd_ListView, "Utilisateur", 150)
_GUICtrlListView_AddColumn($hWnd_ListView, "Nom", 150)
_GUICtrlListView_AddColumn($hWnd_ListView, "VM", 100)
_GUICtrlListView_AddColumn($hWnd_ListView, "Status", 100) ; Liste déroulante ? [Ouverte], [Fermer], [Suspendu], [Annuler]
_GUICtrlListView_AddColumn($hWnd_ListView, "Chemin", 100) ; chemin VM
Local $aSections, $iItem, $aUser
$aSections = IniReadSectionNames($NomFichierIni)
For $i = 1 To $aSections[0]
$iItem = _GUICtrlListView_AddItem($hWnd_ListView, $aSections[$i], 0)
$aUser = IniReadSection($NomFichierIni, $aSections[$i])
For $iSubItem = 1 To $aUser[0][0]
_GUICtrlListView_AddSubItem($hWnd_ListView, $iItem, $aUser[$iSubItem][1], $iSubItem)
Next
Next
_GUICtrlListView_SetItemSelected($hWnd_ListView, 0, True, True)
GUI_UserView()
EndFunc ;==>_GUI_Init
Func GUI_BrowseDst()
Local $message = "Sélectionne la destination de ton fichier VDI."
Local $var = FileOpenDialog($message, @DesktopDir & "\", "Fichiers (*.vdi)")
GUICtrlSetData($Path, $var)
EndFunc ;==>GUI_BrowseDst
Func GUI_CreateContextMenu($hWnd)
Local $aHit = _GUICtrlListView_HitTest($hWnd), $Item = $aHit[0]
Local $hMenu = _GUICtrlMenu_CreatePopup(1)
_GUICtrlMenu_AddMenuItem($hMenu, "Supprimer", $idCmd_Delete)
_GUICtrlMenu_AddMenuItem($hMenu, "Imprimer", $idCmd_Print); exemple pour l'utilisation de Enum
; ========================================================================
; Shows how to capture the context menu selections
; ========================================================================
Local $idCommand = _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd, -1, -1, 1, 1, 2)
_GUICtrlMenu_DestroyMenu($hMenu)
_Events_ContextMenu($hWnd, $idCommand)
EndFunc ;==>GUI_CreateContextMenu
#cs
Func GUI_UserView()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hWnd_ListView, True)
If $aSelected[0] > 0 Then
Local $iItem
$iItem = $aSelected[1]
GUICtrlSetData($User, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 1))
GUICtrlSetData($VM, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 2))
If _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3) Then
GUICtrlSetData($Etat, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3))
Else
[color=#408000]GUICtrlSetData($Etat, "Ouverte")[/color]
EndIf
GUICtrlSetData($Path, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 4))
EndIf
EndFunc ;==>GUI_UserView
#ce
[color=#BF4080]
;ajout des couleurs dans la combobox
Func GUI_UserView()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hWnd_ListView, True)
If $aSelected[0] > 0 Then
Local $iItem
$iItem = $aSelected[1]
GUICtrlSetData($User, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 1))
GUICtrlSetData($VM, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 2))
If _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3) = "Ouverte" Then
GUICtrlSetColor($Etat, 0x00ff00) ;vert
GUICtrlSetData($Etat, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3))
EndIF
If _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3) = "Fermer" Then
GUICtrlSetColor($Etat, 0xff0000) ;rouge
GUICtrlSetData($Etat, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3))
EndIf
If _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3) = "Suspendu" Then
GUICtrlSetColor($Etat, 0xFFCC00) ;jaune orangé
GUICtrlSetData($Etat, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3))
EndIf
If _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3) = "Annuler" Then
GUICtrlSetColor($Etat, 0x0033CC) ;bleu
GUICtrlSetData($Etat, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 3))
Endif
GUICtrlSetData($Path, _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 4))
EndIf
EndFunc ;==>GUI_UserView[/color]
[color=#008000]Func Start_Vm()
Local $aSections, $iItem, $aUser
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hWnd_ListView, True)
If $aSelected[0] > 0 Then
Local $iItem
$iItem = $aSelected[1]
Local $Run_VM = _GUICtrlListView_GetItemText($hWnd_ListView, $iItem, 2)
EndIf
Run('"' & $PathVirtualBox & 'VBoxManage.exe" startvm ' & $Run_VM, $PathVirtualBox, @SW_HIDE)
;ConsoleWrite('"' & $PathVirtualBox & 'VBoxManage.exe" startvm ' & $Run_VM)
EndFunc ;==>Start_Vm[/color]
Func User_Add()
Local $sSection, $iCount = _GUICtrlListView_GetItemCount($hWnd_ListView) + 1
While @error = 0
$sSection = "Utilisateur_" & StringFormat("%03s", $iCount)
$iCount += 1
IniReadSection($NomFichierIni, $sSection)
WEnd
IniWrite($NomFichierIni, $sSection, "Nom", GUICtrlRead($User))
IniWrite($NomFichierIni, $sSection, "Vbox", GUICtrlRead($VM))
IniWrite($NomFichierIni, $sSection, "Status", GUICtrlRead($Etat))
IniWrite($NomFichierIni, $sSection, "Chemin", GUICtrlRead($Path))
Local $iItem = _GUICtrlListView_AddItem($hWnd_ListView, $sSection)
Local $aUser = IniReadSection($NomFichierIni, $sSection)
For $iSubItem = 1 To $aUser[0][0]
_GUICtrlListView_AddSubItem($hWnd_ListView, $iItem, $aUser[$iSubItem][1], $iSubItem)
Next
_GUICtrlListView_SetItemSelected($hWnd_ListView, 0, True, True)
EndFunc ;==>User_Add
Func User_Delete()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hWnd_ListView, True)
If $aSelected[0] > 0 Then
Local $iItem, $sSection
$iItem = $aSelected[1]
$sSection = _GUICtrlListView_GetItemText($hWnd_ListView, $iItem)
_GUICtrlListView_DeleteItem($hWnd_ListView, $iItem)
IniDelete($NomFichierIni, $sSection)
$iItem = _Max(0, $iItem - 1)
_GUICtrlListView_SetItemSelected($hWnd_ListView, $iItem)
GUI_UserView()
EndIf
EndFunc ;==>User_Delete
Func User_Update()
Local $aSelected = _GUICtrlListView_GetSelectedIndices($hWnd_ListView, True)
If $aSelected[0] > 0 Then
Local $iItem = $aSelected[1]
Local $sSection = _GUICtrlListView_GetItemText($hWnd_ListView, $iItem)
IniWrite($NomFichierIni, $sSection, "Nom", GUICtrlRead($User))
IniWrite($NomFichierIni, $sSection, "Vbox", GUICtrlRead($VM))
IniWrite($NomFichierIni, $sSection, "Status", GUICtrlRead($Etat))
IniWrite($NomFichierIni, $sSection, "Chemin", GUICtrlRead($Path))
Local $aUser = IniReadSection($NomFichierIni, $sSection)
For $iSubItem = 1 To $aUser[0][0]
_GUICtrlListView_SetItemText($hWnd_ListView, $iItem, $aUser[$iSubItem][1], $iSubItem)
Next
EndIf
EndFunc ;==>User_Update