英文:
loop use in switch case autoit
问题
我有成千上万个小工具,我试图使用AutoIt创建一个菜单。我必须定义成千上万个函数、案例和变量,如何能够更容易地实现呢?
以下是我的代码,如何使用循环来选择案例并运行 soft.exe。
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("MainMenu", 615, 437, 192, 124)
$Soft1 = GUICtrlCreateButton("Button1", -8, 0, 75, 25)
$Soft2 = GUICtrlCreateButton("Button2", -18, 10, 75, 25)
$Soft3 = GUICtrlCreateButton("Button3", -28, 20, 75, 25)
$Soft4 = GUICtrlCreateButton("Button4", -38, 30, 75, 25)
$Soft5 = GUICtrlCreateButton("Button5", -48, 40, 75, 25)
$Soft6 = GUICtrlCreateButton("Button6", -58, 50, 75, 25)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Soft1
Startsoft1()
Case $Soft2
Startsoft2()
Case $Soft3
Startsoft3()
Case $Soft4
Startsoft4()
Case $Soft5
Startsoft5()
Case $Soft6
Startsoft6()
EndSwitch
WEnd
Func Startsoft1()
Run('Soft1.exe')
EndFunc
Func Startsoft2()
Run('Soft2.exe')
EndFunc
Func Startsoft3()
Run('Soft3.exe')
EndFunc
Func Startsoft4()
Run('Soft4.exe')
EndFunc
Func Startsoft5()
Run('Soft5.exe')
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
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form1 = GUICreate("MainMenu", 615, 437, 192, 124)
$Soft1 = GUICtrlCreateButton("Button1", -8, 0, 75, 25)
$Soft2 = GUICtrlCreateButton("Button2", -18, 10, 75, 25)
$Soft3 = GUICtrlCreateButton("Button3", -28, 20, 75, 25)
$Soft4 = GUICtrlCreateButton("Button4", -38, 30, 75, 25)
$Soft5 = GUICtrlCreateButton("Button5", -48, 40, 75, 25)
$Soft6 = GUICtrlCreateButton("Button6", -58, 50, 75, 25)
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Soft1
Startsoft1()
Case $Soft2
Startsoft2()
Case $Soft3
Startsoft3()
Case $Soft4
Startsoft4()
Case $Soft5
Startsoft5()
Case $Soft6
Startsoft6()
EndSwitch
WEnd
Func Startsoft1()
Run('Soft1.exe')
EndFunc
Func Startsoft2()
Run('Soft2.exe')
EndFunc
Func Startsoft3()
Run('Soft3.exe')
EndFunc
Func Startsoft4()
Run('Soft4.exe')
EndFunc
Func Startsoft5()
Run('Soft5.exe')
EndFunc
iam new in programing there for learning new things
答案1
得分: 1
尝试这样作为一个很好的起点。在StringSplit方法的字符串中放置名称和路径。如果按钮到达GUI宽度末端,GUI按钮会自动放置在新行。
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
main()
Func main()
Local $gui_width = @DesktopWidth -100, $gui_height = @DesktopHeight - 80
Local $Form1 = GUICreate("MainMenu", $gui_width, $gui_height, 50, 10)
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)
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)
Local $height = 45, $width = 125, $y = 0, $x = 0
Local $button_A[UBound($exesName_A)]
For $i = 0 To UBound($exesName_A) - 1
If (8 + ($x + 1) * $width) > $gui_width Then
$y += 1
$x = 0
EndIf
$button_A[$i] = GUICtrlCreateButton($exesName_A[$i], 8 + $x * $width, 10 + $y * $height, 75, $height)
GUICtrlSetImage(-1, "shell32.dll", 22+ $i)
$x += 1
Next
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
; 扫描所有按钮以检查是否有按钮被按下
For $i = 0 To UBound($button_A) - 1
If $nMsg = $button_A[$i] Then
ConsoleWrite('现在运行exe文件' & $exesPath_A[$i] & @CRLF)
;~ ShellExecute($exesPath_A[$i], '') ; 取消注释以运行exe文件
EndIf
Next
WEnd
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.
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
main()
Func main()
Local $gui_width = @DesktopWidth -100, $gui_height = @DesktopHeight - 80
Local $Form1 = GUICreate("MainMenu", $gui_width, $gui_height, 50, 10)
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)
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)
Local $height = 45, $width = 125, $y = 0, $x = 0
Local $button_A[UBound($exesName_A)]
For $i = 0 To UBound($exesName_A) - 1
If ((8 + ($x + 1) * $width) > $gui_width) Then
$y += 1
$x = 0
EndIf
$button_A[$i] = GUICtrlCreateButton($exesName_A[$i], 8 + $x * $width, 10 + $y * $height, 75, $height)
GUICtrlSetImage(-1, "shell32.dll", 22+ $i)
$x += 1
Next
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
; scan all buttons to check if one was pressed
For $i = 0 To UBound($button_A) - 1
If $nMsg = $button_A[$i] Then
ConsoleWrite('Now run the exe' & $exesPath_A[$i] & @CRLF)
;~ ShellExecute($exesPath_A[$i], '') ; uncomment this to run the exe
EndIf
Next
WEnd
EndFunc ;==>main
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论