Tray icon only?

Windows specific forum
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Tray icon only?

Post 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...
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Tray icon only?

Post 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
Et cetera is my worst enemy
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Re: Tray icon only?

Post by highend »

Very cool.

Thanks a lot for the example code, chi!

Works like a charm...
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Tray icon only?

Post 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)
BERESHEIT
User avatar
chi
Addict
Addict
Posts: 1028
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Tray icon only?

Post by chi »

netmaestro wrote:SetParent_(WindowID(0), #HWND_MESSAGE)
Nice one, thanks
Et cetera is my worst enemy
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Re: Tray icon only?

Post by highend »

Nice one, thanks
From me as well :mrgreen:
Post Reply