找回密码
 加入
搜索
查看: 290|回复: 14

[AU3基础] 通过for循环创建的按钮怎么设置对应的点击事件【已解决】

[复制链接]
发表于 2024-4-11 15:20:01 | 显示全部楼层 |阅读模式
本帖最后由 梦倾天下 于 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


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2024-4-11 15:26:19 | 显示全部楼层
$totalButtons ->> Global $TotalButtons[9]
 楼主| 发表于 2024-4-11 16:28:48 | 显示全部楼层
callmekq 发表于 2024-4-11 15:26
$totalButtons ->> Global $TotalButtons[9]

能细说嘛?若是有很多个按钮怎么搞哇
发表于 2024-4-11 20:18:35 | 显示全部楼层
创建按钮时要为每一个按钮指定一个变量,你这创建出来的只有最后一个按钮是$hButton,前面的都没了...
发表于 2024-4-11 20:34:33 | 显示全部楼层
[已解决]多标签批量创建的按钮如何捕捉消息?
https://www.autoitx.com/thread-45295-1-1.html

找到我N年前发的帖子,撸主参考一下...
发表于 2024-4-11 22:32:03 | 显示全部楼层

Assign  与 Eval
这个两个东东,是个好东东
发表于 2024-4-12 08:39:10 | 显示全部楼层

Assign() 创建或写入一个变量, 然后使用 Eval() 函数读取该变量, 并使用 IsDeclared() 函数检查该变量是否存在.

高手啊
发表于 2024-4-12 18:18:08 | 显示全部楼层
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[0] To $aButton[$totalButtons - 1]
                                GUICtrlSetData($iMsg + 1, 100)
                EndSwitch
        WEnd
EndFunc   ;==>Example
发表于 2024-4-13 11:10:14 | 显示全部楼层

这代码也好玩,
发表于 2024-4-14 10:23:08 | 显示全部楼层

AFUN还是介于牛A与牛C之间...


-------------------------
 楼主| 发表于 2024-4-15 10:30:44 | 显示全部楼层

A大YYDS,就是我有些迷惑,$iMsg可以获取按钮的点击事件,为啥GUICtrlSetData($iMsg + 1, 100)确能关联到进度条控件的
发表于 2024-4-15 11:00:48 | 显示全部楼层
梦倾天下 发表于 2024-4-15 10:30
A大YYDS,就是我有些迷惑,$iMsg可以获取按钮的点击事件,为啥GUICtrlSetData($iMsg + 1, 100)确能关联到 ...

$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐标的进度条控件,因此 进度条控件ID 就是 按钮ID + 1,即 $iMsg + 1
 楼主| 发表于 2024-4-15 11:14:07 | 显示全部楼层
afan 发表于 2024-4-15 11:00
$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐 ...

明白了,非常感谢
发表于 2024-4-15 11:20:55 | 显示全部楼层
afan 发表于 2024-4-15 11:00
$iMsg 是当前点击的控件ID。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐 ...

骚,够骚...

-------------------------------
发表于 2024-4-15 16:31:19 | 显示全部楼层
邪恶海盗 发表于 2024-4-15 11:20
骚,够骚...

-------------------------------

这只是基本骚操作
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-30 02:53 , Processed in 0.082810 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表