Jump to content

TrayMenu Issues


Recommended Posts

Hey all,

 

I am trying to figure out Tray Menu stuff here and it's boggling my mind.  I have looked through several posts in the forum, and looked up every keyword I can find in the help file....nothing seems to touch on this.

I want to disable the default tray menu, and I want some items to be checkable and others not to.  I am setting TrayMenuMode to 1, which SHOULD make it so it disables the default menu and lets you check things.  But this makes EVERYTHING be checked/unchecked when you click on it.  I have also tried settings TrayMenuMode to 3, which supposedly takes away any defaults set and will let you set whether it's checked or not on a case by case basis.  This doesn't work either.

In my example here, I want everything in the Monitoring menu to be checkable...and I DON'T want Exit to be checkable.  In fact, my Exit button doesn't even work and I have zero clue why.  That's also driving me nuts, but one thing at a time here.  Anyway, here is the chunk of code regarding the try menu below:

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

TraySetState()
TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ShowTrayMenu" )

   global $Monitoring         = TrayCreateMenu("Monitoring")
   global $PrintPolice        = TrayCreateItem("Print Server", $Monitoring)
   global $CreditConstable    = TrayCreateItem("Credit Server", $Monitoring)
   global $PMSPatrol           = TrayCreateItem("PMS Server", $Monitoring)
   TrayCreateItem("")
   global $exititem       = TrayCreateItem("Exit")

   Global $checked = TrayItemSetState($PrintPolice,$TRAY_UNCHECKED)
   Global $checked1 = TrayItemSetState($CreditConstable,$TRAY_UNCHECKED)
   Global $checked2 = TrayItemSetState($PMSPatrol,$TRAY_UNCHECKED)
   Global $checked3 = TrayItemSetState($exititem,$TRAY_UNCHECKED)

Func ShowTrayMenu()


   While 1
      global $msg = TrayGetMsg()
      Select
         Case $msg = 0
               ExitLoop
         Case $msg = $PrintPolice
            if $checked =  1 Then
               ;SplashTextOn("", "Print Server is checked")
               TrayItemSetState($PrintPolice,$TRAY_CHECKED)
               $checked = 4
               Local $iPid = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PrintPolice.exe")
            Else
               ;SplashTextOn("", "Print Server is unchecked")
               TrayItemSetState($PrintPolice,$TRAY_UNCHECKED)
               $checked = 1
               ProcessClose($iPid)
            EndIf
         Case $msg = $CreditConstable
            if $checked1 =  1 Then
               ;SplashTextOn("", "Credit Server is checked")
               TrayItemSetState($CreditConstable,$TRAY_CHECKED)
               $checked1 = 4
               Local $iPid1 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\CreditConstable.exe")
            Else
               ;SplashTextOn("", "Credit Server is unchecked")
               TrayItemSetState($CreditConstable,$TRAY_UNCHECKED)
               $checked1 = 1
               ProcessClose($iPid1)
            EndIf
         Case $msg = $PMSPatrol
            if $checked2 =  1 Then
               ;SplashTextOn("", "PMS Server is checked")
               TrayItemSetState($PMSPatrol,$TRAY_CHECKED)
               $checked2 = 4
               Local $iPid2 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PMSPatrol.exe")
            Else
               ;SplashTextOn("", "PMS Server is unchecked")
               TrayItemSetState($PMSPatrol,$TRAY_UNCHECKED)
               $checked2 = 1
               ProcessClose($iPid2)
            EndIf
         Case $msg = $exititem
            ;Opt("TrayMenuMode",3)
            if $checked3 = 1 Then
               _ExitScript()
            EndIf
      EndSelect
   WEnd

EndFunc

Func _ExitScript()
   ProcessClose("PrintPolice.exe")
   ProcessClose("CreditConstable.exe")
   ProcessClose("PMSPatrol.exe")
   ProcessClose('"DataDeputy Server.exe"')
EndFunc

 

Link to comment
Share on other sites

There was quite a few bugs in your code, here a working version :

#include <TrayConstants.au3>

Opt("TrayMenuMode", 3)

TraySetState()

Global $Monitoring = TrayCreateMenu("Monitoring")
Global $PrintPolice = TrayCreateItem("Print Server", $Monitoring)
Global $CreditConstable = TrayCreateItem("Credit Server", $Monitoring)
Global $PMSPatrol = TrayCreateItem("PMS Server", $Monitoring)
TrayCreateItem("")
Global $exititem = TrayCreateItem("Exit")

Global $checked = TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
Global $checked1 = TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
Global $checked2 = TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
ShowTrayMenu ()

Func ShowTrayMenu()
  Local $msg
  While 1
    $msg = TrayGetMsg()
    Select
      Case $msg = $PrintPolice
        If $checked = 1 Then
          ;SplashTextOn("", "Print Server is checked")
          TrayItemSetState($PrintPolice, $TRAY_CHECKED)
          $checked = 4
          ;Local $iPid = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PrintPolice.exe")
          ConsoleWrite("print check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "Print Server is unchecked")
          TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
          $checked = 1
          ;ProcessClose($iPid)
          ConsoleWrite("print check <> 1" & @CRLF)
        EndIf
      Case $msg = $CreditConstable
        If $checked1 = 1 Then
          ;SplashTextOn("", "Credit Server is checked")
          TrayItemSetState($CreditConstable, $TRAY_CHECKED)
          $checked1 = 4
          ConsoleWrite("credit check = 1" & @CRLF)
          ;Local $iPid1 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\CreditConstable.exe")
        Else
          ;SplashTextOn("", "Credit Server is unchecked")
          TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
          $checked1 = 1
          ;ProcessClose($iPid1)
          ConsoleWrite("credit check <> 1" & @CRLF)
        EndIf
      Case $msg = $PMSPatrol
        If $checked2 = 1 Then
          ;SplashTextOn("", "PMS Server is checked")
          TrayItemSetState($PMSPatrol, $TRAY_CHECKED)
          $checked2 = 4
          ;Local $iPid2 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PMSPatrol.exe")
          ConsoleWrite("patrol check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "PMS Server is unchecked")
          TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
          $checked2 = 1
          ;ProcessClose($iPid2)
          ConsoleWrite("patrol check <> 1" & @CRLF)
        EndIf
      Case $msg = $exititem
        _ExitScript()
    EndSelect
  WEnd

EndFunc   ;==>ShowTrayMenu

Func _ExitScript()
  ;ProcessClose("PrintPolice.exe")
  ;ProcessClose("CreditConstable.exe")
  ;ProcessClose("PMSPatrol.exe")
  ;ProcessClose('"DataDeputy Server.exe"')
  Exit
EndFunc   ;==>_ExitScript

 

Edited by Nine
Link to comment
Share on other sites

I took your code and pasted it into a file on it's own, and it works FLAWLESSLY.  However, when I try to cram it into my main code, it just doesn't work.  Am I not supposed to have this part in my main code??  Am I supposed to have it be it's own separate script file or something?

Link to comment
Share on other sites

17 minutes ago, maloysius said:

Am I supposed to have it be it's own separate script file or something?

No, it is a question of debugging.  You need to follow each statement with different messages (log file, msgbox, console write) to understand where your code is buggy.  If I look at the first draft of you code, you are not very good at programming (no offense). So you need to create only few lines of code and test it.  Creating a lot of lines without careful knowledge of programming creates for you a nightmare of bugs.  Just take you time, and check for errors at every single lines until you are sure that the code you made out is robust.

Link to comment
Share on other sites

Yeesh.....I got it....I was calling it completely wrong.  I was using TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ShowTrayMenu" ), because I thought just running the function itself would cause an infinite loop.  Nope, go figure!  Apologies for my noobishness and thank you for your help.

Link to comment
Share on other sites

Link to comment
Share on other sites

Oh boy, it actually is in an infinite loop and not performing any of the rest of the script.  Awesome.

I've discovered that it seems to have something to do with my TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ShowTrayMenu" ) command.  For some reason, the menu doesn't like to exist correctly when I do that, causing the issues mentioned earlier.  But as far as I can tell, this is the only way to actually correctly even call a tray menu while also executing the rest of the script.  I am at a total loss here :(

Link to comment
Share on other sites

It's very odd that Opt("TrayMenuMode",3), when in a file by itself with the tray script, seems to do the trick; it actually performs the exit procedure and kills tasks.  But when it's in my big script, suddenly Exit just "checks" and doesn't do anything.  I am going to be bald from all the hair pulling by the end of the day :(

Link to comment
Share on other sites

Nine, I highly appreciate you.  Not sure you really want to see this abomination in action, but here it is in all it's glory:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <WinAPIFiles.au3>
#include <File.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_ICONSTATE_SHOW constant.
#Include <Constants.au3>
#RequireAdmin
#NoTrayIcon

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

TraySetState()
TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ShowTrayMenu" )

   global $Monitoring         = TrayCreateMenu("Monitoring")
   global $PrintPolice        = TrayCreateItem("Print Server", $Monitoring)
   global $CreditConstable    = TrayCreateItem("Credit Server", $Monitoring)
   global $PMSPatrol           = TrayCreateItem("PMS Server", $Monitoring)
   TrayCreateItem("")
   global $exititem       = TrayCreateItem("Exit")

   Global $checked = TrayItemSetState($PrintPolice,$TRAY_UNCHECKED)
   Global $checked1 = TrayItemSetState($CreditConstable,$TRAY_UNCHECKED)
   Global $checked2 = TrayItemSetState($PMSPatrol,$TRAY_UNCHECKED)

   ;showtraymenu()

Func ShowTrayMenu()

  Local $msg
  While 1
    $msg = TrayGetMsg()
    Select
      Case $msg = $PrintPolice
        If $checked = 1 Then
          ;SplashTextOn("", "Print Server is checked")
          TrayItemSetState($PrintPolice, $TRAY_CHECKED)
          $checked = 4
          Local $iPid = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PrintPolice.exe")
          ;ConsoleWrite("print check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "Print Server is unchecked")
          TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
          $checked = 1
          ProcessClose($iPid)
          ;ConsoleWrite("print check <> 1" & @CRLF)
        EndIf
      Case $msg = $CreditConstable
        If $checked1 = 1 Then
          ;SplashTextOn("", "Credit Server is checked")
          TrayItemSetState($CreditConstable, $TRAY_CHECKED)
          $checked1 = 4
          ;ConsoleWrite("credit check = 1" & @CRLF)
          Local $iPid1 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\CreditConstable.exe")
        Else
          ;SplashTextOn("", "Credit Server is unchecked")
          TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
          $checked1 = 1
          ProcessClose($iPid1)
          ;ConsoleWrite("credit check <> 1" & @CRLF)
        EndIf
      Case $msg = $PMSPatrol
        If $checked2 = 1 Then
          ;SplashTextOn("", "PMS Server is checked")
          TrayItemSetState($PMSPatrol, $TRAY_CHECKED)
          $checked2 = 4
          Local $iPid2 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PMSPatrol.exe")
          ;ConsoleWrite("patrol check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "PMS Server is unchecked")
          TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
          $checked2 = 1
          ProcessClose($iPid2)
          ;ConsoleWrite("patrol check <> 1" & @CRLF)
        EndIf
      Case $msg = $exititem
        _ExitScript()
    EndSelect
  WEnd

EndFunc   ;==>ShowTrayMenu

Func _ExitScript()
   ProcessClose("PrintPolice.exe")
   ProcessClose("CreditConstable.exe")
   ProcessClose("PMSPatrol.exe")
   ProcessClose('"DataDeputy Server.exe"')
   Exit
EndFunc

Global $StartTimer = TimerInit()
Global $sIP = "DDSERVER"
Global $xi = 0
Global $ii = 999999
Global $DataPath = IniRead("C:\DDWIN\Application\DDDATA.ini", "Data", "Data Path", "Default Value")

CheckWindow()

Func _DataValidity()
   Global $Validity = _CheckDataValid()
EndFunc

Func _CheckDataValid()
   While 1
      Global $bResult = False
      Global $iErr = 0
      Global $sFileShare = "\\DDSERVER\DDWIN"
      ;Global $sFileShareFile = $sFileShare & "\BACKUP\Valid.txt"
      Sleep(1000)

      $iErr = DriveMapAdd("", $sFileShare)

      If $iErr = 0 or @error Then
         DriveMapDel($sFileShare)
         _Log('$Validity Returns False.')
         ;_Log('$DataValid Returns:')
         ;_Log($DataValid)
         Return "False"
      Else
         ;Global $DataValid = FileOpen("\\DDSERVER\DDWIN\BACKUP\Valid.txt", $FO_READ)
         ;If $DataValid <> -1 Then
         ;FileClose("\\DDSERVER\DDWIN\BACKUP\Valid.txt")
         ;FileDelete("\\DDSERVER\DDWIN\BACKUP\Valid.txt")
         DriveMapDel($sFileShare)
         _Log('$Validity Returns True.')
         ;_Log('$DataValid Returns:')
         ;_Log($DataValid)
         Return "True"
      EndIf
      ;If $DataValid = -1 or @error Then


     ; Endif
     ; If $iErr Then
         ;_Log('$Validity Returns False.')
        ; Return "False"
      ;EndIf
   WEnd
EndFunc

Func _Log($sLogEntry)
    Global $Log = FileOpen( "C:\DataDeputy\Log.txt", $FO_APPEND + $FO_CREATEPATH)
    _FileWriteLog($Log, $sLogEntry)
    fileclose($Log)
 EndFunc

 Func _RefreshNotificationAreaIcons($iTbar = 0)
    Switch @OSVersion
        Case "WIN_2000", "WIN_XP", "WIN_2003", "WIN_XPe"
            Return SetError(1, 1, 0)
    EndSwitch
    Local $hOwnerWin, $i_uID, $aRet, $iRet, $hTrayNotifyWnd, $iButtonCount = 0, _
            $hToolbar, $iCount, $iDLLUser32, $iDLLKrnl32, $iDLLShll32, _
            $tTBBUTTON, $pTBBUTTON, $iTBBUTTON, $tTRAYDATA, $pTRAYDATA, $iTRAYDATA, _
            $tNOTIFYICONDATA, $pNOTIFYICONDATA, $iProcessID, $hProcess, $pAddress
    $hTrayNotifyWnd = ControlGetHandle(WinGetHandle("[CLASS:Shell_TrayWnd]"), "", "[CLASS:TrayNotifyWnd]")
    Switch $iTbar
        Case 0
            $hToolbar = ControlGetHandle(ControlGetHandle($hTrayNotifyWnd, "", "[CLASS:SysPager]"), "", "[CLASS:ToolbarWindow32; INSTANCE:1]")
        Case 1
            $hToolbar = ControlGetHandle(WinGetHandle("[CLASS:NotifyIconOverflowWindow]"), "", "[CLASS:ToolbarWindow32; INSTANCE:1]")
        Case 2
            $hToolbar = ControlGetHandle($hTrayNotifyWnd, "", "[CLASS:ToolbarWindow32; INSTANCE:2]")
    EndSwitch
    $aRet = DllCall("user32.dll", "lparam", "SendMessageW", "hwnd", $hToolbar, "int", 0x418, "wparam", 0, "lparam", 0)
    If @error Or $aRet[0] < 1 Then Return SetError(2, @error, 0)
    $iCount = $aRet[0] - 1
    $iProcessID = WinGetProcess($hToolbar)
    If @error Or $iProcessID = -1 Then Return SetError(3, @error, 0)
    $aRet = DllCall("kernel32.dll", "ptr", "OpenProcess", "dword", 0x00000018, "int", 0, "int", $iProcessID)
    If @error Or $aRet[0] = 0 Then Return SetError(4, @error, 0)
    $hProcess = $aRet[0]
    $tTBBUTTON = DllStructCreate("int;int;byte;byte;align;dword_ptr;int_ptr")
    $pTBBUTTON = DllStructGetPtr($tTBBUTTON)
    $iTBBUTTON = DllStructGetSize($tTBBUTTON)
    If @error Or $iTBBUTTON = 0 Then Return SetError(5, @error, 0)
    $aRet = DllCall("kernel32.dll", "ptr", "VirtualAllocEx", "ptr", $hProcess, "ptr", 0, "int", $iTBBUTTON, "dword", 0x00001000, "dword", 0x00000004)
    If @error Or $aRet[0] = 0 Then
        DllCall("kernel32.dll", "int", "CloseHandle", "ptr", $hProcess)
        Return SetError(6, @error, 0)
    EndIf
    $pAddress = $aRet[0]
    $iDLLUser32 = DllOpen("user32.dll")
    $iDLLKrnl32 = DllOpen("kernel32.dll")
    $iDLLShll32 = DllOpen("shell32.dll")
    $tTRAYDATA = DllStructCreate("hwnd;uint;uint;dword[2];ptr")
    $pTRAYDATA = DllStructGetPtr($tTRAYDATA)
    $iTRAYDATA = DllStructGetSize($tTRAYDATA)
    $tNOTIFYICONDATA = DllStructCreate("dword;hwnd;uint;uint;uint;ptr;wchar[128];dword;dword;wchar[256];uint;wchar[64];dword;int;short;short;byte[8];ptr")
    $pNOTIFYICONDATA = DllStructGetPtr($tNOTIFYICONDATA)
    DllStructSetData($tNOTIFYICONDATA, 1, DllStructGetSize($tNOTIFYICONDATA))
    For $iID = $iCount To 0 Step -1
        If IsHWnd($hToolbar) = False Then ExitLoop
        $aRet = DllCall($iDLLUser32, "lparam", "SendMessageW", "hwnd", $hToolbar, "int", 0x417, "wparam", $iID, "lparam", $pAddress)
        If @error Or $aRet[0] <> 1 Then ContinueLoop
        $aRet = DllCall($iDLLKrnl32, "int", "ReadProcessMemory", "ptr", $hProcess, "ptr", $pAddress, "ptr", $pTBBUTTON, "int", $iTBBUTTON, "int*", -1)
        If @error Or $aRet[5] <> $iTBBUTTON Then ContinueLoop
        $aRet = DllCall($iDLLKrnl32, "int", "ReadProcessMemory", "ptr", $hProcess, "dword_ptr", DllStructGetData($tTBBUTTON, 5), "ptr", $pTRAYDATA, "int", $iTRAYDATA, "int*", -1)
        If @error Or $aRet[5] <> $iTRAYDATA Then ContinueLoop
        $hOwnerWin = DllStructGetData($tTRAYDATA, 1)
        If @error Or $hOwnerWin = 0 Then ContinueLoop
        If IsPtr($hOwnerWin) = 0 Or IsHWnd($hOwnerWin) = True Then ContinueLoop
        $i_uID = DllStructGetData($tTRAYDATA, 2)
        If @error Or $i_uID < 0 Then ContinueLoop
        $iRet = WinGetProcess($hOwnerWin)
        If @error Or $iRet <> -1 Then ContinueLoop
        DllStructSetData($tNOTIFYICONDATA, 2, $hOwnerWin)
        DllStructSetData($tNOTIFYICONDATA, 3, $i_uID)
        $aRet = DllCall($iDLLShll32, "int", "Shell_NotifyIconW", "dword", 0x2, "ptr", $pNOTIFYICONDATA)
        If @error Or $aRet[0] <> 1 Then ContinueLoop
        $iButtonCount += $aRet[0]
    Next
    DllCall($iDLLKrnl32, "int", "VirtualFreeEx", "ptr", $hProcess, "ptr", $pAddress, "int", 0, "dword", 0x00008000)
    DllCall($iDLLKrnl32, "int", "CloseHandle", "ptr", $hProcess)
    DllClose($iDLLShll32)
    DllClose($iDLLUser32)
    DllClose($iDLLKrnl32)
    Return SetError(0, 0, $iButtonCount)
EndFunc   ;==>_RefreshNotificationAreaIcons

Func _Start()
   Global $PingTested = _WaitForIP()
EndFunc

Func _WaitForIP()
   While 1
      Global $Ping = Ping("DDSERVER")
      Sleep(100)
      If $Ping = 0 or @error then
         _Log('$PingTested Returns A.')
         Return "A"
      Else
         _Log('$PingTested returns:')
         _Log($Ping)
         Return $Ping
      Endif
      If $xi = $ii then
         _Log('$PingTested Returns B.')
         Return "B"
      EndIf
      $xi = $xi +1
   WEnd
EndFunc

Func ConnectToData()
   ProcessClose("pos.exe")
   ProcessClose("ddprint.exe")
   ProcessClose("Pos Updater.exe")

   Global $ret = _RefreshNotificationAreaIcons(0);User Promoted Notification Area/Notification Area - Win7-2008R2/Vista
   ConsoleWrite("+User Promoted Notification Area/Notification Area: " & $ret & @LF)
   $ret = _RefreshNotificationAreaIcons(1);Overflow Notification Area - Win7-2008R2
   ConsoleWrite("+Overflow Notification Area: " & $ret & @LF)
   $ret = _RefreshNotificationAreaIcons(2);System Promoted Notification Area - Win7-2008R2
   ;(system clears these after 30 seconds anyway)
   ConsoleWrite("+System Promoted Notification Area: " & $ret & @LF)

   #Region ### START Koda GUI section ### Form=
   Global $Form1 = GUICreate("DataDeputy", 546, 335, -1, -1)
   GUISetIcon("C:\Windows\Oculus.ico", -1)
   Global $Pic1 = GUICtrlCreatePic("C:\DataDeputy\Digital-Dining-Logo.jpg", 64, 16, 172, 100)
   Global $Label1 = GUICtrlCreateLabel("This machine has lost connection to the data.", 32, 152, 482, 33)
   GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
   Global $Label2 = GUICtrlCreateLabel("Attempting to reconnect...", 144, 200, 274, 33)
   GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
   Global $Pic2 = GUICtrlCreatePic("C:\DataDeputy\opos logo.jpg", 304, 32, 172, 60)
   Global $Label3 = GUICtrlCreateLabel("If the problem persists, check your network cables, your network equipment, and", 40, 264, 474, 20)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
   Global $Label4 = GUICtrlCreateLabel("your server.  If still unable to connect, please call the help desk at 503-646-1383.", 40, 288, 469, 20)
   GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
   GUISetState(@SW_SHOW)
   #EndRegion ### END Koda GUI section ###

   While 1
      Global $nMsg = GUIGetMsg()
      Global $error = GUICtrlCreateLabel("", 420, 8, 60, 17)

      Sleep(1000)

      _Start()

      _DataValidity()

      If $PingTested <> 0 and $Validity = "True" Then
         ExitLoop
      EndIf

      If $PingTested = "A" Then
         _Log('(DataDeputy) PING ERROR: Server is not reachable on the network.')
         GuiCtrlSetData($error, "ERROR 1")
         Sleep(1000)
      Endif

      If $PingTested = "B" Then
         _Log('(DataDeputy) PING ERROR: Server is not reachable on the network.')
         GuiCtrlSetData($error, "ERROR 1")
         Sleep(1000)
      Endif

      If $PingTested <> 0 and $Validity = "False" Then
         _Log('(DataDeputy) DATA ERROR: Server is pingable, but unable to reach data.')
         GuiCtrlSetData($error, "ERROR 2")
         Sleep(1000)
      Endif

      Switch $nMsg
         Case $GUI_EVENT_CLOSE
            Exit
         Case $Label1
         Case $Label2
      EndSwitch

   WEnd

   _Log('(DataDeputy) Terminal reconnected to data.')

   GUIDelete("DataDeputy")

   ShellExecute( '"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' )

   If FileExists( '"C:\Users\POS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' ) Then
      ShellExecute( '"C:\Users\POS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' )
   EndIf

   SplashTextOn("DataDeputy Message", "CONNECTED!  RESTARTING DIGITAL DINING APPS. PLEASE DO NOT TOUCH SCREEN, MOUSE, OR KEYBOARD...", -1, 200, -1, -1, $DLG_CENTERONTOP, "", 24)

   While 1

      If WinExists("There was no valid data found.") Then
         WinClose("There was no valid data found.")
         _Log('(DataDeputy) Terminal disconnected from data.')
         SplashOff()
         ConnectToData()
      EndIf
      If WinExists("Digital Dining Application Startup   DDWIN") Then
         WinClose("Digital Dining Application Startup   DDWIN")

         ShellExecute( '"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' )

         If FileExists( '"C:\Users\POS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' ) Then
            ShellExecute( '"C:\Users\POS\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\DDStart.lnk"' )
         EndIf

      EndIf
      If ProcessExists( "pos.exe" ) Then
         ExitLoop
      EndIf
      If ProcessExists( "setup232.exe" ) Then
         ExitLoop
      EndIf
   WEnd

   If ProcessExists ( "pos.exe" ) Then
      SplashOff()
      ShellExecute( '"C:\DataDeputy\POS_Updater\POS Updater.exe"' )
      _Log('(DataDeputy) Successfully restarted POS.')
      $StartTimer = TimerInit()
      CheckWindow()
   EndIf

   If ProcessExists ( "setup232.exe" ) Then
      SplashOff()
      SplashTextOn("DataDeputy Message", "PLEASE WAIT WHILE YOUR DIGITAL DINING PROGRAM IS UPGRADED...", -1, 200, -1, -1, $DLG_CENTERONTOP, "", 24)
      DirCreate ( "C:\DDWIN\BACKUP" )
      FileCopy ( "C:\DDWIN\Application\*.ini", "C:\DDWIN\BACKUP", $FC_OVERWRITE )
      _Log('(DataDeputy) Digital Dining is being patched.')

      Do
         Sleep(1000)
      until ProcessExists ( "pos.exe" )
      _Log('(DataDeputy) Digital Dining successfully patched.')

      SplashOff()
      ShellExecute( '"C:\DataDeputy\POS_Updater\POS Updater.exe"' )
      _Log('(DataDeputy) Successfully restarted POS.')
      $StartTimer = TimerInit()
      CheckWindow()
   EndIf

EndFunc

Func CheckWindow()
   While 1
      If TimerDiff($StartTimer) > 1000 Then
         If WinExists("There was no valid data found.") Then
            WinClose("There was no valid data found.")
            _Log('(DataDeputy) Terminal disconnected from data.')
            ConnectToData()
         EndIf
         Sleep(10)
      EndIf
      If FileExists("\\DDSERVER\DDWIN\BACKUP\DataDeputy\UpdaterUpdate.txt") Then
         ProcessClose ( "POS Updater.exe")

         Global $ret = _RefreshNotificationAreaIcons(0);User Promoted Notification Area/Notification Area - Win7-2008R2/Vista
         ConsoleWrite("+User Promoted Notification Area/Notification Area: " & $ret & @LF)
         $ret = _RefreshNotificationAreaIcons(1);Overflow Notification Area - Win7-2008R2
         ConsoleWrite("+Overflow Notification Area: " & $ret & @LF)
         $ret = _RefreshNotificationAreaIcons(2);System Promoted Notification Area - Win7-2008R2
         ;(system clears these after 30 seconds anyway)
         ConsoleWrite("+System Promoted Notification Area: " & $ret & @LF)

         FileChangeDir("\\DDSERVER\DDWIN\BACKUP\DataDeputy\POS_Updater")
         Global $file1 = FileCopy("POS Updater.exe", "C:\DataDeputy\POS_Updater\", $FC_OVERWRITE)

         FileDelete("\\DDSERVER\DDWIN\BACKUP\DataDeputy\UpdaterUpdate.txt")

         ShellExecute( '"C:\DataDeputy\POS_Updater\POS Updater.exe"' )

         _Log('(DataDeputy) Updated POS Updater.')

         sleep(1000)
      EndIf
   WEnd
EndFunc

It's pretty messy.  I have done a lot of cleaning up, changing, and testing, some things just don't do stuff anymore.  I am not sure if you really wanna dig into this, I hope it's something that's just like "oh you missed a quotation mark" or something....

If you need some context, basically this program sits in wait until a window comes up that says No Valid Data Found, and then it will close the window, pull up a GUI, and try to reconnect to the host station.  Once it does, it goes back into dormancy.  It also features logging and update features.  I just want the dang thing to be able to do all that, and also have a tray menu.

Thanks again for your time good sir.

Link to comment
Share on other sites

Now I have the code below on the top:

TraySetOnEvent($TRAY_EVENT_SECONDARYUP, "ShowTrayMenu" )

This is what tells the script that when the tray icon is right clicked and released, run the ShowTrayMenu function as the tray menu.  It does in fact create the tray menu, but then nothing within that menu functions like when it is in a script by itself.  This is where I am getting stuck, I've attempted troubleshooting and read exhaustive lists of examples and help files.  I am 100% sure I must be thinking about it far too hard and it's probably something super simple...

Link to comment
Share on other sites

Oh god someone please help.  This is driving me insane.  I have been working on this today for 5 hours and have gotten nowhere.

 

All I need honestly is an example of how to put a tray menu into an actual full script.  Not just the script for the tray menu itself, where it just has it all in a function and just calls the function.  I need to have it in an already existing script, and I need that script to be able to run separately of the tray menu loop.  I can't possibly fathom why this is not explained anywhere, which leads me to believe that you should just be able to copy and paste it.  But this is just not the case.  I have a perfectly working tray menu in a script by itself, and when I try to copy it into my script, it just DOES NOT WORK.

 

Does anyone have any links to any examples that might be able to actually demonstrate this?

 

Thank you in advance.

Edited by maloysius
Link to comment
Share on other sites

@maloysius You cannot use :

Opt("TrayOnEventMode", 1)

and then use 

$msg = TrayGetMsg()

they are in contraction both of them,  I suggest you read carefully help file about those statements.  You will then understand the source of your problems.

Link to comment
Share on other sites

Hi Nine!

Gosh, I see that now...definitely a newb mistake.  I was trying to figure out a way to actually incorporate it into my script, and looks like I mixed a few terms up.

 

I have removed this, but it still seems to do the same thing...I've tried using Switch/Case instead of Select/Case, but it does the same thing.

 

This is the menu script that I have in it's own file.  I can run this flawlessly by itself.  It sees when it's checked and unchecked properly, it starts and stops the programs with no problem.  This is exactly how I intend it to function:

#include <TrayConstants.au3>
#NoTrayIcon

Opt("TrayMenuMode", 3)

TraySetState()

Global $Monitoring = TrayCreateMenu("Monitoring")
Global $PrintPolice = TrayCreateItem("Print Server", $Monitoring)
Global $CreditConstable = TrayCreateItem("Credit Server", $Monitoring)
Global $PMSPatrol = TrayCreateItem("PMS Server", $Monitoring)
TrayCreateItem("")
Global $exititem = TrayCreateItem("Exit")

Global $checked = TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
Global $checked1 = TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
Global $checked2 = TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
ShowTrayMenu ()

Func ShowTrayMenu()
  Local $msg
  While 1
    $msg = TrayGetMsg()
    Select
      Case $msg = $PrintPolice
        If $checked = 1 Then
          ;SplashTextOn("", "Print Server is checked")
          TrayItemSetState($PrintPolice, $TRAY_CHECKED)
          $checked = 4
          Local $iPid = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PrintPolice.exe")
          ConsoleWrite("print check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "Print Server is unchecked")
          TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
          $checked = 1
          ProcessClose($iPid)
          ConsoleWrite("print check <> 1" & @CRLF)
        EndIf
      Case $msg = $CreditConstable
        If $checked1 = 1 Then
          ;SplashTextOn("", "Credit Server is checked")
          TrayItemSetState($CreditConstable, $TRAY_CHECKED)
          $checked1 = 4
          ConsoleWrite("credit check = 1" & @CRLF)
          Local $iPid1 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\CreditConstable.exe")
        Else
          ;SplashTextOn("", "Credit Server is unchecked")
          TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
          $checked1 = 1
          ProcessClose($iPid1)
          ConsoleWrite("credit check <> 1" & @CRLF)
        EndIf
      Case $msg = $PMSPatrol
        If $checked2 = 1 Then
          ;SplashTextOn("", "PMS Server is checked")
          TrayItemSetState($PMSPatrol, $TRAY_CHECKED)
          $checked2 = 4
          Local $iPid2 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PMSPatrol.exe")
          ConsoleWrite("patrol check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "PMS Server is unchecked")
          TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
          $checked2 = 1
          ProcessClose($iPid2)
          ConsoleWrite("patrol check <> 1" & @CRLF)
        EndIf
      Case $msg = $exititem
        _ExitScript()
    EndSelect
  WEnd

EndFunc   ;==>ShowTrayMenu?

Func _ExitScript()
  ProcessClose("PrintPolice.exe")
  ProcessClose("CreditConstable.exe")
  ProcessClose("PMSPatrol.exe")
  ProcessClose('"DataDeputy Server.exe"')
  Exit
EndFunc   ;==>_ExitScript

How do I call this correctly to get it to function the same in my main script?

 

Thanks again for your expert input on this Nine.

Link to comment
Share on other sites

Link to comment
Share on other sites

Okay so I just totally re-did it from scratch in my main script, and lo and behold, it actually does it now!!

I have a shiny new issue though; since I call the function at the beginning of my script, it immediately goes into the loop of the tray menu and doesn't execute the rest of my script.  How do I get the program to only go into that loop when I am looking at the tray menu...?

I am hoping that this is not going to cause an issue, since my program pretty much revolves completely around While loops...can you have 2 loops running concurrently, independent of each other?  

Link to comment
Share on other sites

Link to comment
Share on other sites

Okay, I have these variables at the top of my script:

Opt("TrayMenuMode", 3)
;Opt("TrayAutoPause", 0)

Global $Monitoring = TrayCreateMenu("Monitoring")
Global $PrintPolice = TrayCreateItem("Print Server", $Monitoring)
Global $CreditConstable = TrayCreateItem("Credit Server", $Monitoring)
Global $PMSPatrol = TrayCreateItem("PMS Server", $Monitoring)
TrayCreateItem("")
Global $exititem = TrayCreateItem("Exit")

Global $checked = TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
Global $checked1 = TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
Global $checked2 = TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)



Global $StartTimer = TimerInit()
Global $sIP = "DDSERVER"
Global $xi = 0
Global $ii = 999999
Global $DataPath = IniRead("C:\DDWIN\Application\DDDATA.ini", "Data", "Data Path", "Default Value")

;ShowTrayMenu()
CheckWindow()

And the CheckWindow() is the main loop I want this to be available during, so I have implemented the code like this:

Func CheckWindow()
   Local $msg
   While 1
      If TimerDiff($StartTimer) > 1000 Then
         If WinExists("There was no valid data found.") Then
            WinClose("There was no valid data found.")
            _Log('(DataDeputy) Terminal disconnected from data.')
            ConnectToData()
         EndIf
         Sleep(10)
      EndIf
      If FileExists("\\DDSERVER\DDWIN\BACKUP\DataDeputy\UpdaterUpdate.txt") Then
         ProcessClose ( "POS Updater.exe")

         Global $ret = _RefreshNotificationAreaIcons(0);User Promoted Notification Area/Notification Area - Win7-2008R2/Vista
         ConsoleWrite("+User Promoted Notification Area/Notification Area: " & $ret & @LF)
         $ret = _RefreshNotificationAreaIcons(1);Overflow Notification Area - Win7-2008R2
         ConsoleWrite("+Overflow Notification Area: " & $ret & @LF)
         $ret = _RefreshNotificationAreaIcons(2);System Promoted Notification Area - Win7-2008R2
         ;(system clears these after 30 seconds anyway)
         ConsoleWrite("+System Promoted Notification Area: " & $ret & @LF)

         FileChangeDir("\\DDSERVER\DDWIN\BACKUP\DataDeputy\POS_Updater")
         Global $file1 = FileCopy("POS Updater.exe", "C:\DataDeputy\POS_Updater\", $FC_OVERWRITE)

         FileDelete("\\DDSERVER\DDWIN\BACKUP\DataDeputy\UpdaterUpdate.txt")

         ShellExecute( '"C:\DataDeputy\POS_Updater\POS Updater.exe"' )

         _Log('(DataDeputy) Updated POS Updater.')

         sleep(1000)
      EndIf

      $msg = TrayGetMsg()
    Switch $msg
      Case $msg = $PrintPolice
        If $checked = 1 Then
          ;SplashTextOn("", "Print Server is checked")
          TrayItemSetState($PrintPolice, $TRAY_CHECKED)
          $checked = 4
          Local $iPid = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PrintPolice.exe")
          ConsoleWrite("print check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "Print Server is unchecked")
          TrayItemSetState($PrintPolice, $TRAY_UNCHECKED)
          $checked = 1
          ProcessClose($iPid)
          ConsoleWrite("print check <> 1" & @CRLF)
        EndIf
      Case $msg = $CreditConstable
        If $checked1 = 1 Then
          ;SplashTextOn("", "Credit Server is checked")
          TrayItemSetState($CreditConstable, $TRAY_CHECKED)
          $checked1 = 4
          ConsoleWrite("credit check = 1" & @CRLF)
          Local $iPid1 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\CreditConstable.exe")
        Else
          ;SplashTextOn("", "Credit Server is unchecked")
          TrayItemSetState($CreditConstable, $TRAY_UNCHECKED)
          $checked1 = 1
          ProcessClose($iPid1)
          ConsoleWrite("credit check <> 1" & @CRLF)
        EndIf
      Case $msg = $PMSPatrol
        If $checked2 = 1 Then
          ;SplashTextOn("", "PMS Server is checked")
          TrayItemSetState($PMSPatrol, $TRAY_CHECKED)
          $checked2 = 4
          Local $iPid2 = Run("C:\DDWIN\BACKUP\DataDeputy\Monitoring\PMSPatrol.exe")
          ConsoleWrite("patrol check = 1" & @CRLF)
        Else
          ;SplashTextOn("", "PMS Server is unchecked")
          TrayItemSetState($PMSPatrol, $TRAY_UNCHECKED)
          $checked2 = 1
          ProcessClose($iPid2)
          ConsoleWrite("patrol check <> 1" & @CRLF)
        EndIf
      Case $msg = $exititem
        _ExitScript()
    EndSwitch

      $StartTimer = TimerInit()
   WEnd
EndFunc

Now the tray menu works, but is EXTREMELY slow...to the point of taking about 10 seconds to register...and it seems to invalidate everything else that's going on in the CheckWindow() function.  I am so close I can almost taste it.......

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