Sign in to follow this  
Followers 0
ahha

How do you get tray icon mouseover to work 2nd time?

4 posts in this topic

#1 ·  Posted (edited)

I'm trying to do the following.

1) set tray icon and mouseover event

2) on tray icon mouseover open a window

3) user can then

..a) close window in which case mouseover tray icon displays it again (i.e. back to 2) OR

..B) minimize window

4) Now if user minimizes the window I want the window to restore on

..a) user explicitly restoring window from taskbar OR

..B) on tray icon mouseover I want it to restore.

All working except for 4 B).

I can't figure out why the "RestoreMessage" is never activating on a mouseover, see <<<<<<<<<<<<<<< marker in code.

I'd really like to figure out a way to poll for the mouseover as TraySetOnEvent appears to turn TrayGetMsg off.

However, the only tray mouseover function I can find is TraySetOnEvent.

#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <StaticConstants.au3>

Opt("TrayOnEventMode",1)    ;enable tray events
Opt("TrayMenuMode",1)       ;no default menu

Global $eventcount = 0      ;let's keep track of events

$exit = TrayCreateItem("Exit this Program") ;show Exit option to user.  In fact only item shown.
TrayItemSetOnEvent($exit,"ExitEvent")           ;associate tray item with routine to invoke
TraySetState(1)                                 ;show the tray icon so user can operate on it or trigger it
TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "DisplayMessage")    ;careful as this sends zillions of events automatically, need to turn it off ASAP after triggering to cut the event generation off
AutoItSetOption ("WinTitleMatchMode", 2)    ;Match any substring in the title

$d=0    ;debug display

While 1
    Sleep(2147483647 )  ; Idle loop - sleep for 24 days, basically sleep forever.
WEnd

Exit    


; =========================== Functions ===================================

Func DisplayMessage()
    TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "")  ;Turn it off immediately so that we cut down on multiple same events
    $eventcount = $eventcount + 1
    Msgbox(0+262144,"DisplayMessage","Inside DisplayMessage().   $eventcount="&$eventcount)
    ShowMessage()
    Msgbox(0+262144,"DisplayMessage","Going to turn mouseover back on.  $eventcount="&$eventcount)
    Opt("TrayOnEventMode",1)    ;enable tray events
    TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "DisplayMessage")    ;re-enable events after calendar window closed since we are not hovering over tray icon
EndFunc

Func RestoreMessage()
    TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "")  ;Turn it off immediately so that we cut down on multiple same events
    $eventcount = $eventcount + 1
    Msgbox(0+262144,"RestoreMessage","About to restore iconed program.   $eventcount="&$eventcount)
    WinSetState("This is a gui message box", "", @SW_RESTORE)   ;restore it
    ; leave TraySetOnEvent OFF  
EndFunc

Func ShowMessage()
    $cal_gui = GUICreate("This is a gui message box", 546, 750)
    GUISetBkColor (0xFFFF00)
    GUISetFont(9, 400, 0, "Courier New")
    GUICtrlCreateLabel("This is a message inside the box.", 0, 0)
    GUISetState()   ;show the GUI window
    Opt("TrayOnEventMode",0)    ;turn OFF tray events so we can use TrayGetMsg to poll for event
    
    While 1         
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then 
            Msgbox(0+262144,"Info","Closing window.  $eventcount="&$eventcount)
            GUIDelete($cal_gui)     ;destroy the window
            ExitLoop
        EndIf
        
        If TrayGetMsg() = $exit Then    ;check tray for Exit as well
            Msgbox(0+262144,"Info","Tray Exiting window.  $eventcount="&$eventcount)
            GUIDelete($cal_gui)     ;destroy the window
            Exit            ;out of entire program
        EndIf
        
        ;if the icon is minimized and user passes over tray icon again we should restore the window
        ;first see if window exists
        $state = WinGetState("This is a gui message box", "")
        If BitAnd($state, 16) Then  ;okay it's minimized
            ;okay if window minimized then re-enable events BUT point to another routine
            If $d=0 Then 
                Msgbox(0+262144,"Info","Window is minimized.  Going to activate RestoreMessage.")   ;show it once
                $d=2
            EndIf
            Opt("TrayOnEventMode",1)    ;enable tray events - turning off TrayGetMsg
            ;re-enable events after window closed since we are not hovering over tray icon
            TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "RestoreMessage")    ;<<<<<<<<<<<<<<<<<<<<<<< why is RestoreMessage never activating?
            ;TraySetOnEvent is turned off if actually do RestoreMessage
        EndIf
        
    WEnd
    
EndFunc   ;ShowMessage()


Func ExitEvent()
    Msgbox(0+262144,"ExitEvent","About to exit program.  $eventcount="&$eventcount)
    Exit
