How to remove systray icon?

Just starting out? Need help? Post your questions and find answers here.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to remove systray icon?

Post by srod »

Then how do you normally close this program?
I may look like a mule, but I'm not a complete ass.
whertz
Enthusiast
Enthusiast
Posts: 124
Joined: Sat Jun 25, 2005 2:16 pm
Location: United Kingdom

Re: How to remove systray icon?

Post by whertz »

srod wrote:Then how do you normally close this program?
You have to right click on the system tray icon and select exit.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: How to remove systray icon?

Post by RASHAD »

srod I think it is not that easy
I only succeeded to delete one icon ( the network )
Egypt my love
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to remove systray icon?

Post by srod »

Rashad, that code does not retrieve all taskbar icons. I recall reading that this method does/will not retrieve all icons so it is probably something we just have to accept.
srod I think it is not that easy
I only succeeded to delete one icon ( the network )
Doesn't surprise me really. I think in Whertz's case, given that he is killing the program, deleting the systray icon will probably succeed. (By probably I mean that I haven't got a clue really! :wink: )
I may look like a mule, but I'm not a complete ass.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to remove systray icon?

Post by skywalk »

Thanks srod and rashad!
Learned a lot from this thread.
Whertz, didn't realize you wanted to remove systray icons from programs you didn't start.
That is not in my plans.
I only want to control what I create and initiate and try not to do too much damage. :)
Last edited by skywalk on Mon Jul 07, 2014 11:29 pm, edited 1 time in total.
whertz
Enthusiast
Enthusiast
Posts: 124
Joined: Sat Jun 25, 2005 2:16 pm
Location: United Kingdom

Re: How to remove systray icon?

Post by whertz »

skywalk wrote:Thanks srod and rashad!
Learned a lot from this thread.
Whertz, didn't realize you wanted to remove systray icons from programs you didn't start.
That is not in my plans.
I only want to control what I create and initiate and try not to do too much damage. :)

-Steve
I do start the program with runprogram, but when I run it with your code example, it makes an extra system tray icon the same as the program, closes the program and removes the system tray icon that was created. The original system tray icon remains.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How to remove systray icon?

Post by skywalk »

Thanks Whertz, now I really understand the issue... :)
I wasn't testing that case, since "Notepad.exe" doesn't open a systray icon. Doh!
Better test would have been to create a simple exe that opens a systray icon and waits to be closed.
Then attempt to close it with the code in this thread.
Last edited by skywalk on Mon Jul 07, 2014 11:29 pm, edited 1 time in total.
whertz
Enthusiast
Enthusiast
Posts: 124
Joined: Sat Jun 25, 2005 2:16 pm
Location: United Kingdom

Re: How to remove systray icon?

Post by whertz »

skywalk wrote:Thanks Whertz, now I really understand the issue... :)
I wasn't testing that case, since "Notepad.exe" doesn't open a systray icon. Doh!
Better test would have been to create a simple exe that opens a systray icon and waits to be closed.
Then attempt to close it with the code in this thread.

-Steve
Here's an example: Compile this as trayicon.exe:

Code: Select all

OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)
AddSysTrayIcon(1, WindowID(0), ExtractIcon_(0,"notepad.exe",0)) 
Repeat
  WaitWindowEvent()
ForEver
There is no way of ending the program unless you kill it...

Now run it with this:

Code: Select all

processID.l = RunProgram("trayicon.exe","","",#PB_Program_Open) 

Delay(10000)

KillProgram(processID)
CloseProgram(processID) 
I'm sure one of you windows api geniuses out there will figure out a way of doing it! :wink:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: How to remove systray icon?

Post by srod »

Okay, I have tested the following with my MSN/ICQ... chat client (Trillian) which is a program which has to be closed by right-clicking the tray icon and selecting EXIT etc.

When I use RunProgram() to start the program and then KillProgram() to terminate it, the icon is left in the tray, so exactly the same situation as yours Whertz. The function RemoveTrayIcon() (in the code below), used after killing the program, manages to remove the icon without a problem.

@Whertz : note the steps I have taken in my test code to secure the hWnd of the main Trillian window. I used Rashad's Enum_TrayIcons() routine to first check that the owner hWnd corresponding to the tray icon placed there by Trillian is the same one returned by FindWindow_(). I cannot stress this enough. If the code fails for you then it will most likely be because you have not identified the correct handle (hWnd). (I actually used EnumWindows_() first and then compared all hWnd values with that returned by Rashad's code in order to obtain the correct class name. I then use this classname with the FindWindow() call).

The code is based upon Rashad's code based upon Netmaestro's original routine. :)

Code: Select all

Structure TRAYDATA
  hwnd.l                
  uID.l             
  uCallbackMessage.i    
  Reserved.l[2]      
  hIcon.l                
