Jump to content

Bug: Menu and Controls stop working after SW_SHOW from another process


Go to solution Solved by argumentum,

Recommended Posts

Posted

I came across this issue in one of my projects and decided to recreate a smaller script to reproduce the issue and see if anyone knows how I can get around this issue.

  • The main GUI script has the ability to hide to it's system tray icon
  • Clicking on the tray icon will show the GUI again
  • In this case, the menu items (Help > About) and controls still work

I have a situation where I use another script to show the GUI again since it is normally hidden in the system tray.

  • GUI is already hidden in system tray
  • This second script runs to show the GUI
  • GUI does show and everything appears visually correct
  • In this case, the menu items (Help > About) and controls do NOT work

Yet if I hide the GUI again during the same run, and show itself again from the tray icon, everything works again.

Therefore, it only seems to happen when showing the GUI from another process.

Script1:

#NoTrayIcon

#include <AutoItConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>

Global $hGUI

Opt("TrayMenuMode", 3)
Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, 'idGUI')
TraySetClick(16)

$idGUI = TrayCreateItem("Show GUI")
TrayItemSetOnEvent($idGUI, "idGUI")
TrayCreateItem("")
$idExit = TrayCreateItem('Exit')
TrayItemSetOnEvent($idExit, "_Exit")
TraySetIcon(@AutoItExe, 1)
TraySetToolTip("Show GUI")

Example()

Func Example()
    Local $sDefaultstatus = "Ready"

    $hGUI = GUICreate("My GUI menu", 300, 200)

    Local $idMnu_File = GUICtrlCreateMenu("&File")
    Local $idMnu_Help = GUICtrlCreateMenu("Help")
    Local $idMni_Info = GUICtrlCreateMenuItem("About", $idMnu_Help)

    Local $idMnu_View = GUICtrlCreateMenu("View", -1, 1)

    Local $idBtn_Hide = GUICtrlCreateButton("Hide GUI", 50, 130, 70, 20)
    GUICtrlSetState(-1, $GUI_FOCUS)
    Local $idBtn_Cancel = GUICtrlCreateButton("Exit", 180, 130, 70, 20)

    Local $idLbl_Status = GUICtrlCreateLabel($sDefaultstatus, 0, 165, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

    GUISetState(@SW_SHOW)


    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $idBtn_Hide
                GUISetState(@SW_HIDE, $hGUI)
            Case $idBtn_Cancel, $GUI_EVENT_CLOSE
                ExitLoop
            Case $idMni_Info
                MsgBox($MB_SYSTEMMODAL, "Info", "Only a test...")
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

Func _Exit()
    Exit
EndFunc

Func idGUI()
    ; get GUI window state
    Local $iState = WinGetState($hGUI)
    If $iState <> $WIN_STATE_VISIBLE Then GUISetState(@SW_SHOW, $hGUI)
EndFunc

 

Script2:

#include <WinAPISysWin.au3>

; show and activate the GUI window
$hWnd = WinGetHandle("My GUI menu")
_WinAPI_ShowWindow($hWnd, @SW_SHOW)
_WinAPI_SetForegroundWindow($hWnd)

 

By the way, I have tried all of the other functions such as WinActivate and various WinAPI to show and give focus to the GUI window. They all still work to show the GUI but also still have controls and menu items that fail.

Does anyone know why this issue occurs or what I can do to fix it?

Thank you for your time. :)

Posted

Also, I realize that I am using OnEvent for tray stuff and GUIGetMsg for GUI stuff. In my main project, I use OnEvent for everything. The issue occurs either way and therefore this mix of OnEvent and GUIGetMsg in this example was just for the purpose of making a quick smaller example.

  • Solution
Posted
#NoTrayIcon

#include <AutoItConstants.au3>
#include <TrayConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>

Global $hGUI

Opt("TrayMenuMode", 3)
Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)

TraySetOnEvent($TRAY_EVENT_PRIMARYDOWN, 'idGUI')
TraySetClick(16)

Global $idGUI = TrayCreateItem("Show GUI")
TrayItemSetOnEvent($idGUI, "idGUI")
TrayCreateItem("")
Global $idExit = TrayCreateItem('Exit')
TrayItemSetOnEvent($idExit, "_Exit")
TraySetIcon(@AutoItExe, 1)
TraySetToolTip("Show GUI")

Example()

Func Example()
    Local $sDefaultstatus = "Ready"

    $hGUI = GUICreate("My GUI menu", 300, 200)

    Local $idMnu_File = GUICtrlCreateMenu("&File")
    Local $idMnu_Help = GUICtrlCreateMenu("Help")
    Local $idMni_Info = GUICtrlCreateMenuItem("About", $idMnu_Help)

    Local $idMnu_View = GUICtrlCreateMenu("View", -1, 1)

    Local $idBtn_Hide = GUICtrlCreateButton("Hide GUI", 50, 130, 70, 20)
    GUICtrlSetState(-1, $GUI_FOCUS)
    Local $idBtn_Cancel = GUICtrlCreateButton("Exit", 180, 130, 70, 20)

    Local $idLbl_Status = GUICtrlCreateLabel($sDefaultstatus, 0, 165, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN))

    GUISetState(@SW_SHOW)


    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $idBtn_Hide
;~                 GUISetState(@SW_HIDE, $hGUI)
                WinSetState($hGUI, "", @SW_HIDE)

            Case $idBtn_Cancel, $GUI_EVENT_CLOSE
                ExitLoop
            Case $idMni_Info
                MsgBox($MB_SYSTEMMODAL, "Info", "Only a test...")
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

Func _Exit()
    Exit
EndFunc

Func idGUI()
    If Not BitAND(WinGetState($hGUI), $WIN_STATE_VISIBLE) Then WinSetState($hGUI, "", @SW_SHOW)
;~     ; get GUI window state
;~     Local $iState = WinGetState($hGUI)
;~     If $iState <> $WIN_STATE_VISIBLE Then GUISetState(@SW_SHOW, $hGUI)
EndFunc

yes, is an old bug, and the way it is. Fortunately you can get around it.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
15 minutes ago, argumentum said:

yes, is an old bug, and the way it is. Fortunately you can get around it.

You literally just saved the rest of my weekend, Sir. Thank you! 🍷

So the trick was to essentially use WinSetState instead of GUISetState.

I really needed this to work, one way or another. I feel very lucky for the fact that there was a simple workaround for this issue and thankful that you were able to help me solve this issue. Now I can relax. :)

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
  • Recently Browsing   0 members

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