Jump to content

Dialog messages not handled when opening dialog from system tray entry


Go to solution Solved by Melba23,

Recommended Posts

I posted this question on StackOverflow, but I don't see a lot of activity there, so I think my chance of getting it answered here is perhaps better:

I have an AutoIt script in which I open a settings dialog from a system tray menu entry. When opening the dialog this way, messages via button clicks are not handled.

On the other hand, when opening the dialog directly (as indicated in the code below, which you can easily test by uncommenting this call and commenting out the call for the system tray entry), then the messages are handled successfully.

Here is my script. When calling SettingsDialog directly (without going via the systray menu), the OK and Cancel buttons work, but otherwise not. (Sorry, I don't know how to format the code nicely. When pasting, I lose the tabs).

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

;; Start program in system tray
SetupSystemTrayEntry()

;; When calling settings dialog directly, messages are handled properly
;;SettingsDialog()


Func SetupSystemTrayEntry()
Opt("TrayMenuMode", 1)

$settingsitem = TrayCreateItem("Settings")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
Local $traymsg = TrayGetMsg()
Select
Case $traymsg = 0
ContinueLoop
Case $traymsg = $settingsitem
SettingsDialog() ;; Bring up settings dialog
Case $traymsg = $exititem
Exit ;; Exit program
EndSelect
WEnd
EndFunc   ;==>SetupSystemTrayEntry

Func SettingsDialog()
GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
$ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
$cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)

GUISetState()

Do
;; These messages are never handled when the dialog is brought up from
;; the system tray menu entry above, but when calling this function
;; directly, it works
Local $settmsg = GUIGetMsg()
Select
Case $settmsg = $ok_button
ExitLoop
Case $settmsg = $cancel_button
ExitLoop
EndSelect
Until $settmsg = $GUI_EVENT_CLOSE


EndFunc   ;==>SettingsDialog

 

Link to comment
Share on other sites

  • Moderators

mydoghasworms,

Welcome to the AutoIt forum. :)

Your buttons work for me when called from the tray menu - why do you think they do not? :huh:

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

;; Start program in system tray
SetupSystemTrayEntry()

;; When calling settings dialog directly, messages are handled properly
;;SettingsDialog()

Func SetupSystemTrayEntry()
    Opt("TrayMenuMode", 1)

    $settingsitem = TrayCreateItem("Settings")
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Local $traymsg = TrayGetMsg()
        Select
            Case $traymsg = 0
                ContinueLoop
            Case $traymsg = $settingsitem
                SettingsDialog() ;; Bring up settings dialog
            Case $traymsg = $exititem
                Exit ;; Exit program
        EndSelect
    WEnd
EndFunc   ;==>SetupSystemTrayEntry

Func SettingsDialog()
    GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
    $ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
    $cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)

    GUISetState()

    Do
        ;; These messages are never handled when the dialog is brought up from
        ;; the system tray menu entry above, but when calling this function
        ;; directly, it works
        Local $settmsg = GUIGetMsg()
        Select
            Case $settmsg = $ok_button
                MsgBox(0, "Hi", "OK Pressed")
                ExitLoop
            Case $settmsg = $cancel_button
                MsgBox(0, "Hi", "Cancel Pressed")
                ExitLoop
        EndSelect
    Until $settmsg = $GUI_EVENT_CLOSE

EndFunc   ;==>SettingsDialog
And see here how to post code. :)

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

Thanks for the welcome and the hint about the code.

The buttons (OK, Cancel) respond when I call that function directly. That is, the popup disappears (as it exits out of that loop). When I open from the system tray as per the example, the buttons do nothing when clicked. Could it be a platform issue? I am running it as an x86 script on Windows 7 64 bit.

Link to comment
Share on other sites

  • Moderators

mydoghasworms,

 

When I click a button, it pops up a message box, then does nothing (dialog stays there). Successive button presses do nothing

Which is exactly what your code asks the script to do. You run ExitLoop after the button press so you immediately exit the SettingsDialog function. ;)

What do you want to happen? :huh:

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

Okay... I see what is happening. When calling directly, the script exits when the function exits, and the dialog disappears.

When calling the dialog from the systray function, the function exits, but the dialog is still there, correct? I need to explicitly dismiss/close/exterminate my dialog, is that right?

How do I dismiss the dialog? I will check in the API docs quickly...

Thanks! Now I see what is happening, so I can be on my merry way again.  :thumbsup:

Link to comment
Share on other sites

Now I changed my script to set up the settings dialog once, store the handle in a global variable, and show the dialog when needed. But this is not working as expected. The dialog never shows and the script exits (though it does enter the ShowSettingsDialog function):

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

;; Set up settings dialog
InitSettingsDialog();

