happytc 发表于 2011-5-10 23:34:55

guicreate()绘窗口后,用事件模式,如何不用全局变量?

象下面的,若不用全局变量,怎么把象变量$OKButton传给_Event函数呢?

因为用了全局变量不利于封装和转移。



Opt("GUIOnEventMode", 1)
$hwd = GUICreate("Config", 280, 150)
$OKButton = GUICtrlCreateButton("OK", 50, 120, 50, 20)
GUICtrlSetOnEvent(-1, "_Event")
GUISetState()

Func _Event()
        Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $OKButton
                        MsgBox(0, "pressed", "Pressed OK Button")
                        Exit
        EndSwitch
EndFunc

蜘蛛抱蛋 发表于 2011-5-10 23:45:41

用Enum枚举常量试试

happytc 发表于 2011-5-10 23:59:12

用Enum枚举常量试试
蜘蛛抱蛋 发表于 2011-5-10 23:45 http://www.autoitx.com/images/common/back.gif

怎么个枚举法?
没有明白你的意思,你写个代码说明下

pusofalse 发表于 2011-5-11 01:42:29

楼主可以自己调用RegisterClassEx、CreateWindowEx,自己设定控件的ID,在窗口过程中直接使用数字常量来判断就可以了。你可能会说这样做会很麻烦,不过可以写成一个通用的UDF,现在麻烦一点,是为了以后不麻烦。
另外,楼主曾经提过一个与此类似的问题,当时我的回答是使用 局部静态变量的方法来避免使用全局变量(虽然两者的存储位置都一样),貌似当时你也解决了。

ceoguang 发表于 2011-5-11 01:57:51

Opt("GUIOnEventMode", 1)
$hwd = GUICreate("Config", 280, 150)
$OKButton = GUICtrlCreateButton("OK", 50, 120, 50, 20)
GUICtrlSetOnEvent(-1, "_OKButton")
GUISetOnEvent(-3, "_Close")
GUISetState()
While 1
        Sleep(100)
WEnd

Func _Close()
        MsgBox(0, "Close", @GUI_WinHandle)
        GUIDelete()
    Exit
EndFunc

Func _OKButton()
        MsgBox(0, @GUI_CtrlId & " pressed", "Pressed OK Button")
EndFunc

happytc 发表于 2011-5-11 06:05:47

楼主可以自己调用RegisterClassEx、CreateWindowEx,自己设定控件的ID,在窗口过程中直接使用数字常量来判断 ...
pusofalse 发表于 2011-5-11 01:42 http://www.autoitx.com/images/common/back.gif

