Jump to content

problem with closing current window


Recommended Posts

Hello there,

I just started using AutoIt so i'm kind of new into programming.

I have a problem with closing a window. I searched the forums and the AutoIt Help file over and over, but i can't find out what i'm doing wrong here?

My Script:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$mainWindow = GUICreate("my Window", 250, 150 )

$settings = GUICtrlCreateButton("Settings", 10, 100, 72.5, 30 )
GUICtrlSetOnEvent($settings, "changeSettings")

GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE,"exitProgram")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd



Func changeSettings()
    
    $settingswindow = GUICreate("Settings", 200, 100 )
    GUISetState(@SW_SHOW)
    GUISetOnEvent($GUI_EVENT_CLOSE, WinClose(""))
    
EndFunc



Func exitProgram()
    Exit
EndFunc

Hope one of you guys is able to help me.

Cheers,

JohnnyB

Link to comment
Share on other sites

  • Moderators

johnnybravo,

Welcome to the AutoIt forum. :blink:

Your window would not close because you need to put a function name into an GUISetOnEvent/GUICtrlSetOnEvent statement, not a command like WinClose.

This shows how you should do it: ;)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $settingswindow

$mainWindow = GUICreate("my Window", 250, 150 )

$settings = GUICtrlCreateButton("Settings", 10, 100, 72.5, 30 )
GUICtrlSetOnEvent($settings, "changeSettings")

GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE,"exitProgram")

While 1
    Sleep(10) ; In OnEvent mode you must have a short Sleep in this loop or you eat up CPU cycles <<<<<<<<<
WEnd

Func changeSettings()
    $settingswindow = GUICreate("Settings", 200, 100 )
    GUISetState(@SW_SHOW)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Close_Settings") : you needed a function name here <<<<<<<<<<<<
EndFunc

Func exitProgram()
    Exit
EndFunc

Func Close_Settings()
    GUIDelete($settingswindow)
EndFunc

Another point to bear in mind is that you were trying to mix OnEvent mode with GetMessage mode. : You cannot - you must use one or the other. The script above uses OnEvent mode - this one does exactly the same thing, but uses GetMessage mode:

#include <GUIConstantsEx.au3>

Global $settingswindow = 9999

$mainWindow = GUICreate("my Window", 250, 150 )

$settings = GUICtrlCreateButton("Settings", 10, 100, 72.5, 30 )

GUISetState(@SW_SHOW)

While 1
    $aMsg = GUIGetMsg(1)
    Switch $aMsg[1]
        Case $mainWindow
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $settings
                    $settingswindow = GUICreate("Settings", 200, 100 )
                    GUISetState(@SW_SHOW)
            EndSwitch
        Case $settingswindow
            Switch $aMsg[0]
                Case $GUI_EVENT_CLOSE
                    GUIDelete($settingswindow)
            EndSwitch
    EndSwitch

WEnd

Try and work out how each of them works. There are pros and cons to each method - you have to decide which is best for the particular task you are trying to do. :nuke:

As you seem pretty new to all this, could I strongly recommend that re-reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) - this will help you enormously. You should also look at the excellent tutorials that you will find here and here - you will find other tutorials in the Wiki (the link is at the top of the page). There are even video tutorials on YouTube if you prefer watching to reading.

Enjoy AutoIt - you know where we are when you run into difficulties. :P

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...