Page 1 of 1

[Done] Incorrect window number for tray menu events.

Posted: Sun Sep 28, 2025 11:08 pm
by User_Russian

Code: Select all

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
ForEver
Events from window number 0, but there is no such window in the application!

Code: Select all

Procedure Event_Menu()
  Select Event()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
EndProcedure

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

BindEvent(#PB_Event_Menu, @Event_Menu(), 10)

Repeat
  Select WaitWindowEvent()
;      Case #PB_Event_Menu
;        Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
;        Select EventMenu()
;          Case 1 ; Exit 
;            RemoveSysTrayIcon(0)
;            End
;        EndSelect
  EndSelect
ForEver
There are no events in this code at all.
But if in BindEvent() replace the window number with 0 then events appear.

Code: Select all

Procedure Event_Menu()
  Select Event()
    Case #PB_Event_Menu
      Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
      Select EventMenu()
        Case 1 ; Exit 
          RemoveSysTrayIcon(0)
          End
      EndSelect
  EndSelect
EndProcedure

; Invisible window to just have the systray
OpenWindow(10, 0, 0, 10, 10, "", #PB_Window_Invisible)

UsePNGImageDecoder()
AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))

; Create a pop-up menu to be displayed by the systray with a systray look
CreatePopupMenu(0)
MenuItem(0, "About PureBasic...")
MenuBar()
MenuItem(1, "Exit")

; Associate the menu to the systray
SysTrayIconMenu(0, MenuID(0))

BindEvent(#PB_Event_Menu, @Event_Menu(), 0)

Repeat
  Select WaitWindowEvent()
;      Case #PB_Event_Menu
;        Debug "Menu item "+EventMenu()+"  Window "+EventWindow()
;        Select EventMenu()
;          Case 1 ; Exit 
;            RemoveSysTrayIcon(0)
;            End
;        EndSelect
  EndSelect
ForEver
This is incorrect because there is no window number 0 in this code!

Re: Incorrect window number for tray menu events.

Posted: Mon Sep 29, 2025 1:24 am
by Demivec
User_Russian wrote: Sun Sep 28, 2025 11:08 pm Events from window number 0, but there is no such window in the application!
Welcome to the non-null world of PureBasic.

EventWindow() does not have a return value indicating Null or that its value is not applicable to an event. What do you think happens when you Debug EventMenu() for a gadget event or EventGadget() for a menu event? This is also a good example of why it is wise not to use an object ID of zero so that zero can be considered an invalid or Null value indicator (which your first example demonstrates in a round about way).

What value do you want it to return?

I'm inexperienced with pop-up menus, if a menu is already associated with a window, (for display or otherwise) do you really need the window number to process the event?

Re: Incorrect window number for tray menu events.

Posted: Mon Sep 29, 2025 10:41 am
by User_Russian
Demivec wrote: Mon Sep 29, 2025 1:24 amWhat value do you want it to return?
When creating the tray icon, the window with ID 10 is specified.

Code: Select all

AddSysTrayIcon(0, WindowID(10), LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/world.png"))
On Windows, this code works correctly and the function EventWindow() returns the number 10. But on MacOS the function returns 0.
Demivec wrote: Mon Sep 29, 2025 1:24 amdo you really need the window number to process the event?
The window number is specified in function BindEvent() and it is 10, not 0, which is why there are no events on MacOS and there are events on Windows.

Re: Incorrect window number for tray menu events.

Posted: Mon Sep 29, 2025 7:54 pm
by Piero
I would want to celebrate with Vodka the Russian contributions to this Forum, but I'm more inclined to gallons of German Beer :mrgreen:

Re: Incorrect window number for tray menu events.

Posted: Mon Sep 29, 2025 8:36 pm
by mk-soft
BindMenuEvent ...

Re: [Done] Incorrect window number for tray menu events.

Posted: Fri Oct 10, 2025 2:06 pm
by Fred
Fixed.