EndFunc
Edited by ahha

Share this post


Link to post
Share on other sites



I don't think a while loop within a while loop works well in OnEvent mode. You'll have to move a lot of your stuff inside the main while loop, or stick with either message mode or OnEvent mode. My opinion? Break things up into smaller functions and stick with OnEvent mode.

Share this post


Link to post
Share on other sites

I don't think a while loop within a while loop works well in OnEvent mode. You'll have to move a lot of your stuff inside the main while loop, or stick with either message mode or OnEvent mode. My opinion? Break things up into smaller functions and stick with OnEvent mode.

Thanks - I'm currently in the process of unwraveling it into the main while loop. I'm hoping I can get both OnEvent and TrayGetMsg to work at the same time.

Share this post


Link to post
Share on other sites

wraithdu,

Thanks for the hints.

Okay I now have it working perfectly. I have both TraySetOnEvent and TrayGetMsg working at the same time. :)

Example code below performs as wanted in first post above.

#include <GUIConstantsEx.au3>
#include <Constants.au3>

Opt("TrayOnEventMode",1)    ;enable tray events
Opt("TrayMenuMode",1)       ;no default menu

Global $eventcount = 0      ;let's keep track of events
Global $cal_gui

$exit = TrayCreateItem("Exit this Program")     ;show Exit option to user.  In fact only item shown.
TrayItemSetOnEvent($exit,"ExitEvent")           ;associate tray item with routine to invoke
TraySetState(1)                                 ;show the tray icon so user can operate on it or trigger it
TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "DisplayMessage")    ;careful as this sends zillions of events automatically, need to turn it off ASAP after triggering to cut the event generation off
AutoItSetOption ("WinTitleMatchMode", 2)        ;Match any substring in the title

$d=0    ;debug display 0=off, 1=on

DisplayMessage()    ;initially display message so user knows it's there and active

While 1
    ;poll to see if user closing window or wants to exit program
    ;display, restore, and activate all controlled by software interrupt events
    
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then 
        if $d=1 Then Msgbox(0+262144,"Info","Closing window.  $eventcount="&$eventcount)
        GUIDelete($cal_gui)     ;destroy the window
    EndIf
    
    If TrayGetMsg() = $exit Then    ;check tray for Exit as well
        if $d=1 Then Msgbox(0+262144,"Info","Tray Exiting window.  $eventcount="&$eventcount)
        GUIDelete($cal_gui)     ;destroy the window
        Exit            ;out of entire program
    EndIf

WEnd

Exit    


; =========================== Functions ===================================

Func DisplayMessage()
    TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "")  ;Turn it off immediately so that we cut down on multiple same events
    $eventcount = $eventcount + 1
    if $d=1 Then Msgbox(0+262144,"DisplayMessage","Inside DisplayMessage().   $eventcount="&$eventcount)

    ;BEFORE we draw the window check to see if it already exists in which case simply restore or activate it
    ;if the icon is minimized and user passes over tray icon again we should restore the window
    ;first see if window exists
    $state = WinGetState("This is a gui message box", "")
    If BitAnd($state, 16) Then  ;okay it's minimized - restore it
        if $d=1 Then Msgbox(0+262144,"DisplayMessage","About to restore iconed program.   $eventcount="&$eventcount)
        WinSetState("This is a gui message box", "", @SW_RESTORE)   ;restore it
    ElseIf BitAnd($state, 7) Then   ;okay not active - make it active so user can see it as probably hidden in the background
        if $d=1 Then Msgbox(0+262144,"DisplayMessage","About to make window active.   $eventcount="&$eventcount)
        WinActivate("This is a gui message box", "")    ;show it
    Else    ;okay draw the window
        $cal_gui = GUICreate("This is a gui message box", 300, 200)
        GUISetBkColor (0xFFFF00)
        GUISetFont(9, 400, 0, "Courier New")
        GUICtrlCreateLabel("This is a message inside the box.", 0, 0)
        GUISetState()   ;show the GUI window
    EndIf
            
    If $d=1 Then Msgbox(0+262144,"DisplayMessage","Going to turn mouseover back on.  $eventcount="&$eventcount)
    Opt("TrayOnEventMode",1)    ;enable tray events
    TraySetOnEvent ($TRAY_EVENT_MOUSEOVER, "DisplayMessage")    ;re-enable tray events events
EndFunc


Func ExitEvent()
    $ans = Msgbox(0+4+262144,"The Program - Exiting","Thank you for using the Program." &@CRLF &"Exit Program ?")
    If $ans = 7 Then
        Return  
    Else
        Exit
    EndIf
EndFunc

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