Jump to content

Child GUI problem


Recommended Posts

I'm having trouble to detect when the child GUI is closed.

When closing the child the parent gui also closes, I need a way to detect mouse and kybd actions in the child gui

without affecting the parent gui.

Does someone have any ideas to fix this, thanks?

Here's my script:

#include <GuiConstants.au3>

#include <ButtonConstants.au3>

#include <GUIConstantsEx.au3>

#NoTrayIcon

$script_title = "2win"

$gmain=GuiCreate($script_title, 606, 500)

$btn_run = GuiCtrlCreateButton("Action", 244, 444, 97, 25,$BS_DEFPUSHBUTTON)

GUICtrlCreateMenu("Information",300,200)

$filemenu = GUICtrlCreateMenu ("&File")

$statmenu = GUICtrlCreateMenu ("&Statistics")

$printer = GUICtrlCreateMenuitem ("&Printer",$filemenu)

$close = GUICtrlCreateMenuitem ("&Exit Alt+F4",$filemenu)

$stat= GUICtrlCreateMenuitem ("&Complete",$statmenu)

$stat2= GUICtrlCreateMenuitem ("&Sum",$statmenu)

$gchild=GuiCreate($script_title&" child", 306, 300,-1,-1,-1,-1,$gmain)

GuiSetState(@SW_SHOW,$gmain)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $btn_run

MsgBox(16,"Action","action")

Case $msg = $printer

MsgBox(32, "Printer", "printer")

Case $msg = $stat

MsgBox(32, "Stat", "stat")

Case $msg = $stat2

$tst=GUISetState(@SW_SHOW,$gchild)

Case $msg = $close

ExitLoop

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case Else

EndSelect

WEnd

Exit

Link to comment
Share on other sites

  • Moderators

lgnihlman,

You need to use the "advanced" parameter of GUIGetMsg - look it up the details in the Help file. Then you can use code like this - look for my comments:

#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#NoTrayIcon

$script_title = "2win"
$gmain = GUICreate($script_title, 606, 500)
$btn_run = GUICtrlCreateButton("Action", 244, 444, 97, 25, $BS_DEFPUSHBUTTON)

GUICtrlCreateMenu("Information", 300, 200)
$filemenu = GUICtrlCreateMenu("&File")
$statmenu = GUICtrlCreateMenu("&Statistics")
$printer = GUICtrlCreateMenuItem("&Printer", $filemenu)
$close = GUICtrlCreateMenuItem("&Exit Alt+F4", $filemenu)
$stat = GUICtrlCreateMenuItem("&Complete", $statmenu)
$stat2 = GUICtrlCreateMenuItem("&Sum", $statmenu)

GUISetState(@SW_SHOW)

$gchild = GUICreate($script_title & " child", 306, 300, -1, -1, -1, -1, $gmain)
; Hide the child once created
GUISetState(@SW_HIDE)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $btn_run
            MsgBox(16, "Action", "action")
        Case $msg = $printer
            MsgBox(32, "Printer", "printer")
        Case $msg = $stat
            MsgBox(32, "Stat", "stat")
        Case $msg = $stat2
            ; Show the child
            GUISetState(@SW_SHOW, $gchild)
            While 1
                ; Get the advanced return from GUIGetMsg
                $amsg = GUIGetMsg(1)
                ; If the message comes from the child
                If $amsg[1] = $gchild Then
                    ; And it is to close
                    If $amsg[0] = $GUI_EVENT_CLOSE Then
                        ; Then hide the child and return to the main loop
                        GUISetState(@SW_HIDE, $gchild)
                        ExitLoop
                    EndIf
                EndIf
            WEnd

        Case $msg = $close
            ExitLoop
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect

WEnd
Exit

I hope that is clear. Ask again if you still have any questions AFTER you have read the help file! ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

lgnihlman,

You need to use the "advanced" parameter of GUIGetMsg - look it up the details in the Help file. Then you can use code like this - look for my comments:

#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#NoTrayIcon

$script_title = "2win"
$gmain = GUICreate($script_title, 606, 500)
$btn_run = GUICtrlCreateButton("Action", 244, 444, 97, 25, $BS_DEFPUSHBUTTON)

GUICtrlCreateMenu("Information", 300, 200)
$filemenu = GUICtrlCreateMenu("&File")
$statmenu = GUICtrlCreateMenu("&Statistics")
$printer = GUICtrlCreateMenuItem("&Printer", $filemenu)
$close = GUICtrlCreateMenuItem("&Exit Alt+F4", $filemenu)
$stat = GUICtrlCreateMenuItem("&Complete", $statmenu)
$stat2 = GUICtrlCreateMenuItem("&Sum", $statmenu)

GUISetState(@SW_SHOW)

$gchild = GUICreate($script_title & " child", 306, 300, -1, -1, -1, -1, $gmain)
; Hide the child once created
GUISetState(@SW_HIDE)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $btn_run
            MsgBox(16, "Action", "action")
        Case $msg = $printer
            MsgBox(32, "Printer", "printer")
        Case $msg = $stat
            MsgBox(32, "Stat", "stat")
        Case $msg = $stat2
            ; Show the child
            GUISetState(@SW_SHOW, $gchild)
            While 1
                ; Get the advanced return from GUIGetMsg
                $amsg = GUIGetMsg(1)
                ; If the message comes from the child
                If $amsg[1] = $gchild Then
                    ; And it is to close
                    If $amsg[0] = $GUI_EVENT_CLOSE Then
                        ; Then hide the child and return to the main loop
                        GUISetState(@SW_HIDE, $gchild)
                        ExitLoop
                    EndIf
                EndIf
            WEnd

        Case $msg = $close
            ExitLoop
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect

WEnd
Exit

I hope that is clear. Ask again if you still have any questions AFTER you have read the help file! ;)

M23

Many thanks M23!

Now I got it.

Link to comment
Share on other sites

I have a new problem with the child GUI.

When clicking the Exit button the child gui will close but if I then open the child gui again without closing

the parent gui first the Exit button will not work.

What's my mistake here?

Any help and ideas are appreciated,thanks

#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#NoTrayIcon

$script_title = "2win"
$gmain=GuiCreate($script_title, 606, 500)
$btn_run = GuiCtrlCreateButton("Action", 244, 444, 97, 25,$BS_DEFPUSHBUTTON)

GUICtrlCreateMenu("Information",300,200)
$filemenu = GUICtrlCreateMenu ("&File")
$statmenu = GUICtrlCreateMenu ("&Statistics")
$printer = GUICtrlCreateMenuitem ("&Printer",$filemenu)
$close = GUICtrlCreateMenuitem ("&Exit   Alt+F4",$filemenu)
$stat= GUICtrlCreateMenuitem ("&Complete",$statmenu)
$stat2= GUICtrlCreateMenuitem ("&Sum",$statmenu)

GUISetState(@SW_SHOW)

$gchild=GuiCreate($script_title&" child", 306, 300,-1,-1,-1,-1,$gmain)
GUISetState(@SW_HIDE)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $btn_run
            MsgBox(16,"Action","action")
        Case $msg = $printer
            MsgBox(32, "Printer", "printer")
        Case $msg = $stat
            MsgBox(32, "Stat", "stat")
        Case $msg = $stat2
            ; Show the child
            GUISetState(@SW_SHOW, $gchild)
            $btn_xit = GuiCtrlCreateButton("Exit", 114, 254, 97, 25,$BS_DEFPUSHBUTTON)
            While 1
                ; Get the advanced return from GUIGetMsg
                $amsg = GUIGetMsg(1)
                ; If the message comes from the child
                If $amsg[1] = $gchild Then
                    ; And it is to close
                    If $amsg[0] = $GUI_EVENT_CLOSE Then
                        ; Then hide the child and return to the main loop
                        GUISetState(@SW_HIDE, $gchild)
                        ExitLoop
                    ElseIf $amsg[0]= $btn_xit Then
                        ; Then hide the child and return to the main loop
                        GUISetState(@SW_HIDE, $gchild)
                        ExitLoop
                    EndIf
                EndIf
            WEnd

        Case $msg = $close
            ExitLoop

        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
    EndSelect

WEnd
Exit
Link to comment
Share on other sites

  • Moderators

lgnihlman,

At the moment you are creating a new button in the same position each time you open the child GUI. Becasue of the way AutoIt layers the controls, the new buttons are hidden under the old ones. So you are clicking the first (and oldest) button while you have reset $btn_xit to the ControlID of the new button, which will be higher. Then when your GUIGetMsg loop fires as you click the oldest (first created) button, the ControlID returned will not match the new value of $btn_exit and so nothing happens.

Look at the returns in the SciTE console from this cut-down version of your script:

#include <GuiConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#NoTrayIcon

$script_title = "2win"
$gmain=GuiCreate($script_title, 606, 500)

GUICtrlCreateMenu("Information",300,200)
$statmenu = GUICtrlCreateMenu ("&Statistics")
$stat2= GUICtrlCreateMenuitem ("&Sum",$statmenu)

GUISetState(@SW_SHOW)

$gchild=GuiCreate($script_title&" child", 306, 300,-1,-1,-1,-1,$gmain)

GUISetState(@SW_HIDE)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $stat2
            ; Show the child
            GUISetState(@SW_SHOW, $gchild)
            $btn_xit = GuiCtrlCreateButton("Exit", 114, 254, 97, 25,$BS_DEFPUSHBUTTON)
            ConsoleWrite("This is the ControlID set in $btn_exit: " & $btn_xit & @CRLF)
            While 1
                ; Get the advanced return from GUIGetMsg
                $amsg = GUIGetMsg(1)

                If $amsg[0] > 0 Then ConsoleWrite("And this is the ControlID returned from GUIGetMsg: " & $amsg[0] &  @CRLF)

                ; If the message comes from the child
                If $amsg[1] = $gchild Then
                    ; And it is to close
                    If $amsg[0] = $GUI_EVENT_CLOSE Or $amsg[0]= $btn_xit Then  :<<<<<< neater syntax here <<<<<<<<<<<<<
                        ; Then hide the child and return to the main loop
                        GUISetState(@SW_HIDE, $gchild)
                        ExitLoop
                    EndIf
                EndIf
            WEnd
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case Else
    EndSelect

WEnd
Exit

All you need to do to solve this is to create the button once when you first create the child GUI:

$gchild=GuiCreate($script_title&" child", 306, 300,-1,-1,-1,-1,$gmain)
$btn_xit = GuiCtrlCreateButton("Exit", 114, 254, 97, 25,$BS_DEFPUSHBUTTON) ; <<<<< Add this line <<<<<<<
GUISetState(@SW_HIDE)

; And not each time you show the child GUI
; Show the child
GUISetState(@SW_SHOW, $gchild)
$btn_xit = GuiCtrlCreateButton("Exit", 114, 254, 97, 25,$BS_DEFPUSHBUTTON) ; <<<< Remove this line <<<<<<<<<

Then it will work as you expect because the returned ControlID will match $btn_exit. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...