Sign in to follow this  
Followers 0
gritts

Tray menu requires multiple clicks to work

3 posts in this topic

Hello, I looking for an assist on a bit of an oddity. I have an app that I am building that is eventually intended to automatically click a button on a specific program. What I would also like to use a tray menu to perform some of the same tasks manually as opposed to waiting for the internal timer to trip.

I am testing my progress by using a dummy application I built that should closely duplicate what I am attempting to resolve. Here is the code for my dummy application..

$clkcount = 0
$rslt = 0

while $rslt < 2
    $rslt = MsgBox(1,"Click me","I have been clicked "&$clkcount&" times.")
    $clkcount +=1;
WEnd

The dummy application has been compiled into an executable.

When I run my application and use the Tray menu I have to click the menu item twice before it will perform its task. Its task is solely to click the OK button on my test application. Is this related to how I am using my While statement? Sorry if this seems confusing. Let me know if there is more I can provide.

#include <Constants.au3>
$windowtitle = "Click me"
$buttonname = "Button1"
$buttontext = "OK"


#NoTrayIcon
Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
TraySetClick(8)


$clickitem = TrayCreateItem("Click Now")
;TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1
    Switch TrayGetMsg()
    ;$msg = TrayGetMsg()
    Case 0
        ContinueLoop
    Case $TRAY_EVENT_PRIMARYDOWN ;reaction to left clicking
        Msgbox(64,"","You left clicked me...")
    Case $clickitem
        ;Msgbox(64, "Click:", "Click the button now.")
        TrayItemSetState($clickitem,$TRAY_UNCHECKED)
        If WinExists($windowtitle) Then
            ;WinActivate($windowtitle)
            ControlClick($windowtitle, "", $buttonname)
        EndIf
    Case $aboutitem
        TrayItemSetState($aboutitem,$TRAY_UNCHECKED)
        MsgBox(64, "about:", "AutoIt3-Tray-sample.")
    Case $exititem
        ExitLoop
    EndSwitch
WEnd

Exit

Share this post


Link to post
Share on other sites



gritts,

Welcome to the AutoIt forum. :)

A couple of things wrong with that code.

- Your GUI button will only ever react the first time - unless you are resetting $rslt somewhere else in the code. When you escape from the While...WEnd loop the first time, $rslt is set to 2 and therefore the loop will never run again. ;)

- In the tray code, I would use the more normal identifiers for the control rather than the ControlID.

When I run these 2 scripts, the button is clicked without problem:

GUI:

#include <GUIConstantsEx.au3>

$windowtitle = "Click me"
$buttonname = "Button1"
$clkcount = 1
$rslt = 0

$hGUI = GUICreate($windowtitle, 500, 500)
$hButton = GUICtrlCreateButton($buttonname, 10, 10, 80, 30)

GUISetState()
While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton

            While $rslt < 2
                $rslt = MsgBox(1,"Click me","I have been clicked "&$clkcount&" times.")
                $clkcount += 1;
            WEnd
            $rslt = 0

    EndSwitch

WEnd

Script:

#include <Constants.au3>

$windowtitle = "Click me"
$buttonname = "Button1"
$clkcount = 0
$rslt = 0

#NoTrayIcon
Opt("TrayMenuMode", 1) ; Default tray menu items (Script Paused/Exit) will not be shown.
TraySetClick(8)

$clickitem = TrayCreateItem("Click Now")
;TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()

While 1

    Switch TrayGetMsg()
        ;$msg = TrayGetMsg()
        Case $TRAY_EVENT_PRIMARYDOWN ;reaction to left clicking
            MsgBox(64, "", "You left clicked me...")
        Case $clickitem
            ;Msgbox(64, "Click:", "Click the button now.")
            TrayItemSetState($clickitem, $TRAY_UNCHECKED)
            If WinExists($windowtitle) Then
                WinActivate($windowtitle)
                ControlClick($windowtitle, "", "[CLASS:Button; INSTANCE:1]")
            EndIf
        Case $aboutitem
            TrayItemSetState($aboutitem, $TRAY_UNCHECKED)
            MsgBox(64, "about:", "AutoIt3-Tray-sample.")
        Case $exititem
            ExitLoop
    EndSwitch
WEnd

Exit

Please ask if anything is unclear - although I will be watching the Ryder Cup highlights for the next few hours - Bravo Europe!!! ;)

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

Thank you for the fast reply. I took a look at your example code and like how you handled the task menu script. I think I might need to give a little more background if anything for others who may be reading the thread.

With the test button script I think I understand your approach to reseting the $rslt value. One thing I did note in my testing prior to posting here is that if I use WinActivate() in my Tray Menu script, I can get a successful click each time. Removing the WinActivate(), the Tray Menu seems to do nothing the first execution and then on the second run, makes the test window active and the button is clicked. Is using WinActivate() essentially the same as reseting the $rslt value?

The final destination for the Tray Menu script is a system with proprietary apps running. Occasionally one of the apps will stick, requiring us to click on a specific button twice before it will continue. I suppose you could call it a band-aide of sorts while the developers work out the actual cause for the issue. Would using the WinActivate() be advisable in this case? The test button script was there to help me iron out other features for the Tray Menu script as I put them in. The Tray Menu script would eventually be a app that sleeps on a server, waking occasionally or gets run as a scheduled task.

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