Link to home
Start Free TrialLog in
Avatar of ianmarchant
ianmarchant

asked on

Refresh System Tray / Automatically Close Application

hi,
i have a batch file which runs some automated tasks, and as part of these, it runs an application, then uses pskill (process kill) to close the application. this is because there is no automated way of closing it. it runs in the system tray and to manually close it i would have to right click on it and click exit. however cause of this way of closing it, the icon is left in the system tray of the application and when i move the mouse over it, it disappears just as u would imagine.

however i want a program or some way of refreshing the system tray icons, so these "dead" icons dont hang about.

alternatively, a way to close the application in the system tray automatically from a batch file would be useful!

i'm using Windows 2000 Server SP4.

i know about making any application a service and i dont wanna do that with this one.

any suggestions appreciated.

thanks
Avatar of kcarrim
kcarrim

You will need to find window handle of the system tray, gets its bounding rectangle, and programmatically moves the mouse over it.
I believe the operative API is GetWindowRect, documented at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Windows/WindowReference/WindowFunctions/GetWindowRect.asp 

Getting the system tray's handle seems to consist of (assuming the user hasn't switched shells):

ControlHandle=cWndByWndSpec("Shell_TrayWnd","explorer",0)

So the code would look something like this:
ControlHandle=cWndByWndSpec("Shell_TrayWnd","explorer",0)
buf = BinaryAlloc(16)
dll = StrCat(DirWindows(1), "USER32.DLL")
DllCall(dll, long:"GetWindowRect", long:ControlHandle, lpbinary:buf)
BinaryEodSet(buf, 16)
left = BinaryPeek4(buf, 0)
top = BinaryPeek4(buf, 4)
right = BinaryPeek4(buf, 8)
bottom = BinaryPeek4(buf, 12)
BinaryFree(buf)
Message("Coordinates", "%left% %top% %right% %bottom%")

Now use the MousePlay function to move the mouse to those coordinates

Or you can use tray utility
>>http://www.4t-niagara.com/4t-min.zip>>Free Ware
>>http://www.pssoftlab.com/download/pstrayf.zip>>Share Ware
Avatar of ianmarchant

ASKER

thanks for the quick response!

sorry, but i hardly know anything about programming, so have no idea what to do with all that.... can u write me a script to do this?

those tray utilties are for minimising applications to the system tray, i thought? how could they help in this situation?
also forgot to mention, that for security reasons this pc is always locked, so i need this to happen while it is locked... i realise this may make things more difficult, but applications and tasks still work even while a pc is locked, so i'm sure this is possible. if not, a workaround would be to refresh the system tray when i unlock the pc.

thanks for the help ;)
You could also try killing explorer.exe and re-starting it...not sure how this woudl affect running services but I think not at all...

If you kill you program, then kill explorer, then restart explorer does the icon stay there or do just those that were running return...? I think the icons would not return although I haven't had the opportunity to actually try it, why don't you let me know...?

J
Ok-one more thing, not sure if it will work or not, never tried it, but if you're willing we could always go to this key in the registry:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify

and delete these two dwords from the right hand side:

IconStreams
PastIconsStream

that should take care of any icon history, I am not sure if the key's will be re-created or not upon starting the program and then killing it once more,
without them, the systray should keep any history at all but simply destroy the icon once the process has been killed..Not sure if this will have any effect or not as the problem seems to be the wait time between killing the app and the system tray being notified of it...but we'll see.

John
You don't need to be much of a programmer. See here : http://www.winbatch.com
Use the info in my previous post together with Winbatch
ASKER CERTIFIED SOLUTION
Avatar of kcarrim
kcarrim

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Winbatch looks like a great programming--I got tired of payingfor software--Linux is really great that way...

AND, kcarrim, I supposed that would refresh the system tray while it hid and un-hid? That would definately be a work around for me if it works!

Cheers,

Jatcan
i dont want to use the kill explorer etc option, this is far too messy and could lead to system instability. plus i would loose the systemtray icons of the other applications i have running.

i think by deleting those keys, it would probably loose the icons of the other applications i have running aswell.

although winbatch looks useful, it looks far too complicated for doing what i need to do. and possible wouldnt b able to refresh the icons while the pc is locked anyway.

i'll try the "enableautotray" reg fix later, and let u know how i get on. is this similar to the built in option in XP where it hides the icons?

thanks for the suggestions guys.
kcarrim: by inactive icons, what does that mean? cause i have a few other applications which minimize to the systemtray, and obviously dont want them to disappear... what does it class as "inactive" or does it mean the way XP does it, when a icon hasnt actually made some on screen activity or hasnt been clicked on by the user?
On windows xp, the taskbar can be customized to hide or show inactive icons. This basically means that if the program has not ben accessed for a certain amount of time, the icon will be hidden. You can customize notifications. Say for exaple you are running spybot, the icon only gets hidden once it has been idle. If you look closely; right click taskbar, properties, check the box reading Hide inactive icons. Next to it is a button Customize. Look for Spybot, click on Spybot. You now have three options. Always show, Always hide and Hide when inactive. You will see current and past items. You can set the properties of you batch file's icon here. Have it always hidden!!
ok, thats probably what I want then!

will making that registry change enable that on Windows 2000 though???

Yes.
sorry for the delay, i've made the change... will i need to reboot for it to take effect?
Any changes to the registry requires a reboot.
thanks for the help/suggestions everyone.
Cool!