Page 3 of 4

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 7:53 pm
by srod
Then how do you normally close this program?

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 7:55 pm
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.

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 7:58 pm
by RASHAD
srod I think it is not that easy
I only succeeded to delete one icon ( the network )

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 7:59 pm
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: )

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 8:24 pm
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. :)

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 8:56 pm
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.

Re: How to remove systray icon?

Posted: Wed Feb 17, 2010 10:02 pm
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.

Re: How to remove systray icon?

Posted: Thu Feb 18, 2010 11:32 am
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:

Re: How to remove systray icon?

Posted: Thu Feb 18, 2010 3:29 pm
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

Re: How to remove systray icon?

Posted: Thu Feb 18, 2010 9:23 pm
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

Re: How to remove systray icon?

Posted: Thu Feb 18, 2010 11:52 pm
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:

Re: How to remove systray icon?

Posted: Fri Feb 19, 2010 12:43 am
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?

Re: How to remove systray icon?

Posted: Fri Feb 19, 2010 12:52 am
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:

Re: How to remove systray icon?

Posted: Fri Feb 19, 2010 10:33 am
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)

Re: How to remove systray icon?

Posted: Fri Feb 19, 2010 10:43 am
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?