EndStructure

Structure _TBBUTTON
  iBitmap.l 
  idCommand.l
  fsState.b 
  fsStyle.b 
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  bReserved.b[6]
CompilerElse
  bReserved.b[2]
CompilerEndIf
  dwData.i 
  iString.i 
EndStructure


;Returns #True if successful.
Procedure.i RemoveTrayIcon(hWndOwner)
  Protected result, hWnd, hTray, count, dwExplorerThreadId, i, hProc, *lpData, button._TBBUTTON, button_td.TRAYDATA
  Protected nid.NOTIFYICONDATA
  hWnd = FindWindow_("Shell_TrayWnd", #Null)
  If hWnd
    hWnd = FindWindowEx_(hWnd, #Null, "TrayNotifyWnd", #Null)
    If hWnd
      hWnd = FindWindowEx_(hWnd,#Null, "SysPager", #Null)
      If hWnd 
        hTray = FindWindowEx_(hWnd, #Null, "ToolbarWindow32", #Null)
        If hTray
          dwExplorerThreadId=GetWindowThreadProcessId_(hTray, @dwExplorerProcessId)
          If dwExplorerProcessId
            hProc = OpenProcess_(#PROCESS_ALL_ACCESS, #False, dwExplorerProcessId)
            If hProc
              *lpData = VirtualAllocEx_(hProc, #Null, SizeOf(_TBBUTTON), #MEM_COMMIT, #PAGE_READWRITE)
              If *lpData
                count = SendMessage_(hTray, #TB_BUTTONCOUNT, 0, 0)
                For i = 0 To count-1
                  SendMessage_(hTray, #TB_GETBUTTON, i, *lpData)
                  ReadProcessMemory_(hProc, *lpData, @button, SizeOf(_TBBUTTON), #Null)    
                  ReadProcessMemory_(hProc, button\dwData, @button_td, SizeOf(TRAYDATA), #Null)
                  If button_td\hwnd = hWndOwner
                    With nid
                      \cbSize = SizeOf(NOTIFYICONDATA)
                      \hWnd = hWndOwner
                      \uID = button_td\uID
                      \uFlags = #NIF_ICON
                      \hIcon = button_td\hIcon
                    EndWith
                    result = Shell_NotifyIcon_(#NIM_DELETE, nid)
                    Break
                  EndIf
                Next
                VirtualFreeEx_(hProc, *lpData, #Null, #MEM_RELEASE)
              EndIf      
              CloseHandle_(hProc)
            EndIf
          EndIf        
        EndIf
      EndIf
    EndIf
  EndIf
  ProcedureReturn result  
EndProcedure


;Test.

  processID = RunProgram("C:\Program Files\Trillian\trillian.exe", "", "", #PB_Program_Open)  

  MessageRequester("", "Click OK when Trillian has loaded!")

  hWnd = FindWindow_("trillian", 0)
  KillProgram(processID)

  If RemoveTrayIcon(hWnd)
    MessageRequester("Heyho!", "RemoveTrayIcon() reports success!")
  Else
    MessageRequester("Heyho!", "RemoveTrayIcon() reports failure!")
  EndIf
I may look like a mule, but I'm not a complete ass.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: How to remove systray icon?

Post by RASHAD »

Hi everybody
Hi whertz
You did not inform us after srod last code

Ok here is some code may it help you somehow
Tested with Xp x86 Win 7 x64

Code: Select all

  ; Show or Hide System Tray Icons or System Tray Clock Programmatic (Windows)
  ; RASHAD 18/02/2010

    h1 = FindWindow_("Shell_TrayWnd",0)   
    h2 = FindWindowEx_(h1,0,"TrayNotifyWnd",0)
    h3 = FindWindowEx_(h2,0,"SysPager",0)  
    h4 = FindWindowEx_(h3,0,"ToolbarWindow32",0)
    h5 = FindWindowEx_(h2,0,"TrayClockWClass",0)
    
;******************(  Hide System Tray Icons  )**************************
    
    ShowWindow_(h3,#SW_HIDE)
    SHChangeNotify_(134217728,0,0, 0)
    
;******************(  Show System Tray Icons  )**************************
    
    ;ShowWindow_(h3,#SW_SHOW)
    ;SHChangeNotify_(134217728,0,0, 0)    
    
;*********************(  Hide System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_HIDE)
    ;SHChangeNotify_(134217728,0,0, 0)
    
;*********************(  Show System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_SHOW)
    ;SHChangeNotify_(134217728,0,0, 0)

Have fun
Egypt my love
User avatar
Spaceman Spiff
User
User
Posts: 11
Joined: Mon Feb 15, 2010 9:13 pm

Re: How to remove systray icon?

Post by Spaceman Spiff »

I played with this rashad! I uncommented all the ; stuff... HEELP... all my windows has disappeared! How do I get it back! This is mean code! :shock:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: How to remove systray icon?

Post by RASHAD »

@Spaceman Spiff Hi
Uncomment only what you want to do per once
So uncomment only Show System tray icons and run the code once
Then comment Show system Tray icons again and uncomment Show System clock and run the code again

Uncomment only what you want to do and run the code once then comment it again

this one will hide system tray icons:

Code: Select all


; Show or Hide System Tray Icons or System Tray Clock Programmatic (Windows)
; RASHAD 18/02/2010

    h1 = FindWindow_("Shell_TrayWnd",0)   
    h2 = FindWindowEx_(h1,0,"TrayNotifyWnd",0)
    h3 = FindWindowEx_(h2,0,"SysPager",0)  
    h4 = FindWindowEx_(h3,0,"ToolbarWindow32",0)
    h5 = FindWindowEx_(h2,0,"TrayClockWClass",0)
    
;******************(  Hide System Tray Icons  )**************************
    
    ShowWindow_(h3,#SW_HIDE)
    SHChangeNotify_(134217728,0,0, 0)
    
;******************(  Show System Tray Icons  )**************************
    
    ;ShowWindow_(h3,#SW_SHOW)
    ;SHChangeNotify_(134217728,0,0, 0)    
    
;*********************(  Hide System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_HIDE)
    ;SHChangeNotify_(134217728,0,0, 0)
    
;*********************(  Show System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_SHOW)
    ;SHChangeNotify_(134217728,0,0, 0)
this one will show system tray icons:

Code: Select all

; Show or Hide System Tray Icons or System Tray Clock Programmatic (Windows)
; RASHAD 18/02/2010

    h1 = FindWindow_("Shell_TrayWnd",0)   
    h2 = FindWindowEx_(h1,0,"TrayNotifyWnd",0)
    h3 = FindWindowEx_(h2,0,"SysPager",0)  
    h4 = FindWindowEx_(h3,0,"ToolbarWindow32",0)
    h5 = FindWindowEx_(h2,0,"TrayClockWClass",0)
    
;******************(  Hide System Tray Icons  )**************************
    
    ;ShowWindow_(h3,#SW_HIDE)
    ;SHChangeNotify_(134217728,0,0, 0)
    
;******************(  Show System Tray Icons  )**************************
    
    ShowWindow_(h3,#SW_SHOW)
    SHChangeNotify_(134217728,0,0, 0)    
    
;*********************(  Hide System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_HIDE)
    ;SHChangeNotify_(134217728,0,0, 0)
    
;*********************(  Show System Clock  )****************************
    
    ;ShowWindow_(h5,#SW_SHOW)
    ;SHChangeNotify_(134217728,0,0, 0)
Got it?
Egypt my love
User avatar
Spaceman Spiff
User
User
Posts: 11
Joined: Mon Feb 15, 2010 9:13 pm

Re: How to remove systray icon?

Post by Spaceman Spiff »

YAY RASHAD! That worked! This could really mess up someones mind! Ha!

I have the impulse to play a trick on my sister later... :lol:
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: How to remove systray icon?

Post by Michael Vogel »

Cool trick, rashad!

Is there a possibility to remove the empty space, where the icons have been, automatically?

Code: Select all

; Show or Hide System Tray Icons or System Tray Clock Programmatic (Windows)
; RASHAD 18/02/2010

h1 = FindWindow_("Shell_TrayWnd",0)
h2 = FindWindowEx_(h1,0,"TrayNotifyWnd",0)
h3 = FindWindowEx_(h2,0,"SysPager",0)
h4 = FindWindowEx_(h3,0,"ToolbarWindow32",0)
h5 = FindWindowEx_(h2,0,"TrayClockWClass",0)

#Wait=1000
;******************(  Hide System Tray Icons  )**************************
ShowWindow_(h3,#SW_HIDE)
SHChangeNotify_(134217728,0,0, 0)

End

;******************(  Show System Tray Icons  )**************************
Delay(#Wait)
ShowWindow_(h3,#SW_SHOW)
SHChangeNotify_(134217728,0,0, 0)

;*********************(  Hide System Clock  )****************************
Delay(#Wait)
ShowWindow_(h5,#SW_HIDE)
SHChangeNotify_(134217728,0,0, 0)

;*********************(  Show System Clock  )****************************
Delay(#Wait)
ShowWindow_(h5,#SW_SHOW)
SHChangeNotify_(134217728,0,0, 0)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: How to remove systray icon?

Post by RASHAD »

Hi Michael
Nice talking to you
Give me some details, like What do you want to do?(may be I can help)
I think XP is in your mind not Win 7, right?
Egypt my love
Post Reply