Re: How to remove systray icon?
Posted: Wed Feb 17, 2010 7:53 pm
Then how do you normally close this program?
http://www.purebasic.com
https://www.purebasic.fr/english/
You have to right click on the system tray icon and select exit.srod wrote:Then how do you normally close this program?
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!srod I think it is not that easy
I only succeeded to delete one icon ( the network )
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.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
Here's an example: Compile this as trayicon.exe: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
Code: Select all
OpenWindow(0,0,0,0,0,"",#PB_Window_Invisible)
AddSysTrayIcon(1, WindowID(0), ExtractIcon_(0,"notepad.exe",0))
Repeat
WaitWindowEvent()
ForEver
Code: Select all
processID.l = RunProgram("trayicon.exe","","",#PB_Program_Open)
Delay(10000)
KillProgram(processID)
CloseProgram(processID)
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
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)
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)
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)
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)