Page 1 of 1

Tray icon only?

Posted: Tue Oct 31, 2017 3:29 pm
by highend
Is there a way to create a PureBasic app
that only uses a tray icon?

It shouldn't create / show any visible windows
and it would be nice if the tray icon could
provide a right click menu with toggle options
(option1, option2, etc. - on / off) and an "Exit"
entry that kills the whole app...

Re: Tray icon only?

Posted: Tue Oct 31, 2017 3:49 pm
by chi
It shouldn't create / show any visible windows
Why even make it visible? ;)

Code: Select all

If OpenWindow(0, 100, 150, 300, 100, "PureBasic - SysTray Example", #PB_Window_Invisible)
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    ; .ico format is available only on Windows
    IconName$ = #PB_Compiler_Home + "examples/sources/Data/CdPlayer.ico"
  CompilerElse
    IconName$ = #PB_Compiler_Home + "examples/sources/Data/Drive.bmp"
  CompilerEndIf
  
  AddSysTrayIcon(1, WindowID(0), LoadImage(0, IconName$))
  SysTrayIconToolTip(1, "Icon 1")
  
  If CreatePopupMenu(0)
    MenuItem(1, "Option 1")
    MenuItem(2, "Option 2")
    MenuItem(3, "Option 3")
    MenuBar()
    MenuItem(0, "Exit")
  EndIf
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
        
      Case #PB_Event_SysTray
        Select EventType()
          Case #PB_EventType_LeftClick, #PB_EventType_RightClick   
            DisplayPopupMenu(0, WindowID(0))
        EndSelect
        
      Case #PB_Event_Menu
        Select EventMenu()
            
          Case 0
            Event = #PB_Event_CloseWindow
            
          Case 1 To 3
            SetMenuItemState(0, EventMenu(), 1-GetMenuItemState(0, EventMenu()))
            
        EndSelect
    EndSelect
  Until Event = #PB_Event_CloseWindow
  
EndIf

Re: Tray icon only?

Posted: Tue Oct 31, 2017 5:33 pm
by highend
Very cool.

Thanks a lot for the example code, chi!

Works like a charm...

Re: Tray icon only?

Posted: Tue Oct 31, 2017 9:01 pm
by netmaestro
Best window to use for that is a message-only window. It is not visible, has no toolbar icon and is not enumerated. For most window purposes, it simply does not exist. But it will process messages and that's all you need. Make a message-only window like this:

Code: Select all

#HWND_MESSAGE = -3
OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)
SetParent_(WindowID(0), #HWND_MESSAGE)

Re: Tray icon only?

Posted: Wed Nov 01, 2017 3:05 am
by chi
netmaestro wrote:SetParent_(WindowID(0), #HWND_MESSAGE)
Nice one, thanks

Re: Tray icon only?

Posted: Wed Nov 01, 2017 12:57 pm
by highend
Nice one, thanks
From me as well :mrgreen: