找回密码
 加入
搜索
查看: 3109|回复: 5

[GUI管理] [已解决]很奇怪的控件事件消失的问题请教

  [复制链接]
发表于 2011-6-7 16:36:48 | 显示全部楼层 |阅读模式
本帖最后由 itljl 于 2011-6-8 18:39 编辑
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)


If WinExists("TestEvent") Then
        WinSetState("TestEvent", "", @SW_SHOW)
        WinActivate("TestEvent")
        Exit
EndIf

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("TestEvent", 399, 191, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "Form1Minimize")
GUISetOnEvent($GUI_EVENT_MAXIMIZE, "Form1Maximize")
GUISetOnEvent($GUI_EVENT_RESTORE, "Form1Restore")
$Button1 = GUICtrlCreateButton("测试", 136, 80, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click")

$Button2 = GUICtrlCreateButton("隐藏窗口", 136, 80+60, 75, 25)
GUICtrlSetOnEvent(-1, "Button2")

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        Sleep(100)
WEnd

Func Button1Click()
        MsgBox(0, 0, "有响应")
EndFunc   ;==>Button1Click

Func Button2()
        GUISetState(@SW_hide,$Form1)
EndFunc   ;==>Button1Click

Func Form1Close()
        Exit
EndFunc   ;==>Form1Close
以上代码中,编译后。
先点“隐藏”将窗口隐藏起来。再双击exe程序,EXE执行时,检测到已经存在该窗口,于是不开新窗口中,而显示并激活原来的窗口。
但原来窗口控制的事件就不能用,点击两个按钮都没用了,

为什么会消失呢?请教。
发表于 2011-6-7 23:10:58 | 显示全部楼层
本人水平有限,曲线救国,应该可以满足LZ的要求
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{F4}", "_Show")

If WinExists("TestEvent") Then
Send('{F4}')
Exit
EndIf

$Form1 = GUICreate("TestEvent", 399, 191, 192, 114)
$Button1 = GUICtrlCreateButton("测试", 136, 80, 75, 25)

$Button2 = GUICtrlCreateButton("隐藏窗口", 136, 80 + 60, 75, 25)

GUISetState(@SW_SHOW)


While 1
        $nmsg = GUIGetMsg()
        Select
                Case $nmsg = $GUI_EVENT_CLOSE
                        Exit
                Case $nmsg = $Button1
                        Button1Click()
                Case $nmsg = $Button2
                        Button2()
        EndSelect
WEnd




Func Button1Click()
        MsgBox(0, 0, "有响应")
EndFunc   ;==>Button1Click

Func Button2()
        GUISetState(@SW_HIDE, $Form1)
EndFunc   ;==>Button2


Func _Show()
        GUISetState(@SW_SHOW)
EndFunc   ;==>_Show
发表于 2011-6-8 08:11:20 | 显示全部楼层
回复 1# itljl

你应该让程序自己显示,而不是让外部程序来显示窗体。
发表于 2011-6-8 09:44:20 | 显示全部楼层
本帖最后由 happytc 于 2011-6-8 09:58 编辑

回复 1# itljl


    要深入了解GUISetState(@SW_hide,$Form1)的用法,它把窗口隐藏后,同时也会窗口上的控件都失活了。而你用WinSetState("TestEvent", "", @SW_SHOW)仅仅是激了窗体而已,并不能激活控件。所以你只需要把Func Button2()改一下就可以了:

Func Button2()
        WinSetState("TestEvent", "", @SW_HIDE)
EndFunc

跟你编译不编译没有任何关系,如下面代码,你试试

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)


$Form1 = GUICreate("TestEvent", 399, 191, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")

$Button1 = GUICtrlCreateButton("测试", 136, 80, 75, 25)
GUICtrlSetOnEvent(-1, "Button1Click")

$Button2 = GUICtrlCreateButton("隐藏窗口", 136, 80+60, 75, 25)
GUICtrlSetOnEvent(-1, "Button2")

GUISetState(@SW_SHOW)


While 1
        Sleep(100)
WEnd

Func Button1Click()
        MsgBox(0, 0, "有响应")
EndFunc   ;==>Button1Click

Func Button2()
        GUISetState(@SW_hide,$Form1)
                Sleep(1000)
                WinSetState("TestEvent", "", @SW_SHOW)
EndFunc   ;==>Button1Click

Func Form1Close()
        Exit
EndFunc   ;==>Form1Close
 楼主| 发表于 2011-6-8 18:39:57 | 显示全部楼层
回复 2# sunafter


    谢谢。
 楼主| 发表于 2011-6-8 18:40:06 | 显示全部楼层
回复 4# happytc


    谢谢,这个方法果然可行。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-2 23:04 , Processed in 0.073387 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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