Advertisement
aveyo

multiple_gui_choices

Aug 17th, 2018
878
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.76 KB | None | 0 0
  1. @echo off &title Multiple GUI choices via powershell snippet by AveYo                 &rem preview: https://i.imgur.com/JjazNR0.png                              
  2. :: setup gui dialog choices
  3. set all_choices=Option 1,Option 2,Option 3,Option4,Option5
  4. set def_choices=Option 1,Option 2,Option5
  5. :: Show gui dialog choices %1:title %2:all_choices %3:def_choices %4:output_variable
  6. call :choices "Multiple GUI choices" "%all_choices%" "%def_choices%" CHOICES
  7. :: Quit if no choice selected
  8. if not defined CHOICES color 0c &echo  ERROR! No choice selected.. &timeout /t 20 &color 07 &exit/b
  9. :: Print choices
  10. echo Choices: %CHOICES% & echo.
  11. :: Process choices
  12. call :process "%CHOICES%"
  13.  
  14. :Done
  15. timeout /t -1
  16. exit/b
  17.  
  18. :"Option 1"
  19. echo running code for %0
  20. rem do stuff here
  21. exit/b
  22. :"Option 2"
  23. echo running code for %0
  24. rem do stuff here
  25. exit/b
  26. :"Option 3"
  27. echo running code for %0
  28. rem do stuff here
  29. exit/b
  30. :"Option4"
  31. echo running code for %0
  32. rem do stuff here
  33. exit/b
  34. :"Option5"
  35. echo running code for %0
  36. rem do stuff here
  37. exit/b
  38.  
  39. :process %1=choices
  40. for /f "tokens=1* delims=," %%a in ("%~1") do if %%a.==. (exit/b) else call :"%%a" &call :process "%%b"
  41. exit/b
  42.  
  43. ::---------------------------------------------------------------------------------------------------------------------------------
  44. :choices %1:title %2:all_choices %3:def_choices %4:output_variable          GUI dialog with autosize checkboxes - outputs %CHOICES%
  45. setlocal &set "parameters=$n='%~1'; $all='%~2'; $def='%~3'; $p='HKCU:\Environment'; $pad=32;"
  46. set "s1=[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); $f=New-Object System.Windows.Forms.Form;"
  47. set "s2=[void][System.Reflection.Assembly]::LoadWithPartialName('System.Drawing'); $f.Forecolor='Black';$f.BackColor='WhiteSmoke';"
  48. set "s3=$r=(Get-ItemProperty $p).$n; if($r -ne $null){$opt=$r.split(',')}else{$opt=$def.split(',')}; function CLK(){ $v=@();"
  49. set "s4=foreach($x in $cb){if($x.Checked){$v+=$x.Text}}; New-ItemProperty -Path $p -Name $n -Value $($v -join ',') -Force };"
  50. set "s5=$BCLK=@(0, {CLK}, {foreach($z in $cb){$z.Checked=$false;if($def.split(',') -contains $z.Text){$z.Checked=$true}};CLK});"
  51. set "s6=$i=1; $cb=foreach($l in $all.split(',')){$c=New-Object System.Windows.Forms.CheckBox; $c.Name='c$i'; $c.AutoSize=$true;"
  52. set "s7=$c.Text=$l; $c.Location=New-Object System.Drawing.Point(($pad*2.5),(16+(($i-1)*24))); $c.BackColor='Transparent';"
  53. set "s8=$c.add_Click({CLK}); $f.Controls.Add($c); $c; $i++; }; foreach($s in $cb){if($opt -contains $s.Text){$s.Checked=$true}};"
  54. set "s9=$j=1; $bn=@('OK','Reset');foreach($t in $bn){ $b=New-Object System.Windows.Forms.Button; $b.BackColor='Transparent';"
  55. set "s10=$b.Location=New-Object System.Drawing.Point(($pad*2+($j-1)*$pad*3),(32+(($i-1)*24))); $b.Margin='0,0,72,20';"
  56. set "s11=$b.add_Click($BCLK[$j]); if ($t -eq 'OK'){$b.DialogResult=1;$f.AcceptButton=$b}; $b.Name='b$j'; $b.Text=$t;"
  57. set "s12=$f.Controls.Add($b);$j++;}; $f.Text=$n; $f.FormBorderStyle='Fixed3D'; $f.AutoSize=$true; $f.AutoSizeMode='GrowAndShrink';"
  58. set "s13=$f.MaximizeBox=$false; $f.StartPosition='CenterScreen'; $f.Add_Shown({$f.Activate()}); $ret=$f.ShowDialog();"
  59. set "s14=if ($ret -ne 1) {Remove-ItemProperty -Path $p -Name $n -Force  -erroraction 'silentlycontinue' | out-null}"
  60. set "s15=else {write-host (Get-ItemProperty -Path $p -Name $n).$n;}"
  61. for /l %%# in (1,1,15) do call set "ps_Choices=%%ps_Choices%%%%s%%#:"=\"%%"                          
  62. for /f "usebackq tokens=* delims=" %%# in (`powershell -c "%parameters% %ps_Choices%"`) do set "choices_var=%%#"
  63. endlocal & set "%~4=%choices_var%" & exit/b &rem snippet by AveYo released under MIT License          
  64. ::---------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement