Sign in to follow this  
Followers 0
Naveed

How to Close 2nd GUI

8 posts in this topic

This might be straight forward but i am struggling to work it out. I have included an example of the problem i am having with one of my programs i have built.

I am able to launch another gui window without a problem using the guicreate method however i am unable to close this 2nd gui.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui

$gui = GUICreate("test",400,100,-1,-1)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close,"Exitgui")

$but = GUICtrlCreateButton("Test",10,10,200,50)
GUIctrlSetOnEvent($but,"newgui")

Func newgui()
    $gui2 = GUICreate("Test 2",400,100,-1,-1)
    GUISetState(@SW_SHOW)
    Do
        $msg = GUIGetMsg()

    Until $msg = $GUI_EVENT_CLOSE

    while 1

    WEnd
EndFunc

Func exitgui()
    Exit
EndFunc

While 1
Sleep(10000)
WEnd

Any help will be much appreciated, thanks

Nav

Share this post


Link to post
Share on other sites



You may try to use GUIDelete() or GUISetState(@SW_HIDE, $2nd_GUI) and similar ways.

Share this post


Link to post
Share on other sites

You may try to use GUIDelete() or GUISetState(@SW_HIDE, $2nd_GUI) and similar ways.

Thanks for that however GuiDelete()doesnt work and guisetstate is no good as i want to close the GUI rather then hide it.

Share this post


Link to post
Share on other sites

You're mixing GuiOnEventMode with GUIGetMsg. If you want to use GUIGetMsg, disable the OnEvent mode at the beginning of the function an reenable it at the end.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui, $gui2

$gui = GUICreate("test", 400, 100)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close, "Exitgui")
$but = GUICtrlCreateButton("Test", 10, 10, 200, 50)
GUICtrlSetOnEvent($but, "newgui")

While 1
    Sleep(10)
WEnd

Func newgui()
    if IsHWnd($gui2) then GUIDelete($gui2)
    $gui2 = GUICreate("Test 2", 200, 50)
    GUISetOnEvent($gui_event_close, "Exitgui")
    GUISetState(@SW_SHOW)
EndFunc   ;==>newgui

Func exitgui()
    Switch @GUI_WinHandle
        Case $gui
            Exit
        Case $gui2
            GUIDelete($gui2)
    EndSwitch
EndFunc   ;==>exitgui

Share this post


Link to post
Share on other sites

You're mixing GuiOnEventMode with GUIGetMsg. If you want to use GUIGetMsg, disable the OnEvent mode at the beginning of the function an reenable it at the end.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui, $gui2

$gui = GUICreate("test", 400, 100)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close, "Exitgui")
$but = GUICtrlCreateButton("Test", 10, 10, 200, 50)
GUICtrlSetOnEvent($but, "newgui")

While 1
    Sleep(10)
WEnd

Func newgui()
    if IsHWnd($gui2) then GUIDelete($gui2)
    $gui2 = GUICreate("Test 2", 200, 50)
    GUISetOnEvent($gui_event_close, "Exitgui")
    GUISetState(@SW_SHOW)
EndFunc   ;==>newgui

Func exitgui()
    Switch @GUI_WinHandle
        Case $gui
            Exit
        Case $gui2
            GUIDelete($gui2)
    EndSwitch
EndFunc   ;==>exitgui

Thanks for that KaFu that helps alot, does the positioning of onevent mode within the code matter and if so could you please explain why.

Thanks once again.

Nav

Share this post


Link to post
Share on other sites

does the positioning of onevent mode within the code matter and if so could you please explain why.

Yep, it does. Autoit is single threaded, you can always follow the flow with your finger tip. If Onevent mode is enabled, guigetmsg will not work an vice versa.

If you want to mix both methods you'll have to do it like this, note that if you're opening the 2nd gui the onevent trigger on the first gui is disabled.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui, $gui2

$gui = GUICreate("test", 400, 100)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close, "Exitgui")
$but = GUICtrlCreateButton("Test", 10, 10, 200, 50)
GUICtrlSetOnEvent($but, "newgui")

While 1
    Sleep(10)
WEnd

Func newgui()
    If IsHWnd($gui2) Then GUIDelete($gui2)
    
    Opt('GuiOnEventMode', 0)

    $gui2 = GUICreate("Test 2", 200, 50)
    GUISetState(@SW_SHOW)
    Do
        $msg = GUIGetMsg()
    Until $msg = $gui_event_close
    GUIDelete($gui2)

    Opt('GuiOnEventMode', 1)

EndFunc   ;==>newgui



Func exitgui()
    Switch @GUI_WinHandle
        Case $gui
            Exit
    EndSwitch
EndFunc   ;==>exitgui

Share this post


Link to post
Share on other sites

Naveed,

I would go further than KaFu and say that, as a general rule, you should only ever use just the one mode your script. Switching modes in the same script is fraught with danger and should only be used in the most exceptional circumstances - and even then you should have alarm bells ringing in your head asking why the circumstances are so exceptional and whether it would not be better to recast your code. ;)

Here is the same code without a mode change in the function: :blink:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui, $gui2

$gui = GUICreate("test", 400, 100)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close, "Exitgui")
$but = GUICtrlCreateButton("Test", 10, 10, 200, 50)
GUICtrlSetOnEvent($but, "newgui")

While 1
    Sleep(10)
WEnd

Func newgui()
    If IsHWnd($gui2) Then GUIDelete($gui2)

    $gui2 = GUICreate("Test 2", 200, 50)
    GUISetState(@SW_SHOW)
    GUISetOnEvent($gui_event_close, "Exitgui")

EndFunc   ;==>newgui

Func exitgui()
    Switch @GUI_WinHandle
        Case $gui
            Exit
        Case $gui2
            GUIDelete($gui2)
    EndSwitch
EndFunc   ;==>exitgui

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Naveed,

I would go further than KaFu and say that, as a general rule, you should only ever use just the one mode your script. Switching modes in the same script is fraught with danger and should only be used in the most exceptional circumstances - and even then you should have alarm bells ringing in your head asking why the circumstances are so exceptional and whether it would not be better to recast your code. ;)

Here is the same code without a mode change in the function: :blink:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('GuiOnEventMode', 1)

Global $gui, $gui2

$gui = GUICreate("test", 400, 100)
GUISetState(@SW_SHOW)
GUISetOnEvent($gui_event_close, "Exitgui")
$but = GUICtrlCreateButton("Test", 10, 10, 200, 50)
GUICtrlSetOnEvent($but, "newgui")

While 1
    Sleep(10)
WEnd

Func newgui()
    If IsHWnd($gui2) Then GUIDelete($gui2)

    $gui2 = GUICreate("Test 2", 200, 50)
    GUISetState(@SW_SHOW)
    GUISetOnEvent($gui_event_close, "Exitgui")

EndFunc   ;==>newgui

Func exitgui()
    Switch @GUI_WinHandle
        Case $gui
            Exit
        Case $gui2
            GUIDelete($gui2)
    EndSwitch
EndFunc   ;==>exitgui

M23

Thanks for the advice M23, i really appreciate all your help with this, i have definitely gained a better understanding of how the code works, i only wish i knew as much as you guys when it comes to writing auoit script......maybe one day.....

Share this post


Link to post
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
Sign in to follow this  
Followers 0