函数参考


GUICtrlSetOnEvent

为指定控件的点击行为定义一个用户函数.

GUICtrlSetOnEvent ( 控件ID, "函数名" )

参数

控件ID 控件标识符(控件ID),可由 GUICtrlCreate... 函数的返回值获得.
函数名 要调用的用户函数名.

返回值

成功: 返回值为1.
失败: 返回值为0.

注意/说明

函数仅当选项 GUIOnEventMode 的值为1时才能被调用,而在此模式下 GUIGetMsg 将完全无效.

你不能调用带有参数的函数.

在被调用的用户函数内部可由宏 @GUI_CTRLID 获得控件的标识符.
如有需要则窗口句柄和控件句柄可分别由 @GUI_WINHANDLE 和 @GUI_CTRLHANDLE 获得.

如果函数是一个空字符串 "",那么上一个用户定义的事件将会关闭.

相关

GUICtrlCreate..., GUIGetMsg, GUIOnEventMode (Option), GUISetOnEvent, GUICtrlCreateDummy, GUICtrlSendToDummy

示例/演示


#include <GUIConstantsEx.au3>

Example()

Func Example()
    Opt("GUICoordMode", 2)
    Opt("GUIResizeMode", 1)
    Opt("GUIOnEventMode", 1)

    GUICreate("Parent1")
    GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
    GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
    GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

    GUICtrlCreateButton("OK", 10, 30, 50)
    GUICtrlSetOnEvent(-1, "OKPressed")

    GUICtrlCreateButton("Cancel", 0, -1)
    GUICtrlSetOnEvent(-1, "CancelPressed")

    GUISetState(@SW_SHOW)

    ; Just idle around
    While 1
        Sleep(10)
    WEnd

EndFunc   ;==>Example


Func OKPressed()
    MsgBox(0, "OK Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
EndFunc   ;==>OKPressed


Func CancelPressed()
    MsgBox(0, "Cancel Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle & " CtrlHandle=" & @GUI_CtrlHandle)
EndFunc   ;==>CancelPressed


Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            Exit

        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
            MsgBox(0, "Window Minimized", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

        Case @GUI_CtrlId = $GUI_EVENT_RESTORE
            MsgBox(0, "Window Restored", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)

    EndSelect
EndFunc   ;==>SpecialEvents