调用RegisterClassEx等来做,的确麻烦呀,并且正是由于上面说的原因,一直不喜欢用事件模式,也就一直用消息模式,很好用,写出来的代码相对简洁点,要不是最近遇到下面诡异问题,也不会被逼用事件模式了(http://www.autoitx.com/thread-24289-1-1.html)若真今后还遇到这种问题,我就下决心写个通用的udf了。

“楼主曾经提过一个与此类似的问题,当时我的回答是使用 局部静态变量的方法来避免使用全局变量(虽然两者的存储位置都一样),貌似当时你也解决了。”
------------ 那个问题跟这个不大一样的,这个问题是要传递参数,用局部静态变量肯定不行嘛,又不是要保留曾用过的变量值为再调用这个函数时用这个值。

happytc 发表于 2011-5-11 06:09:09

回复 5# ceoguang

这样做,也算是一种方法,但明显会麻烦得多了,当窗口的控件很多时,得定义多少个函数呀?并且很多时候需要对几个控件的操作形为联合起来判断,这样这种方法就更不适用了

3mile 发表于 2011-5-11 09:48:07

回复ceoguang

这样做,也算是一种方法,但明显会麻烦得多了,当窗口的控件很多时,得定义多少个函数呀 ...
happytc 发表于 2011-5-11 06:09 http://www.autoitx.com/images/common/back.gif
你的意思是这个?
Opt("GUIOnEventMode", 1)
local $OKButton
$hwd = GUICreate("Config", 280, 200)
GUISetOnEvent(-3,"_Event")

For $i=0 to 9
        $OKButton[$i] = GUICtrlCreateButton("OK"&$i+1, 50, $i*20, 50, 20)
        GUICtrlSetOnEvent(-1, "_Event")
        Assign($OKButton[$i],$i+1)
Next
GUISetState()
While 1
       Sleep(100)
WEnd

Func _Event()
      Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $OKButton to $OKButton
                        MsgBox(0, "pressed", "Pressed OK Button"&Eval(@GUI_CtrlId))
                        ;Exit
      EndSwitch
EndFunc

happytc 发表于 2011-5-11 10:38:26

你的意思是这个?
3mile 发表于 2011-5-11 09:48 http://www.autoitx.com/images/common/back.gif

这跟我在一楼举的例子一样。

意思比如你举的例子中,我加了一个Input,现在想按SaveButton时,读取Input里的东西
当一个控件事件发生时读取其它一些控件的值/状态。有没有什么方便的方法实现?
当然对每个@GUI_CtrlId写临时文件来实现是可以的,这样不但要针对每个@GUI_CtrlId都得Case一样,还是读/写临时文件


Opt("GUIOnEventMode", 1)
Local $OKButton
$hwd = GUICreate("Config", 280, 230)
GUISetOnEvent(-3, "_Event")

For $i = 0 To 9
        $OKButton[$i] = GUICtrlCreateButton("OK" & $i + 1, 50, $i * 20, 50, 20)
        GUICtrlSetOnEvent(-1, "_Event")
        Assign($OKButton[$i], $i + 1)
Next

$iInput = GUICtrlCreateInput("", 10, 200, 50, 20)
$iSaveButton = GUICtrlCreateButton("Save", 200, 200, 50, 20)
GUICtrlSetOnEvent(-1, "_Event")
GUISetState()

While 1
        Sleep(100)
WEnd

Func _Event()
        Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $iSaveButton
                        MsgBox(0,0,GUICtrlRead($iInput))
                Case $OKButton To $OKButton
                        MsgBox(0, "pressed", "Pressed OK Button" & Eval(@GUI_CtrlId))
                        ;Exit
        EndSwitch
EndFunc   ;==>_Event

3mile 发表于 2011-5-11 10:53:28

回复 9# happytc
写临时文件?
哪有写临时文件的语句啊?

happytc 发表于 2011-5-11 11:06:02

本帖最后由 happytc 于 2011-5-11 11:08 编辑

回复happytc
写临时文件?
哪有写临时文件的语句啊?
3mile 发表于 2011-5-11 10:53 http://www.autoitx.com/images/common/back.gif
意思象若要在Case $iSaveButton时得到一个$iInput的值,可以从临时文件里读取
Case $iInput
       FileWrite(@TempDir & "\tmp.txt", GUICtrlRead($iInput))


全代码如下, 实现没有想到更好的方法。若您有,请指教

Opt("GUIOnEventMode", 1)
Local $OKButton
$hwd = GUICreate("Config", 280, 230)
GUISetOnEvent(-3, "_Event")

For $i = 0 To 9
        $OKButton[$i] = GUICtrlCreateButton("OK" & $i + 1, 50, $i * 20, 50, 20)
        GUICtrlSetOnEvent(-1, "_Event")
        Assign($OKButton[$i], $i + 1)
Next

$iInput = GUICtrlCreateInput("", 10, 200, 50, 20)
GUICtrlSetOnEvent(-1, "_Event")

$iSaveButton = GUICtrlCreateButton("Save", 200, 200, 50, 20)
GUICtrlSetOnEvent(-1, "_Event")

GUISetState()

While 1
        Sleep(100)
WEnd

Func _Event()
        Switch @GUI_CtrlId
                Case -3
                        Exit
                Case $iInput
                        FileWrite(@TempDir & "\tmp.txt", GUICtrlRead($iInput))
                Case $iSaveButton
                        MsgBox(0,0,FileRead(@TempDir & "\tmp.txt"))
                Case $OKButton To $OKButton
                        MsgBox(0, "pressed", "Pressed OK Button" & Eval(@GUI_CtrlId))
                        ;Exit
        EndSwitch
EndFunc   ;==>_Event

haijie1223 发表于 2011-5-11 14:16:43

好问题   留意一下
页: [1]
查看完整版本: guicreate()绘窗口后,用事件模式,如何不用全局变量?