梦倾天下 发表于 2024-4-11 15:20:01

通过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


callmekq 发表于 2024-4-11 15:26:19

$totalButtons ->> Global $TotalButtons

梦倾天下 发表于 2024-4-11 16:28:48

callmekq 发表于 2024-4-11 15:26
$totalButtons ->> Global $TotalButtons

能细说嘛?若是有很多个按钮怎么搞哇

邪恶海盗 发表于 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
这个两个东东,是个好东东

redapple2008 发表于 2024-4-12 08:39:10

绿色风 发表于 2024-4-11 22:32


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

高手啊

afan 发表于 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 To $aButton[$totalButtons - 1]
                                GUICtrlSetData($iMsg + 1, 100)
                EndSwitch
        WEnd
EndFunc   ;==>Example

顽固不化 发表于 2024-4-13 11:10:14

afan 发表于 2024-4-12 18:18


这代码也好玩,{:3_86:}

邪恶海盗 发表于 2024-4-14 10:23:08

afan 发表于 2024-4-12 18:18


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


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

梦倾天下 发表于 2024-4-15 10:30:44

afan 发表于 2024-4-12 18:18


A大YYDS,就是我有些迷惑,$iMsg可以获取按钮的点击事件,为啥GUICtrlSetData($iMsg + 1, 100)确能关联到进度条控件的

afan 发表于 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。因为控件是批量顺序创建的,按创建顺序,按钮控件后面一个总是与其同一批次坐 ...

骚,够骚...

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

afan 发表于 2024-4-15 16:31:19

邪恶海盗 发表于 2024-4-15 11:20
骚,够骚...

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

这只是基本骚操作:face (24):
页: [1]
查看完整版本: 通过for循环创建的按钮怎么设置对应的点击事件【已解决】