通过for循环创建的按钮怎么设置对应的点击事件【已解决】
本帖最后由 梦倾天下 于 2024-4-15 13:06 编辑如图,通过for循环创建的按钮及进度条,怎么设置对应的点击事件和进度条同步操作,各位大神帮帮忙
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Example()
Func Example()
;~ GUICreate("My GUI") ; start the definition
;~ GUISetState(@SW_SHOW) ; will display an empty dialog box
; 初始化GUI
Local $hWnd = GUICreate("按钮创建示例", 400, 300)
GUISetState(@SW_SHOW)
Local $buttonWidth = 100 ; 按钮宽度
Local $buttonHeight = 30 ; 按钮高度
Local $buttonPadding = 20 ; 按钮之间的间距
Local $currentX = $buttonPadding ; 初始化X坐标为按钮间距
Local $currentY = $buttonPadding ; 初始化Y坐标为按钮间距
Local $buttonsPerRow = 3 ; 每行按钮数量
Local $buttonCount = 0 ; 按钮计数器
; 假设我们要创建9个按钮
Local $totalButtons = 9
; 循环创建按钮
For $i = 1 To $totalButtons
$buttonCount += 1
Local $buttonText = "按钮 " & $i
; 检查是否需要换行(即当前按钮是否超出每行按钮数量)
If Mod($buttonCount , $buttonsPerRow) = 1 Then
$currentX = $buttonPadding ; 重置X坐标到起始位置
$currentY += $buttonHeight + $buttonPadding ; 增加Y坐标以开始新的一行
EndIf
; 创建按钮及进度条并设置其位置
Local $hButton = GUICtrlCreateButton($buttonText, $currentX, $currentY, $buttonWidth, $buttonHeight)
Local $hProgress = GUICtrlCreateProgress($currentX, $currentY+$buttonHeight, $buttonWidth, 5) ;进度条
; 增加X坐标以准备放置下一个按钮
$currentX += $buttonWidth + $buttonPadding
Next
; Loop until the user exits.
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
WEnd
EndFunc ;==>Example
$totalButtons ->> Global $TotalButtons callmekq 发表于 2024-4-11 15:26
$totalButtons ->> Global $TotalButtons
能细说嘛?若是有很多个按钮怎么搞哇 创建按钮时要为每一个按钮指定一个变量,你这创建出来的只有最后一个按钮是$hButton,前面的都没了... [已解决]多标签批量创建的按钮如何捕捉消息?
https://www.autoitx.com/thread-45295-1-1.html
找到我N年前发的帖子,撸主参考一下...
Assign与 Eval
这个两个东东,是个好东东
绿色风 发表于 2024-4-11 22:32
Assign() 创建或写入一个变量, 然后使用 Eval() 函数读取该变量, 并使用 IsDeclared() 函数检查该变量是否存在.
高手啊
Example()
Func Example()
; 初始化GUI
Local $hWnd = GUICreate("按钮创建示例", 400, 300)
GUISetState(@SW_SHOW)
Local $buttonWidth = 100 ; 按钮宽度
Local $buttonHeight = 30 ; 按钮高度
Local $buttonPadding = 20 ; 按钮之间的间距
Local $currentX = $buttonPadding ; 初始化X坐标为按钮间距
Local $currentY = $buttonPadding ; 初始化Y坐标为按钮间距
Local $buttonsPerRow = 3 ; 每行按钮数量
Local $buttonCount = 0 ; 按钮计数器
; 假设我们要创建9个按钮
Local $totalButtons = 9
Local $buttonText, $aButton[$totalButtons]
; 循环创建按钮
For $i = 1 To $totalButtons
$buttonCount += 1
$buttonText = "按钮 " & $i
; 检查是否需要换行(即当前按钮是否超出每行按钮数量)
If Mod($buttonCount, $buttonsPerRow) = 1 Then
$currentX = $buttonPadding ; 重置X坐标到起始位置
$currentY += $buttonHeight + $buttonPadding ; 增加Y坐标以开始新的一行
EndIf
; 创建按钮及进度条并设置其位置
$aButton[$i - 1] = GUICtrlCreateButton($buttonText, $currentX, $currentY, $buttonWidth, $buttonHeight)
GUICtrlCreateProgress($currentX, $currentY + $buttonHeight, $buttonWidth, 10) ;进度条
; 增加X坐标以准备放置下一个按钮
$currentX += $buttonWidth + $buttonPadding
Next
; Loop until the user exits.
Local $iMsg
While 1
$iMsg = GUIGetMsg()
Switch $iMsg
Case -3
ExitLoop
Case $aButton To $aButton[$totalButtons - 1]
GUICtrlSetData($iMsg + 1, 100)
EndSwitch
WEnd
EndFunc ;==>Example afan 发表于 2024-4-12 18:18
这代码也好玩,{:3_86:} afan 发表于 2024-4-12 18:18
AFUN还是介于牛A与牛C之间...
------------------------- afan 发表于 2024-4-12 18:18
A大YYDS,就是我有些迷惑,$iMsg可以获取按钮的点击事件,为啥GUICtrlSetData($iMsg + 1, 100)确能关联到进度条控件的 梦倾天下 发表于 2024-4-15 10:30
A大YYDS,就是我有些迷惑,$iMsg可以获取按钮的点击事件,为啥GUICtrlSetData($iMsg + 1, 100)确能关联到 ...
$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐标的进度条控件,因此 进度条控件ID 就是 按钮ID + 1,即 $iMsg + 1 afan 发表于 2024-4-15 11:00
$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐 ...
明白了,非常感谢 afan 发表于 2024-4-15 11:00
$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐 ...
骚,够骚...
------------------------------- 邪恶海盗 发表于 2024-4-15 11:20
骚,够骚...
-------------------------------
这只是基本骚操作:face (24):
页:
[1]