在 AutoIt 中使用 switch case 中的循环。

huangapple go评论77阅读模式
英文:

loop use in switch case autoit

问题

我有成千上万个小工具,我试图使用AutoIt创建一个菜单。我必须定义成千上万个函数、案例和变量,如何能够更容易地实现呢?
以下是我的代码,如何使用循环来选择案例并运行 soft.exe。

  1. #include <ButtonConstants.au3>
  2. #include <GUIConstantsEx.au3>
  3. #include <WindowsConstants.au3>
  4. $Form1 = GUICreate("MainMenu", 615, 437, 192, 124)
  5. $Soft1 = GUICtrlCreateButton("Button1", -8, 0, 75, 25)
  6. $Soft2 = GUICtrlCreateButton("Button2", -18, 10, 75, 25)
  7. $Soft3 = GUICtrlCreateButton("Button3", -28, 20, 75, 25)
  8. $Soft4 = GUICtrlCreateButton("Button4", -38, 30, 75, 25)
  9. $Soft5 = GUICtrlCreateButton("Button5", -48, 40, 75, 25)
  10. $Soft6 = GUICtrlCreateButton("Button6", -58, 50, 75, 25)
  11. GUISetState(@SW_SHOW)
  12. While 1
  13. $nMsg = GUIGetMsg()
  14. Switch $nMsg
  15. Case $GUI_EVENT_CLOSE
  16. Exit
  17. Case $Soft1
  18. Startsoft1()
  19. Case $Soft2
  20. Startsoft2()
  21. Case $Soft3
  22. Startsoft3()
  23. Case $Soft4
  24. Startsoft4()
  25. Case $Soft5
  26. Startsoft5()
  27. Case $Soft6
  28. Startsoft6()
  29. EndSwitch
  30. WEnd
  31. Func Startsoft1()
  32. Run('Soft1.exe')
  33. EndFunc
  34. Func Startsoft2()
  35. Run('Soft2.exe')
  36. EndFunc
  37. Func Startsoft3()
  38. Run('Soft3.exe')
  39. EndFunc
  40. Func Startsoft4()
  41. Run('Soft4.exe')
  42. EndFunc
  43. Func Startsoft5()
  44. Run('Soft5.exe')
  45. EndFunc

我是编程新手,因此正在学习新的东西。

英文:

