Having trouble with 'Right-Click on Systray icon' .

Just starting out? Need help? Post your questions and find answers here.
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Having trouble with 'Right-Click on Systray icon' .

Post by vmars316 »

TIA ,

I'm Having trouble with 'Right-Click on Systray icon' .
In the code below , everything works
except this

Code: Select all

    If Event = #PB_Event_Menu ; <<-- PopUp Event 
      Select EventMenu() 
        Case 1 ; Restore 
           RemoveSysTrayIcon(1) 
           HideWindow(0, #False)
           MessageRequester("Info", "Press OK and then the Minimize button on the window", 0)
           PostEvent(#PB_Event_MinimizeWindow)
        Case 2 ; Quit 
           Quit = #True
        Case 3 ; Help
            RunProgram("Hibernate-Help.html") 
      EndSelect 
          MessageRequester(" Select EventMenu()" , "Select EventMenu() case 1 , 2 , 3")
    EndIf 
Program never gets to : MessageRequester()
Here is the full pgm code :

Code: Select all

; -----------------------------------------------------------------------------
; 1    ; C:\PureBasic-myApps\Examples 
; 2    ; "Hide_To_Tray" Author: PWS32 (updated for PB4.00 by blbltheworm) for Hide_To_Tray
; 3    ; "Hibernate_Plus" Author: vmars316 Combined into one program  
; CRLF = +Chr(10)+Chr(34)+
; -----------------------------------------------------------------------------

EnableExplicit

Define Event.i ;, Quit.i  

;- Enumerations
Enumeration Window
  #Window_0 = 0
EndEnumeration

Enumeration Gadgets ; Only Constants are allowed in Enumeration
;  String_1
;  String_2
EndEnumeration

Enumeration Font
  #Font_1
;  #Font_2
EndEnumeration

;- Load Fonts
LoadFont(#Font_1, "Tahoma", 22)
;LoadFont(#Font_2, "Tahoma", 16)
:
Global x=0 , y=0 , top=0 , left=0 , stringContainer1 , stringContainer2 , stringContainer3 ,
       Window_0 , Quit = #False ,
       String_1 , String_2 , String_3 , ToTrayButton ,
       Hour$ , Minute$ , TimesThru = 0 , ReadThisFile$ = "TimeParms.txt" , 
       FirstLine$ , SecondLine$ , Hourz , MinuteZ
;- Declare
Declare Open_Window_0() 
Declare CheckTimeParams()
Declare Clock()
Declare KreatePopupMenu()
Declare Hide_Window_Load_Image()
;
Procedure Open_Window_0() 
  If OpenWindow(0, X, Y, 140, 106, "Hibernate-Timed" , #PB_Window_SystemMenu | #PB_Window_ScreenCentered) ;  ,  #PB_Window_BorderLess)
    stringContainer1 = ContainerGadget(#PB_Any, 0 , 0 , 160 , 55) ; String$ = GetGadgetText(#Gadget)
      SetGadgetColor(stringContainer1 , #PB_Gadget_BackColor,RGB(0,0,0))
     String_1 = StringGadget(#PB_Any, 0 , 0 , 140 , 40 , "String_1")
       GadgetToolTip(String_1 , "Show Current Time , hour:minute:second .")
       SetGadgetColor(String_1, #PB_Gadget_FrontColor, RGB(255, 255, 255))
       SetGadgetColor(String_1, #PB_Gadget_BackColor, RGB(0, 0, 0))
       SetGadgetFont(String_1, FontID(#Font_1))
    CloseGadgetList()   
       
     stringContainer2 = ContainerGadget(#PB_Any, 0 , 50 , 160 , 55) ; String$ = GetGadgetText(#Gadget)
      SetGadgetColor(stringContainer2 , #PB_Gadget_BackColor,RGB(0,0,0))
     String_2 = StringGadget(#PB_Any, 0 , 0 , 43 , 40 , "00" , #PB_String_Numeric)
       GadgetToolTip(String_2 , "Enter Hour '0 - 23' , When To Hibernate .")
       SetGadgetColor(String_2, #PB_Gadget_FrontColor, RGB(255, 255, 255))
       SetGadgetColor(String_2, #PB_Gadget_BackColor, RGB(0, 0, 0))
       SetGadgetFont(String_2, FontID(#Font_1))
;    CloseGadgetList()   
     stringContainer3 = ContainerGadget(#PB_Any, 46 , 0 , 160 , 55) ; String$ = GetGadgetText(#Gadget)
      SetGadgetColor(stringContainer3 , #PB_Gadget_BackColor,RGB(0,0,0))
     String_3 = StringGadget(#PB_Any, 0 , 0 , 43 , 40 , "00" , #PB_String_Numeric)
       GadgetToolTip(String_3 , "Enter Minute '1 - 59' , When To Hibernate .")
       SetGadgetColor(String_3, #PB_Gadget_FrontColor, RGB(255, 255, 255))
       SetGadgetColor(String_3, #PB_Gadget_BackColor, RGB(0, 0, 0))
       SetGadgetFont(String_3, FontID(#Font_1))
     ToTrayButton = ButtonGadget(#PB_Any, 48 , 0 , 46 , 40 , "Tray" )   
       GadgetToolTip(ToTrayButton , "Click to 'Send/Hide Program' in TaskTray .")
    CloseGadgetList()   
  
  EndIf
EndProcedure
;
Procedure CheckTimeParams()  ; Check validity of "TimeParms"
  Global k = 0 , ReadInput$ = ""
  If OpenFile(0, "TimeParms.txt")   ; if the file could be read, we continue...
  For k = 1 To 2 
    ReadInput$ = ReadString(0)  
    If k = 1  
      Hour$ = ReadInput$
      Hourz = Val(Hour$)
      If Str(Val(Hour$)) <> Hour$ Or Len(Hour$) = 0 Or Hourz > 23; Either = Bad input
        MessageRequester("ERROR", "Hour must be a number"  +Chr(10)+Chr(34)+ 
                                  "containg 1 Or 2 digits!" +Chr(10)+Chr(34)+
                                  "And NOT Greater Than 23" +Chr(10)+Chr(34)+
                                  "Edit 'TimeParms.txt' file and RESTART Program")
        Quit = #True
      EndIf   
      Break 
     EndIf 
;
    If k = 2  
      Minute$ = ReadInput$
      Minutez = Val(Minute$)
      If Str(Val(Minute$)) <> Minute$ Or Len(Minute$) = 0 Or Minutez > 59 ; Either = Bad input
        MessageRequester("ERROR", "Minute must be a number"  +Chr(10)+Chr(34)+ 
                                  "containg 1 Or 2 digits!" +Chr(10)+Chr(34)+
                                  "And NOT Greater Than 59" +Chr(10)+Chr(34)+
                                  "Edit 'TimeParms.txt' file and RESTART Program")
        Quit = #True 
      EndIf   
      Break  
    EndIf 
  Next  ;  For k = 1 To 2 
  EndIf ;  If OpenFile(0,
EndProcedure  ;  CheckTimeParams()
;
Procedure KreatePopupMenu()
  CreatePopupMenu(0) 
    MenuItem(1, "Restore Window") 
     MenuBar() 
    MenuItem(2, "Quit") 
     MenuBar() 
    MenuItem(3, "Help") 
EndProcedure ; KreatePopupMenu()
:
Procedure Hide_Window_Load_Image()
MessageRequester("Info", "Click to Move Program to SYSTRAY") 
  HideWindow(0, #True) 
  LoadImage(0, "HT.ico")
  
;  Debug WindowID(0) : Debug ImageID(0)
  
  AddSysTrayIcon(1, WindowID(0), ImageID(0)) ; <<--- Take any 16X16 icon here 
  SysTrayIconToolTip (1, "Right-click for OPTIONS") 
  
EndProcedure ; Hide_Window_Load_Image()
;
Procedure Clock()  ;  
  Global Time$ 
  Time$ = FormatDate("%hh:%ii:%ss", Date())
  SetGadgetText(String_1, Time$)
  
  If Hourz = Hour(Date())  And  Minutez = Minute(Date())
    If TimesThru = 0 ; = First Time Thru 'If Hourz = Hour(Date())  And '
       TimesThru = 1
;      RunProgram(Hibernate-Now.exe) 
       MessageRequester("FirstLine$ ","FirstLine$ = " + FirstLine$      +Chr(10)+Chr(34)+ 
                                     "    SecondLine$ = " + SecondLine$ +Chr(10)+Chr(34)+
                                     "    TimesThru = " + Str(TimesThru ) )
    EndIf 
  EndIf 
  
  If Not Hourz = Hour(Date())  Or  Not Minutez = Minute(Date())
    If TimesThru = 1 
       Debug "If Not Hourz = Hour(Date())  Or  Not Minutez = Minute(Date())"
       Debug "TimesThru = " + TimesThru
      TimesThru = 0
       Debug "TimesThru now = " + TimesThru
    EndIf 
    
  EndIf ;  If Not Hour$ = FirstLine$
EndProcedure  ;  If OpenFile(0,
;
;- Main Program
Open_Window_0()
SetTimer_(WindowID(#Window_0), 1,1000, @Clock() ) ; Update the clock every 1000mS
CheckTimeParams()
Clock()
KreatePopupMenu()

;
;- Event Loop
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      KillTimer_(WindowID(#Window_0),1)
      Quit = #True
    Case #PB_Event_Gadget
  Select EventGadget()
         Case ToTrayButton
           MessageRequester("Case ToTrayButton" , "ToTrayButton was Clicked !") 
           Hide_Window_Load_Image()  
;
  EndSelect       
  EndSelect 
;  
    Event = WaitWindowEvent() 

    If Event = #PB_Event_SysTray ; <<-- PopUp when you right click on your systray icon
      If EventType() = #PB_EventType_RightClick 
        DisplayPopupMenu(0, WindowID(0)) 
      EndIf 
    EndIf 
    
    If Event = #PB_Event_Menu ; <<-- PopUp Event 
      Select EventMenu() 
        Case 1 ; Restore 
           RemoveSysTrayIcon(1) 
           HideWindow(0, #False)
           MessageRequester("Info", "Press OK and then the Minimize button on the window", 0)
           PostEvent(#PB_Event_MinimizeWindow)
        Case 2 ; Quit 
           Quit = #True
        Case 3 ; Help
            RunProgram("Hibernate-Help.html") 
      EndSelect 
;          MessageRequester(" Select EventMenu()" , "Select EventMenu() case 1 , 2 , 3")
    EndIf 

    If Event = #PB_Event_MinimizeWindow ;<<-- same With the Minimize button 
      HideWindow(0, #True)
      AddSysTrayIcon(1, WindowID(0), ImageID(0)) 
      SysTrayIconToolTip(1, "Right Mouse Button for PopUp")    
    EndIf 
    
    If Event = #PB_Event_CloseWindow 
      Quit = #True
    EndIf 
    
    
  Until Quit = #True 

vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Having trouble with 'Right-Click on Systray icon' .

Post by breeze4me »

This is because the WaitWindowEvent() function is used twice in the event loop. You can fix it like below.

Code: Select all

;- Event Loop
Repeat
  Event = WaitWindowEvent() 
  
  Select Event
    Case #PB_Event_CloseWindow
      KillTimer_(WindowID(#Window_0),1)
      Quit = #True
    Case #PB_Event_Gadget
      Select EventGadget()
        Case ToTrayButton
          MessageRequester("Case ToTrayButton" , "ToTrayButton was Clicked !") 
          Hide_Window_Load_Image()  
          ;
      EndSelect       
  EndSelect 
  ;  
  
  If Event = #PB_Event_SysTray ; <<-- PopUp when you right click on your systray icon
    If EventType() = #PB_EventType_RightClick 
      DisplayPopupMenu(0, WindowID(0)) 
    EndIf 
  EndIf 
  
  If Event = #PB_Event_Menu ; <<-- PopUp Event 
    Select EventMenu() 
      Case 1 ; Restore 
        RemoveSysTrayIcon(1) 
        HideWindow(0, #False)
        MessageRequester("Info", "Press OK and then the Minimize button on the window", 0)
        PostEvent(#PB_Event_MinimizeWindow)
      Case 2 ; Quit 
        Quit = #True
      Case 3 ; Help
        RunProgram("Hibernate-Help.html") 
    EndSelect 
    ;          MessageRequester(" Select EventMenu()" , "Select EventMenu() case 1 , 2 , 3")
  EndIf 
  
  If Event = #PB_Event_MinimizeWindow ;<<-- same With the Minimize button 
    HideWindow(0, #True)
    AddSysTrayIcon(1, WindowID(0), ImageID(0)) 
    SysTrayIconToolTip(1, "Right Mouse Button for PopUp")    
  EndIf 
  
  If Event = #PB_Event_CloseWindow 
    Quit = #True
  EndIf 
  
  
Until Quit = #True 
Axolotl
Addict
Addict
Posts: 802
Joined: Wed Dec 31, 2008 3:36 pm

Re: Having trouble with 'Right-Click on Systray icon' .

Post by Axolotl »

without running your code
You are using two WaitWindowEvent()
I am pretty sure that you loose your events in that construction....
Edit: breeze4me was faster :oops:
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Sergey
User
User
Posts: 53
Joined: Wed Jan 12, 2022 2:41 pm

Re: Having trouble with 'Right-Click on Systray icon' .

Post by Sergey »

You have WaitWindowEvent() twice. It's bad idea
I combined in one Select and now it's works and looks more beautiful :D

Code: Select all

;- Event Loop
Repeat
	Select WaitWindowEvent() 
	Case #PB_Event_Gadget
		Select EventGadget()
		Case ToTrayButton
			MessageRequester("Case ToTrayButton" , "ToTrayButton was Clicked !") 
			Hide_Window_Load_Image()  
		EndSelect       

	Case #PB_Event_SysTray ; <<-- PopUp when you right click on your systray icon
		If EventType() = #PB_EventType_RightClick 
			DisplayPopupMenu(0, WindowID(0)) 
		EndIf 
	
	Case #PB_Event_Menu ; <<-- PopUp Event 
		Select EventMenu() 
		Case 1 ; Restore 
			RemoveSysTrayIcon(1) 
			HideWindow(0, #False)
			MessageRequester("Info", "Press OK and then the Minimize button on the window", 0)
			PostEvent(#PB_Event_MinimizeWindow)
		Case 2 ; Quit 
			Quit = #True
		Case 3 ; Help
			RunProgram("Hibernate-Help.html") 
		EndSelect 
		; MessageRequester(" Select EventMenu()" , "Select EventMenu() case 1 , 2 , 3")
	
	Case #PB_Event_MinimizeWindow ;<<-- same With the Minimize button 
		HideWindow(0, #True)
		AddSysTrayIcon(1, WindowID(0), ImageID(0)) 
		SysTrayIconToolTip(1, "Right Mouse Button for PopUp")
	
	Case #PB_Event_CloseWindow
		KillTimer_(WindowID(#Window_0),1)
		Quit = #True
		
	EndSelect
Until Quit = #True 
Quin
Addict
Addict
Posts: 1125
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Having trouble with 'Right-Click on Systray icon' .

Post by Quin »

Yup, as others have said it appears to be because you're using WaitWindowEvent() twice. There are a couple gotchas with that procedure:
docs wrote: An application should always use this function instead of WindowEvent() if possible, as it doesn't takes an CPU time while waiting for an event.

The window event loop should not be processed in a thread, as there is some limitation on OS X and Linux. A debugger error will be raised.

WaitWindowEvent() can only be called once per event loop, because else events will be "lost" (every event can only be processed once and isn't available anymore for a second time after first processing).
vmars316
Enthusiast
Enthusiast
Posts: 474
Joined: Fri Jun 29, 2012 12:24 am
Contact:

Re: Having trouble with 'Right-Click on Systray icon' .

Post by vmars316 »

Ah.. Perfect ;
Great Response Folks !
Thank you...
vmars.us Win11 x64 , Martin Guitar 000-16 (1995)
"All things in moderation , except for love and forgiveness."
Post Reply