找回密码
 加入
搜索
查看: 7547|回复: 7

[GUI管理] 主窗口点击按钮后又创建了一个新的子窗口,在关闭子窗口时主窗口也关闭了

  [复制链接]
发表于 2012-7-31 12:51:39 | 显示全部楼层 |阅读模式
大侠们帮帮忙看看我这个问题:
主窗口上点击按钮,然后调用另外的窗口脚本,创建了一个子窗口出来,但在关闭子窗口时会将程序一起退出去,大家有没有办法可以只关闭子窗口,而不影响到主窗口,下次再主窗口上点击按钮后,又可以拉起这个子窗口来。
发表于 2012-7-31 17:18:04 | 显示全部楼层
#include <GUIConstantsEx.au3>


_Main()

Func _Main()

        ;Initialize variables
        Local $GUIWidth = 250, $GUIHeight = 250
        Local $ParentWin, $ParentWin_Pos, $ChildWin, $msg

        ;Create main/parent window
$ParentWin = GUICreate("父窗体", $GUIWidth, $GUIHeight)
        ;Save the position of the parent window
        $ParentWin_Pos = WinGetPos($ParentWin, "")
        ;Show the parent window/Make the parent window visible
        GUISetState(@SW_SHOW)

        ;Create child window and add the parameter to make it the child of the parent window
$ChildWin = GUICreate("子窗体", $GUIWidth, $GUIHeight, $ParentWin_Pos[0] + 100, $ParentWin_Pos[1] + 100, -1, -1, $ParentWin)
        ;Show the child window/Make the child window visible
        GUISetState(@SW_SHOW)

        ;Switch to the parent window
        GUISwitch($ParentWin)

        ;Loop until:
        ;- user presses Esc when focused to the parent window
        ;- user presses Alt+F4 when focused to the parent window
        ;- user clicks the close button of the parent window
        While 1
                ;After every loop check if the user clicked something in the GUI windows
                $msg = GUIGetMsg(1)
                Select
                        ;Check if user clicked on a close button of any of the 2 windows
                        Case $msg[0] = $GUI_EVENT_CLOSE
                                ;Check if user clicked on the close button of the child window
                                If $msg[1] = $ChildWin Then
            MsgBox(64, "测试", "您关闭了子窗体.")
                                        ;Switch to the child window
                                        GUISwitch($ChildWin)
                                        ;Destroy the child GUI including the controls
                                        GUIDelete()
                                        ;Check if user clicked on the close button of the parent window
                                ElseIf $msg[1] = $ParentWin Then
            MsgBox(64, "测试", "您关闭了父窗体.")
                                        ;Switch to the parent window
                                        GUISwitch($ParentWin)
                                        ;Destroy the parent GUI including the controls
                                        GUIDelete()
                                        ;Exit the script
                                        Exit
                                EndIf

                EndSelect

        WEnd
EndFunc   ;==>_Main


D:\autoit3\Examples\GUI\Simple\child.au3
 楼主| 发表于 2012-7-31 19:18:42 | 显示全部楼层
给的例子固然可以只是关闭子窗口,但是再用父窗口开创建子窗体依然创建不起来。
发表于 2012-7-31 19:36:47 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-7-31 19:38 编辑

回复 3# andyloving


    在 2# 的基础上改了下:
#include <GUIConstantsEx.au3>

Local $ParentWin, $ChildWin, $msg

$ParentWin = GUICreate("父窗体", 250, 250)
$Button = GUICtrlCreateButton("Popup", 75, 150, 100, 35)
GUISetState()

While 1
        ;After every loop check if the user clicked something in the GUI windows
        $msg = GUIGetMsg()
        Switch $msg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button
                        GUISetState(@SW_DISABLE)
                        $ChildWin = GUICreate("子窗体", 200, 200, -1, -1, -1, -1, $ParentWin)
                        GUISetState()
                        Do
                        Until GUIGetMsg() = $GUI_EVENT_CLOSE 
                        GUIDelete($ChildWin)
                        GUISetState(@SW_ENABLE)
                        GUISetState(@SW_RESTORE)
        EndSwitch
WEnd
 楼主| 发表于 2012-7-31 21:29:37 | 显示全部楼层
找到方法了,谢谢两位。我的方法如下:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form = GUICreate("Form1", 429, 155, 192, 124)
$Button = GUICtrlCreateButton("Button1", 216, 48, 137, 41)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Button
                         XIXI()

        EndSwitch
WEnd

;子窗口
Func XIXI()
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 460, 155, 400, 124)
$Button1 = GUICtrlCreateButton("Button1", 216, 48, 137, 41)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$show= True

While $show
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        GUIDelete($Form1)
                        $show=False
                Case $Button1
                        MsgBox(0,0,0)

        EndSwitch
WEnd
EndFunc
发表于 2012-7-31 21:37:20 | 显示全部楼层
不知这个适合你胃口不?


#include <GUIConstantsEx.au3>

$mainwindow = GUICreate("Hello World", 300, 200)
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("opened the subform", 20, 30, 150)

$dummywindow = GUICreate("Dummy window for testing ", 200, 100)

GUISwitch($mainwindow)
GUISetState(@SW_SHOW)

While 1
  $msg = GUIGetMsg(1)

  Select
    Case $msg[0] = $okbutton
      GUISetState(@SW_SHOW,$dummywindow)

    Case $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $mainwindow 

      Exit
  Case $msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $dummywindow
          GUISetState(@SW_HIDE,$dummywindow)
  EndSelect
WEnd 
 楼主| 发表于 2012-7-31 21:50:28 | 显示全部楼层
回复 6# 鸟人

这个也是个不错的方法,非常感谢你!
发表于 2012-8-6 16:10:39 | 显示全部楼层
好技术  强帖
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-18 17:05 , Processed in 0.077631 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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