I have a thousands mini tools, I am trying to create a menu by using Autuit. I have to define thousand functions and case and variable how i can make it easy.
here is my code how can use loop for select case and run soft.exe

  1. #include &lt;ButtonConstants.au3&gt;
  2. #include &lt;GUIConstantsEx.au3&gt;
  3. #include &lt;WindowsConstants.au3&gt;
  4. $Form1 = GUICreate(&quot;MainMenu&quot;, 615, 437, 192, 124)
  5. $Soft1 = GUICtrlCreateButton(&quot;Button1&quot;, -8, 0, 75, 25)
  6. $Soft2 = GUICtrlCreateButton(&quot;Button2&quot;, -18, 10, 75, 25)
  7. $Soft3 = GUICtrlCreateButton(&quot;Button3&quot;, -28, 20, 75, 25)
  8. $Soft4 = GUICtrlCreateButton(&quot;Button4&quot;, -38, 30, 75, 25)
  9. $Soft5 = GUICtrlCreateButton(&quot;Button5&quot;, -48, 40, 75, 25)
  10. $Soft6 = GUICtrlCreateButton(&quot;Button6&quot;, -58, 50, 75, 25)
  11. GUISetState(@SW_SHOW)
  12. While 1
  13. $nMsg = GUIGetMsg()
  14. Switch $nMsg
  15. Case $GUI_EVENT_CLOSE
  16. Exit
  17. Case $Soft1
  18. Startsoft1()
  19. Case $Soft2
  20. Startsoft2()
  21. Case $Soft3
  22. Startsoft3()
  23. Case $Soft4
  24. Startsoft4()
  25. Case $Soft5
  26. Startsoft5()
  27. Case $Soft6
  28. Startsoft6()
  29. EndSwitch
  30. WEnd
  31. Func Startsoft1()
  32. Run(&#39;Soft1.exe&#39;)
  33. EndFunc
  34. Func Startsoft2()
  35. Run(&#39;Soft2.exe&#39;)
  36. EndFunc
  37. Func Startsoft3()
  38. Run(&#39;Soft3.exe&#39;)
  39. EndFunc
  40. Func Startsoft4()
  41. Run(&#39;Soft4.exe&#39;)
  42. EndFunc
  43. Func Startsoft5()
  44. Run(&#39;Soft5.exe&#39;)
  45. EndFunc

iam new in programing there for learning new things

答案1

得分: 1

尝试这样作为一个很好的起点。在StringSplit方法的字符串中放置名称和路径。如果按钮到达GUI宽度末端,GUI按钮会自动放置在新行。

  1. #include <ButtonConstants.au3>
  2. #include <GUIConstantsEx.au3>
  3. #include <WindowsConstants.au3>
  4. main()
  5. Func main()
  6. Local $gui_width = @DesktopWidth -100, $gui_height = @DesktopHeight - 80
  7. Local $Form1 = GUICreate("MainMenu", $gui_width, $gui_height, 50, 10)
  8. Local $exesName_A = StringSplit('a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe,a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe', ',', 2)
  9. Local $exesPath_A = StringSplit('a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe,a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe', ',', 2)
  10. Local $height = 45, $width = 125, $y = 0, $x = 0
  11. Local $button_A[UBound($exesName_A)]
  12. For $i = 0 To UBound($exesName_A) - 1
  13. If (8 + ($x + 1) * $width) > $gui_width Then
  14. $y += 1
  15. $x = 0
  16. EndIf
  17. $button_A[$i] = GUICtrlCreateButton($exesName_A[$i], 8 + $x * $width, 10 + $y * $height, 75, $height)
  18. GUICtrlSetImage(-1, "shell32.dll", 22+ $i)
  19. $x += 1
  20. Next
  21. GUISetState(@SW_SHOW)
  22. While 1
  23. $nMsg = GUIGetMsg()
  24. Switch $nMsg
  25. Case $GUI_EVENT_CLOSE
  26. Exit
  27. EndSwitch
  28. ; 扫描所有按钮以检查是否有按钮被按下
  29. For $i = 0 To UBound($button_A) - 1
  30. If $nMsg = $button_A[$i] Then
  31. ConsoleWrite('现在运行exe文件' & $exesPath_A[$i] & @CRLF)
  32. ;~ ShellExecute($exesPath_A[$i], '') ; 取消注释以运行exe文件
  33. EndIf
  34. Next
  35. WEnd
  36. EndFunc ;==>main
英文:

Try something like this as a good starting point.
Put the names and pathes in the strings of the method StringSplit.
The GUI buttons are automatically placed in a new line if the button reaches the GUI width end.

  1. #include &lt;ButtonConstants.au3&gt;
  2. #include &lt;GUIConstantsEx.au3&gt;
  3. #include &lt;WindowsConstants.au3&gt;
  4. main()
  5. Func main()
  6. Local $gui_width = @DesktopWidth -100, $gui_height = @DesktopHeight - 80
  7. Local $Form1 = GUICreate(&quot;MainMenu&quot;, $gui_width, $gui_height, 50, 10)
  8. Local $exesName_A = StringSplit(&#39;a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe,a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe&#39;, &#39;,&#39;, 2)
  9. Local $exesPath_A = StringSplit(&#39;a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe,a.exe,b.exe,c.exe,d.exe,e.exe,f.exe,g.exe,h.exe,i.exe,j.exe,k.exe,l.exe,m.exe,n.exe,o.exe&#39;, &#39;,&#39;, 2)
  10. Local $height = 45, $width = 125, $y = 0, $x = 0
  11. Local $button_A[UBound($exesName_A)]
  12. For $i = 0 To UBound($exesName_A) - 1
  13. If ((8 + ($x + 1) * $width) &gt; $gui_width) Then
  14. $y += 1
  15. $x = 0
  16. EndIf
  17. $button_A[$i] = GUICtrlCreateButton($exesName_A[$i], 8 + $x * $width, 10 + $y * $height, 75, $height)
  18. GUICtrlSetImage(-1, &quot;shell32.dll&quot;, 22+ $i)
  19. $x += 1
  20. Next
  21. GUISetState(@SW_SHOW)
  22. While 1
  23. $nMsg = GUIGetMsg()
  24. Switch $nMsg
  25. Case $GUI_EVENT_CLOSE
  26. Exit
  27. EndSwitch
  28. ; scan all buttons to check if one was pressed
  29. For $i = 0 To UBound($button_A) - 1
  30. If $nMsg = $button_A[$i] Then
  31. ConsoleWrite(&#39;Now run the exe&#39; &amp; $exesPath_A[$i] &amp; @CRLF)
  32. ;~ ShellExecute($exesPath_A[$i], &#39;&#39;) ; uncomment this to run the exe
  33. EndIf
  34. Next
  35. WEnd
  36. EndFunc ;==&gt;main

huangapple
  • 本文由 发表于 2023年2月7日 01:00:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定