Tray icon without creating a window

Just starting out? Need help? Post your questions and find answers here.
simkot
User
User
Posts: 31
Joined: Sat Oct 26, 2024 8:25 am

Tray icon without creating a window

Post by simkot »

While the script is running, it is necessary that the icon is displayed in the tray. The program works without creating a window. How can this be done?
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Tray icon without creating a window

Post by PBJim »

You can do it through an invisible window, like this :

Code: Select all

; **
; ** Display status icon
; **
Procedure StatusIcon()
  
  If OpenWindow(0, 0, 0, 300, 100, "", #PB_Window_Invisible)
    AddSysTrayIcon(0, WindowID(0), LoadImage(0, "x:\..\status-icon.png"))
  EndIf
  
EndProcedure
To remove the icon :

Code: Select all

RemoveSysTrayIcon(0)                                          ; Remove system tray icon
simkot
User
User
Posts: 31
Joined: Sat Oct 26, 2024 8:25 am

Re: Tray icon without creating a window

Post by simkot »

Thank you! And how to make the icon file is inside the exe file?
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Tray icon without creating a window

Post by ChrisR »

If an icon is used in the compiler options, you can retrieve the exe icon with this (windows only) :

Code: Select all

Procedure AddSysTray()
  ; Load Icon directly from the executable resource 
  If Not IsSysTrayIcon(1)
    hinstance = GetClassLong_(WindowID(0), #GCL_HMODULE)
    iconhWnd = LoadIcon_(hinstance, 1)
    If Not iconhWnd  ; Just in case it is compiled without icons, Load image File.bmp here
      iconhWnd = ImageID(LoadImage(#PB_Any, #PB_Compiler_Home + "examples/sources/Data/File.bmp"))
    EndIf
    AddSysTrayIcon(1, WindowID(0), iconhWnd)
    SysTrayIconToolTip(1, "Open Notepad")
  EndIf
  
  If CreatePopupMenu(0)
    MenuItem(1, "Notepad")
    MenuBar()
    MenuItem(0, "Exit")
  EndIf
EndProcedure

If OpenWindow(0, -10, -10, 0, 0, "", #PB_Window_Invisible)
  AddSysTray()
  
  Repeat
    Event = WaitWindowEvent()
    Select Event
         
      Case #PB_Event_Menu
        Select EventMenu()
          Case 0
            Event = #PB_Event_CloseWindow
          Case 1
            RunProgram("Notepad.exe")
        EndSelect
        
      Case #PB_Event_SysTray
        Select EventType()
          Case #PB_EventType_RightClick   
            DisplayPopupMenu(0, WindowID(0))
          Case #PB_EventType_LeftClick
            RunProgram("Notepad.exe")
        EndSelect

    EndSelect
  Until Event = #PB_Event_CloseWindow
EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Tray icon without creating a window

Post by RASHAD »

Hi
System Tray Icon - No Window
Escape to END
You can use the hardcoded icons
Or
Your own image

Code: Select all

;System Tray icon Without Window
;Escap to end

;1- :    
;   CatchImage(0,?View)
;   AddSysTrayIcon(0,GetDesktopWindow_(),ImageID(0))
;   SysTrayIconToolTip(0,"Window Information")

hIcon = LoadIcon_(0,#IDI_WINLOGO	)
AddSysTrayIcon(0,GetDesktopWindow_(),hIcon)
SysTrayIconToolTip(0,"Window Information")

Repeat
  Delay (1)
  
  If GetAsyncKeyState_(#VK_ESCAPE) & $8000 = 32768   
    End
  EndIf 
  
ForEver

DataSection
  View:
  Data.q $1010000200010000,$0568000800010000,$1010000000260000,$0468002000010000,$00280000058E0000,$0020000000100000,$0000000800010000,$0000000001400000
  Data.q $0100000000000000,$3668000000000000,$F3FB004040750039,$F0F70000000000E4,$00000000000000D4,$F6FB000000000000,$FCFF0000000000E6,$E2F400ECFAFC00F5
  Data.q $671000DEF3FA00C4,$610E00FFF7B200C0,$9BCA009E9EBD00BA,$0000005B58890084,$D2EB005B578A0000,$00000000000000B4,$F8FB000000000000,$00000000000000EE
  Data.q $0000000000000000,$CEED00E6F5FB0000,$00000000000000AE,$0000000000000000,$000000C0E4FA0000,$DEF2000000000000,$00000000000000C3,$741100AEAFC70000
  Data.q $00000000000000D0,$D4EC000000000000,$000000ECFFFF00B3,$0000000000000000,$0000008B43170000,$0000000000000000,$000000B6ADC40000,$0000000000000000
  Data.q $0000000000000000,$000000BCDBF10000,$C4E600D9E1EA0000,$00000000000000A2,$F1F700ABCEEB0000,$000000EFFCFD00E5,$0000000000000000,$000000EC8E150000
  Data.q $5010000000000000,$00000000000000A6,$0000000000000000,$F6FA00E4F5FB0000,$DFF100C0DDF200E5,$00000000000000C2,$0000000000000000,$FFFF00CCE9F50000
  Data.q $00000000000000E3,$C5EC000000000000,$000000000000009D,$0000000000000000,$C2CD00BFDDF00000,$401000C1B9C700C8,$000000BBD9F0008A,$0000000000000000
  Data.q $5015000000000000,$00000000000000A1,$FEFE00C2E1F50000,$00000000000000F8,$E2F200E1F3FA0000,$00000000000000C4,$0000000000000000,$0000004F45720000
  Data.q $D4ED00C4E0F30000,$5F120000000000B6,$511D0000000000B6,$76B900000000009C,$E9B100FFFEFE006C,$00000000000000FE,$F4CD00FDF4C60000,$00000000000000FF
  Data.q $000000A9C0E00000,$881F000000000000,$FFFF0000000000DF,$A4C000736E9200F4,$F8FC009CACC70097,$F0F90000000000ED,$000000EAFFFF00DD,$0000000000000000
  Data.q $0000000000000000,$7B09000000000000,$8FC50000000000E5,$000000000000007E,$000000FFC7580000,$000000ADD2F10000,$91B9000000000000,$0000000000000081
  Data.q $0000000000000000,$0000000000000000,$B1C600E3F5FB0000,$000000B9B3C500B1,$0000000000000000,$000000FFF7DB0000,$FFFF000000000000,$000000DCFAFF00DE
  Data.q $0000000000000000,$3E6F00A6CAEC0000,$C0E900F2FFFF0043,$EDF800000000009B,$000000CCE9FA00D3,$0000000000000000,$749F000000000000,$0000000000000077
  Data.q $C0CE000000000000,$000000625E8D00C4,$0000000000000000,$0000005F62970000,$F2F9000000000000,$00000000000000D9,$F2FA000000000000,$0000006C608800DA
  Data.q $C7D100EDFBFC0000,$D4EF0000000000CD,$CDEC0000000000B6,$00000000000000AD,$000000C3E3F40000,$FFFE000000000000,$DBF000B2D1EE00F8,$00000000000000BE
  Data.q $0000000000000000,$E3F3000000000000,$00000000000000C5,$E8F6000000000000,$000000AACCEE00CC,$0000000000000000,$0000009549160000,$0000000000000000
  Data.q $FFFF00C4E1F30000,$680300FFFFFF00FF,$0303030303030335,$0F82030303030303,$030303030303F9A0,$A54E030303030303,$03030303036EA086,$1003030303030303
  Data.q $0303030380A08AA5,$0303030303030303,$0303030E4BB7A510,$0303030303030303,$94C0D3A089A52C03,$030303030301AA95,$C14615A5A5900303,$0303030300BA5C99
  Data.q $0BE8FECF84030303,$030303C2C5280402,$0B728541CA030303,$03037B25287D0402,$531A969211030303,$03037BE016C4981F,$B10808312B030303,$030313E97645550A
  Data.q $75DAD6BBB2030303,$0303DB200C47FEE5,$0D5BF4F039030303,$030393F56554DD30,$42FD71CE03030303,$030303A2603F56EA,$69A78D0303030303,$0303030312BFE27E
  Data.q $DE8D030303030303,$FF9F030303B36766,$FF070000FF0F0000,$FFC10000FF830000,$03F0000007E00000,$00F8000001F80000,$00F8000000F80000,$00F8000000F80000
  Data.q $03FE000001FC0000,$0028000007FF0000,$0020000000100000,$0000002000010000,$0000000004400000,$0000000000000000,$6AAE000000000000,$2703F676330D5F5D
  Data.q $000000000000B578,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$623A000000000000,$8A14FFFFD470FF9D
  Data.q $0000CE792802FFFD,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$5900000000000000,$FFCFFFFFF1A1B8B7
  Data.q $3C02FFF69117FFFF,$000000000000C997,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$E27EC9B753000000
  Data.q $A12BFFFFFFE0FFFF,$0000CDAC4B00FFF6,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$0000000000000000,$6100000000000000
  Data.q $FFE9FFFFD965CDC6,$6B00FFF7B34BFFFF,$000000000000C9E4,$225208302D590000,$00001026234E1A27,$0000000000000000,$0000000000000000,$0000000000000000
  Data.q $D558C9D56D000000,$8B11FFFFFBBCFFFF,$2F676E625775FFF0,$85A6E16A6E94A42D,$2956E8636F99F378,$000030231E47BF2B,$0000000000000000,$0000000000000000
  Data.q $8200000000000000,$B8C7FFFFA519D0E9,$FAFAFF6E74ABFFBC,$FFFFFFFFFFFFFFF6,$F8FFFFEBFFFFFFF3,$1E48FF5C6998FFD2,$0000000000005F23,$0000000000000000
  Data.q $0000000000000000,$8AC964947A700000,$FFFFFFFFFFFDFF7D,$F2FAFFECFAFDFFFF,$E8F8FFD3F0F6FFE1,$6999FFD9FFFFFFCB,$0000262D274EFF59,$0000000000000000
  Data.q $0000000000000000,$EFF56F5559960000,$FCFEFFFFFFFFFFE8,$F2FBFFEEF9FCFFF5,$D5EDFFD7F3F9FFDF,$D5F5FFBCDCF4FFB7,$0000AE3E406FFFAE,$0000000000000000
  Data.q $0000000000000000,$FFFFCB8380A50000,$F9FCFFF2FAFDFFFC,$F8FDFFE5F4FBFFEF,$E5F5FFDDF3FAFFE9,$E4FFFFB6D4ECFFC9,$0000F8616895FFBC,$0000000000000000
  Data.q $0000000000000000,$FFFFCA9D9EBA0000,$F5FAFFE5F4FBFFE9,$F4FCFFE9F7FDFFE2,$D9F1FFC9E3F4FFE2,$E1FFFFC0DDEFFFBF,$0000F56C75A2FFB9,$0000000000000000
  Data.q $0000000000000000,$FEFFC29B9CB60000,$F3FAFFDBF2FAFFDA,$E3F4FFE0F5FBFFD9,$FFFFFFFBFDFEFFC6,$D5FDFFCFEBF5FFFB,$0000FC6C729EFFAB,$0000000000000000
  Data.q $0000000000000000,$DAEEA1A499B40000,$DEF2FFCEEFFCFFBB,$E1F3FFD0EAF6FFC0,$F8FAFFEDFDFDFFC6,$B8E8FFC8E8F7FFE3,$00009E696894FF92,$0000000000000000
  Data.q $0000000000000000,$AEC90DB3ACBF0000,$D9F1FFB3D8F6FFAA,$E2F4FFACCCE8FFB7,$E3F7FFCAE9F4FFC6,$86B9FF9FCCF3FFC3,$000037706484FF77,$0000000000000000
  Data.q $0000000000000000,$BAC7000000000000,$C4E7FFA7B1CB38C1,$C5EEFF98C4EDFF9F,$AFE1FF90BDEBFF9A,$7291FF798FBFFF84,$0000000000007483,$0000000000000000
  Data.q $0000000000000000,$0000000000000000,$B4C620C3BDC70000,$ADC8F5A4B0CB8FB6,$96B5F89AA3C3F3A5,$0000439B8FA4A896,$FF1F000000000000,$FF070000FF0F0000
  Data.q $8FC10000FF830000,$01F0000003E00000,$00F8000000F80000,$00F8000000F80000,$00F8000000F80000,$01FC000000F80000,$000003FE0000
  Viewend:
EndDataSection
Egypt my love
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Tray icon without creating a window

Post by infratec »

simkot wrote: Sun Oct 27, 2024 7:27 pm Thank you! And how to make the icon file is inside the exe file?

Code: Select all

CatchImage(0, ?IconStart, ?IconEnd - ?IconStart)

DataSection
  IconStart:
  IncludeBinary #PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico"
  IconEnd:
EndDataSection
mrbungle
Enthusiast
Enthusiast
Posts: 164
Joined: Wed Dec 30, 2020 3:18 am

Re: Tray icon without creating a window

Post by mrbungle »

You can also create an invisible, 0 width, 0 height window to minimize the window requirement
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Tray icon without creating a window

Post by jacdelad »

A window with width 0 and height 0 is still shown, tested with PB 6.12 x64 ASM on Windows 11 24H2.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Tray icon without creating a window

Post by Quin »

jacdelad wrote: Mon Oct 28, 2024 1:24 pm A window with width 0 and height 0 is still shown, tested with PB 6.12 x64 ASM on Windows 11 24H2.
Even with #PB_Window_Invisible?
User avatar
jacdelad
Addict
Addict
Posts: 2031
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Tray icon without creating a window

Post by jacdelad »

No, of course not. It sounded to me that it should be enough to give the window size 0 to make it invisible. My fault.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
AZJIO
Addict
Addict
Posts: 2225
Joined: Sun May 14, 2017 1:48 am

Re: Tray icon without creating a window

Post by AZJIO »

Code: Select all

AddSysTrayIcon(0, WindowID(0), GetClassLongPtr_(WindowID(0), #GCL_HICON))
specify the icon in the compiler settings

It looks like we need to go through all the stages of standard things again. Just download my archive of programs and there you will find answers to all your questions. There is also an archive of examples in Russian for WinAPI, I gave a link, there is more than you need.
Use the search on this forum.
User avatar
moulder61
Enthusiast
Enthusiast
Posts: 205
Joined: Sun Sep 19, 2021 6:16 pm
Location: U.K.

Re: Tray icon without creating a window

Post by moulder61 »

jacdelad wrote: Mon Oct 28, 2024 1:59 pm No, of course not. It sounded to me that it should be enough to give the window size 0 to make it invisible. My fault.
I had the same issue. I got around it by hiding the title bar too with PB_Window_BorderLess.

I'm guessing making it invisible is a cleaner method?

Might as well hide it from the task bar too while you're at it with #PB_Window_Tool. :wink:

Don't try this at home. I'm a professional. :P

Moulder.
"If it ain't broke, fix it until it is!

This message is brought to you thanks to SenselessComments.com

My PB stuff for Linux: "https://u.pcloud.link/publink/show?code ... z3MR0T3jyV
Post Reply