Page 1 of 1

AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Thu Mar 07, 2024 8:44 pm
by vmars316
TIA ,
I am looking for 'how to add 'Hide_program_from_Taskbar'
See Example code below :
And I am wondering how/why WindowID(0) and ImageID(0) have the same ObjectID , both zero .

Code: Select all

; Author: PWS32 (updated for PB4.00 by blbltheworm)

If OpenWindow(0, 100, 200, 195, 260, "Test Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 

  If CreatePopupMenu(0) 
    MenuItem(1, "Restore Window") 
    MenuBar() 
    MenuItem(2, "Quit") 
    MenuBar() 
    MenuItem(3, "Help") 
  EndIf  
  
  MessageRequester("Info", "Click to Move Program to SYSTRAY", 0) 
  HideWindow(0, 1) 
  LoadImage(0, "HT.ico")
  AddSysTrayIcon (1, WindowID(0), ImageID(0)) ; <<--- Take any 16X16 icon here 
  SysTrayIconToolTip (1, "Right-click for OPTIONS") 

  Repeat 

    EventID.l = WaitWindowEvent() 

    If EventID = #PB_Event_SysTray ; <<-- PopUp when you right click on your systray icon
      If EventType() = #PB_EventType_RightClick 
        DisplayPopupMenu(0, WindowID(0)) 
      EndIf 
    EndIf 

    If EventID = #PB_Event_Menu ; <<-- PopUp Event 
      Select EventMenu() 
        Case 1 ; Restore 
           RemoveSysTrayIcon (1) 
           HideWindow(0, 0) 
           MessageRequester("Info", "Press OK and then the Minimize button on the window", 0) 
        Case 2 ; Quit 
           Quit = 1 
        Case 3 ; Help
           RunProgram("Hibernate-Help.html") 
      EndSelect 
    EndIf 
    
    If IsIconic_(WindowID(0)) ;<<-- same With the Minimize button 
      HideWindow(0, 1) 
      AddSysTrayIcon (1, WindowID(0), LoadImage(0, "HT.ico")) 
      SysTrayIconToolTip (1, "Right Mouse Button for PopUp")    
    EndIf 

    If EventID = #PB_Event_CloseWindow 
      Quit = 1 
    EndIf 

  Until Quit = 1 
  
EndIf


Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Thu Mar 07, 2024 9:05 pm
by infratec
This works:

Code: Select all

EnableExplicit

; Author: PWS32 (updated for PB4.00 by blbltheworm)

Define Event.i, Quit.i


If OpenWindow(0, 100, 200, 195, 260, "Test Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget) 

  If CreatePopupMenu(0) 
    MenuItem(1, "Restore Window") 
    MenuBar() 
    MenuItem(2, "Quit") 
;     MenuBar() 
;     MenuItem(3, "Help") 
  EndIf  
  
  MessageRequester("Info", "Click to Move Program to SYSTRAY") 
  HideWindow(0, #True) 
  LoadImage(0, #PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico")
  
  Debug WindowID(0)
  Debug ImageID(0)
  
  AddSysTrayIcon(1, WindowID(0), ImageID(0)) ; <<--- Take any 16X16 icon here 
  SysTrayIconToolTip (1, "Right-click for OPTIONS") 

  Repeat 

    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 
    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
  
EndIf
When do you see the 0 values?
There is no Debug in your code.

An Id can never be 0.
Or do you mean the PB numbers?

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Thu Mar 07, 2024 9:06 pm
by mk-soft
Each object type has its own object array.
See PB help Purebasic objects.

https://www.purebasic.com/documentation ... jects.html

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Thu Mar 07, 2024 11:47 pm
by AZJIO
mk-soft wrote: Thu Mar 07, 2024 9:06 pm See PB help Purebasic objects.
I read and received an answer that sometimes worried me
Both methods (indexed and dynamic) can be used together at the same time without any conflict.

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Fri Mar 08, 2024 12:18 am
by Demivec
AZJIO wrote: Thu Mar 07, 2024 11:47 pm
mk-soft wrote: Thu Mar 07, 2024 9:06 pm See PB help Purebasic objects.
I read and received an answer that sometimes worried me
Both methods (indexed and dynamic) can be used together at the same time without any conflict.
Did you resolve your worries or do you still have concerns when mixing indexed and dynamic object numbering?

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Fri Mar 08, 2024 12:27 am
by AZJIO
Demivec wrote: Fri Mar 08, 2024 12:18 am Did you resolve your worries or do you still have concerns when mixing indexed and dynamic object numbering?
Finally, I became 100% sure of this. At first I thought that this is one array through which a number is searched instead of direct access by index, because creating a large array with index access is not efficient. The numbers returned by #PB_Any are too large to create an array of that size. Now I think we have two different lists, so they can't overlap.

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Fri Mar 08, 2024 9:07 am
by mk-soft
With #PB_Any it is a memory address. #PB_Any is also somewhat slower than via array, as it has to be searched for when checking.

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Fri Mar 08, 2024 9:12 am
by Fred
mk-soft wrote: Fri Mar 08, 2024 9:07 am With #PB_Any it is a memory address. #PB_Any is also somewhat slower than via array, as it has to be searched for when checking.
To be more precise, #PB_Any is only slower if you use the Is() functions ( IsWindow(), IsImage() etc.), it's the same speed for all other operations

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Fri Mar 08, 2024 5:18 pm
by vmars316
infratech ,
Yes ,
Or do you mean the PB numbers?

in

Code: Select all

AddSysTrayIcon (1,    WindowID(0),     LoadImage(0, "Examples\Sources\Data\CdPlayer.ico")) 
Aren't these PB numbers ?

Just as the PB numbers for HideWindow and LoadImage below ?

Code: Select all

  HideWindow(0, #True) 
  LoadImage(0, #PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico")
Thanks

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Sun Mar 10, 2024 10:20 pm
by vmars316
Sorry ,
I'm just mixed up about what's the diff between
AddSysTrayIcon (1, WindowID(0), LoadImage(0, "Examples\Sources\Data\CdPlayer.ico"))
and HideWindow(0, #True)
LoadImage(0, #PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico")

Are they all PB_Any objects ?
Thanks

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Mon Mar 11, 2024 6:29 am
by DarkDragon
Initially there was no #PB_Any, back then everyone had to rely on constant numbers. However you sometimes want to dynamically create those objects inside procedures or inside loops without having to check whether the outside system uses the number already. That's when #PB_Any was introduced.

Code: Select all

OpenWindow(0, ...)
Handle = WindowID(0)
Here 0 is the number and you get the OS's native window handle by calling WindowID.

#PB_Any makes the Open/Load/Create functions return a dynamic "Number".

Code: Select all

Number = OpenWindow(#PB_Any, ...)
Handle = WindowID(Number)
The WindowID/GadgetID/... functions used to return the native OS handles. Functions which require IDs can often be used for non PB objects, too. However that's not always the case.

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Mon Mar 11, 2024 11:09 am
by mk-soft
To get the window os handle from the image, this has always worked.
However, it is not included in the documentation and is therefore unofficial.

Code: Select all

hImage = LoadImage(0, #PB_Compiler_Home + "Examples\Sources\Data\CdPlayer.ico")
Debug "Window handle of Image unofficial: " + hImage
Debug "Window handle of Image official: " + ImageID(0)

Re: AddSysTrayIcon (1, WindowID(0), ImageID(0)) ?

Posted: Mon Mar 18, 2024 5:11 pm
by vmars316
In the context of your PureBasic code, when you use `0` as an argument for `OpenWindow`, `CreatePopupMenu`, `LoadImage`, and similarly for other functions, it specifies the identifier (ID) for the window, popup menu, or image. The use of `0` as an ID is a common practice for simplicity, especially in smaller programs or examples where there is only one object of each type, thus avoiding the need for maintaining a separate, unique identifier for each.

The reason `WindowID(0)` and `ImageID(0)` can both have the ID `0` and not conflict with each other is due to the way PureBasic manages these IDs internally. PureBasic segregates the namespaces for different types of objects, such as windows, images, and menus. This means that having a window and an image both with an ID of `0` is perfectly acceptable because these IDs are managed in separate contexts.

Here's a breakdown of how this works:

- `OpenWindow(0, ...)`: This opens a window with an ID of `0`. In the context of window management, `0` uniquely identifies this window.
- `LoadImage(0, "HT.ico")`: This loads an image with an ID of `0`. In the context of image management, `0` uniquely identifies this image.

Despite both the window and the image having an ID of `0`, there is no conflict because these IDs are used within their respective domains or namespaces (one for windows, one for images). This separation allows PureBasic to understand exactly what you're referring to when you use `WindowID(0)` or `ImageID(0)`, based on the function being called and the context in which the ID is used.

This design makes it easier to work with IDs in PureBasic, especially for simple applications where you might not need multiple windows or dozens of images, and thus, you can use simple, sequential IDs or even just `0` without worrying about conflicts
Ok , I get it now...