;; Start program in system tray
SetupSystemTrayEntry()

Global $settings_window_handle

Func SetupSystemTrayEntry()
    Opt("TrayMenuMode", 1)

    $settingsitem = TrayCreateItem("Settings")
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Local $traymsg = TrayGetMsg()
        Select
            Case $traymsg = 0
                ContinueLoop
            Case $traymsg = $settingsitem
                ShowSettingsDialog() ;; Bring up settings dialog
            Case $traymsg = $exititem
                Exit ;; Exit program
        EndSelect
    WEnd
EndFunc   ;==>SetupSystemTrayEntry

Func InitSettingsDialog()
    $settings_window_handle = GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
    Global $ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
    Global $cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
EndFunc   ;==>SettingsDialog

Func ShowSettingsDialog()
    GUISetState(@SW_SHOW, $settings_window_handle)

    Local $settmsg
    While 1
        $settmsg = GUIGetMsg()
        Select
            Case $settmsg = $ok_button
                GUISetState(@SW_HIDE, $settings_window_handle)
                ExitLoop
            Case $settmsg = $cancel_button
                ExitLoop
                GUISetState(@SW_HIDE, $settings_window_handle)
        EndSelect
    WEnd
EndFunc
Link to comment
Share on other sites

OK, I had declared $settings_window_handle after initializing it, so it was local only the initializer function. Now, I tried with the following code, and it shows the dialog successfully, but does not hide it. In this case, it does not reach the Select in the loop.

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

Global $settings_window_handle

;; Set up settings dialog
InitSettingsDialog();

;; Start program in system tray
SetupSystemTrayEntry()

Func SetupSystemTrayEntry()
    Opt("TrayMenuMode", 1)

    $settingsitem = TrayCreateItem("Settings")
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Local $traymsg = TrayGetMsg()
        Select
            Case $traymsg = 0
                ContinueLoop
            Case $traymsg = $settingsitem
                ShowSettingsDialog() ;; Bring up settings dialog
            Case $traymsg = $exititem
                Exit ;; Exit program
        EndSelect
    WEnd
EndFunc   ;==>SetupSystemTrayEntry

Func InitSettingsDialog()
    $settings_window_handle = GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
    Global $ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
    Global $cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
EndFunc   ;==>SettingsDialog

Func ShowSettingsDialog()
    GUISetState(@SW_SHOW, $settings_window_handle)

    Local $settmsg
    While 1
        $settmsg = GUIGetMsg()
        Select
            Case $settmsg = $ok_button
                MsgBox(0, "test", "test")
                GUISetState(@SW_HIDE, $settings_window_handle)
                ExitLoop
            Case $settmsg = $cancel_button
                ExitLoop
                GUISetState(@SW_HIDE, $settings_window_handle)
        EndSelect
    WEnd
EndFunc
Link to comment
Share on other sites

  • Moderators
  • Solution

mydoghasworms,

If the GUI is simple, then creating it intially and hiding/showing it as required is always a good idea in my book. :)

Try this slight variation - I also used Switch in place of Select just to show how it is done:

#include <GUIConstantsEx.au3>

; Do not declare Global variables in a function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $settings_window_handle, $ok_button, $cancel_button

;; Set up settings dialog
InitSettingsDialog();

;; Start program in system tray
SetupSystemTrayEntry()

Func SetupSystemTrayEntry()
    Opt("TrayMenuMode", 1)

    $settingsitem = TrayCreateItem("Settings")
    TrayCreateItem("")
    $exititem = TrayCreateItem("Exit")

    TraySetState()

    While 1
        Switch TrayGetMsg()
            Case $settingsitem
                ShowSettingsDialog() ;; Bring up settings dialog
            Case $exititem
                Exit ;; Exit program
        EndSwitch
    WEnd
EndFunc   ;==>SetupSystemTrayEntry

Func InitSettingsDialog()
    ; Create
    $settings_window_handle = GUICreate("Settings", 400, 150, @DesktopWidth / 2 - 200, @DesktopHeight / 2 - 75)
    $ok_button = GUICtrlCreateButton("OK", 100, 100, 80, 25, $BS_DEFPUSHBUTTON)
    $cancel_button = GUICtrlCreateButton("Cancel", 200, 100, 80, 25)
    GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc   ;==>SettingsDialog

Func ShowSettingsDialog()
    GUISetState(@SW_SHOW, $settings_window_handle)
    While 1
        Switch GUIGetMsg()
            Case $ok_button
                MsgBox(0, "test", "test")
                ExitLoop
            Case $cancel_button
                ExitLoop
        EndSwitch
    WEnd
    GUISetState(@SW_HIDE, $settings_window_handle)
EndFunc
Better? :)

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