Question menu 3 options

Plus d'informations
il y a 6 ans 6 mois #24390 par crogiez
menu 3 options a été créé par crogiez
[code:1]
#powershell menu 3 options - november 2015
#philippe crogiez - french english
#http://crogiez.free.fr - Cette adresse e-mail est protégée contre les robots spammeurs. Vous devez activer le JavaScript pour la visualiser.
# oct 2017 - affiche perpetuel

[void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.Drawing\"«»)
[void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)
[void] [System.Windows.Forms.Application]::EnableVisualStyles()

function ecrit_log($llogf){

#rem create log
$llog=get-date -Format \"yyyy-MM-dd-HH-mm-ss\"
$llog=$llog+\";\"+$llogf
$llog
$llog >> $malog
}


function Affiche_Menu{

$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(400,200)
$form.MaximizeBox = $false
$Form.StartPosition = \"CenterScreen\"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = \"Mon titre\"

#icon en haut a gauche
$path_icon=$mondossier+\"bing.ico\"

#$formIcon = New-Object system.drawing.icon ($path_icon)
#$form.Icon = $formicon

$Font = New-Object System.Drawing.Font(\"Arial\",15,[System.Drawing.FontStyle]::Bold)
$form.Font = $Font

$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = \"Quel est votre choix ?\"
$Label1.AutoSize = $true
$Form.Controls.Add($Label1)

$O1button = New-Object System.Windows.Forms.Button
$O1button.Location = New-Object System.Drawing.Size(140,40)
$O1button.Size = New-Object System.Drawing.Size(120,30)
$O1button.Text = \"Option1\"
$O1button.Add_Click({$form.Close();$arr_menu[0]=1})
$Form.Controls.Add($O1button)

$O2button = New-Object System.Windows.Forms.Button
$O2button.Location = New-Object System.Drawing.Size(140,70)
$O2button.Size = New-Object System.Drawing.Size(120,30)
$O2button.Text = \"Option2\"
$O2button.Add_Click({$form.Close();$arr_menu[0]=2})
$Form.Controls.Add($O2button)

$O3button = New-Object System.Windows.Forms.Button
$O3button.Location = New-Object System.Drawing.Size(140,100)
$O3button.Size = New-Object System.Drawing.Size(120,30)
$O3button.Text = \"Exit\"
$O3button.Add_Click({$form.Close();$arr_menu[0]=3})
$Form.Controls.Add($O3button)

$Label2 = New-Object System.Windows.Forms.Label
$Label2.Location = New-Object System.Drawing.Size(10,130)
$Label2.Text = $arr_menu[1]
$Label2.AutoSize = $true
$Form.Controls.Add($Label2)

$Form.ShowDialog()

}

#recuperation du dossier courant
$monchemin=$MyInvocation.InvocationName
$monchemin
$mondossier=(split-path -Path $monchemin -Parent) + \"\\"
$mondossier
$monscript=split-path -path $monchemin -leaf
$monscript
$malog=$mondossier+$monscript.Substring(0,$monscript.IndexOf(\".\"«»))+\".log\"
$malog

#rem create log
$mlog=\"
\"; ecrit_log($mlog)
$mlog=$monchemin; ecrit_log($mlog)

#on affiche le menu tout le temps
#jusqua option Exit
$affiche_menu=$true

#il faut passer l'arg dans un tableau
#alors l'arg est passé et reçu par référence
#ici l'arg n'est pas passé, il est contenu dans tableau
#et la fonction Option* le modifie
$arr_menu=@()
$arr_menu+=\"\";$arr_menu[0]=0
$arr_menu+=\"\";$arr_menu[1]=\"message\"

while ($affiche_menu -eq $true)
{

Affiche_Menu

\"apres saisi menu\"
$arr_menu[0]
$mlog=$arr_menu[0]; ecrit_log($mlog)

if($arr_menu[0] -eq 0){
\"Vous n'avez rien choisi\"
$mlog=\"Vous n'avez rien choisi\"; ecrit_log($mlog)
}

if($arr_menu[0] -eq 1){
\"Vous avez choisi le 1\"
$mlog=\"Vous avez choisi le 1\"; ecrit_log($mlog)
$arr_menu[1]=$mlog
}

if($arr_menu[0] -eq 2){
\"Vous avez choisi le 2\"
$mlog=\"Vous avez choisi le 2\"; ecrit_log($mlog)
$arr_menu[1]=$mlog
}

if($arr_menu[0] -eq 3){
\"Exit\"
$mlog=\"Exit\"; ecrit_log($mlog)
$affiche_menu=$false
}
}

#rem fin log

$mlog=\"end\"; ecrit_log($mlog)

[/code:1]
Pièces jointes :

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 6 ans 6 mois #24396 par Arnaud Petitjean
Réponse de Arnaud Petitjean sur le sujet Re:menu 3 options
Bonjour Philippe,

Merci pour ta contribution ;).

Il aurait été pas mal que tu donnes quelques explications associées à ton script.

Par ailleurs, je vois que tu utilises WinForms. Je pense qu'il serait mieux que tu utilises à la place WPF.

En effet, WPF est une techno plus moderne que Windows Forms et c'est bien plus facile, concis, et tu as plein d'éditeurs pour créer ton interface graphique.

Si tu veux des exemples pour t'inspirer, je te conseille de télécharger les exemples (c'est gratuit et en libre accès) de notre livre sur \"Les fonctionnalités avancées\" (regarde dans la colonne de gauche du site).
Ensuite je t'invite à comparer les exemples Winforms et WPF du générateur de mots de passe.

@+
Arnaud

MVP PowerShell et créateur de ce magnifique forum :-)
Auteur de 6 livres PowerShell aux éditions ENI
Fondateur de la société Start-Scripting
Besoin d'une formation PowerShell ?

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.070 secondes
Propulsé par Kunena