找回密码
 加入
搜索
查看: 28785|回复: 53

[原创] autoit刷新托盘图标

[复制链接]
发表于 2009-5-4 20:45:37 | 显示全部楼层 |阅读模式
本帖最后由 owkj 于 2009-5-4 20:50 编辑

问题:用autoir3结束了一个进程,这个进程在任务栏有图标(比如privoxy.exe),用autoir3结束了进程后,其托盘图标还在,必须把光标移到托盘图标 上才会消失,怎么让au3结束这个进程后刷新任务栏图标,使被结束的进程的图标消失?

http://www.autoitx.com论坛找了些资料,最后成功解决了本问题。

方案一:使用_RefreshSystemTray函数,查看6楼帖子http://www.autoitx.com/forum.php?mod=viewthread&tid=1480

例子:sshr.au3

#NoTrayIcon
If ProcessExists ( "myentunnel.exe" ) Or ProcessExists ( "privoxy.exe" ) Then
   ProcessClose("myentunnel.exe")
   ProcessClose("privoxy.exe")
   ProcessClose("Plink.exe")
   _RefreshSystemTray()
   _RefreshSystemTray()
Else
    ShellExecute (@ScriptDir & "\"&"privoxy.exe" , "config.txt" , @ScriptDir , "" ,@SW_HIDE)
        Run("myentunnel.exe")
EndIf

Func _RefreshSystemTray($nDelay = 1000)
    Local $oldMatchMode = Opt("WinTitleMatchMode", 4)
    Local $oldChildMode = Opt("WinSearchChildren", 1)
    Local $error = 0
    Do
        Local $hWnd = WinGetHandle("classname=TrayNotifyWnd")
        If @error Then
            $error = 1
            ExitLoop
        EndIf
        Local $hControl = ControlGetHandle($hWnd, "", "Button1")
        
        If $hControl <> "" And ControlCommand($hWnd, "", $hControl, "IsVisible") Then
            ControlClick($hWnd, "", $hControl)
            Sleep($nDelay)
        EndIf
        Local $posStart = MouseGetPos()
        Local $posWin = WinGetPos($hWnd)   
        Local $y = $posWin[1]
        While $y < $posWin[3] + $posWin[1]
            Local $x = $posWin[0]
            While $x < $posWin[2] + $posWin[0]
                DllCall("user32.dll", "int", "SetCursorPos", "int", $x, "int", $y)
                If @error Then
                    $error = 2
                    ExitLoop 3;
                EndIf
                $x += 8
            WEnd
            $y += 8
        WEnd
        DllCall("user32.dll", "int", "SetCursorPos", "int", $posStart[0], "int", $posStart[1])
        If $hControl <> "" And ControlCommand($hWnd, "", $hControl, "IsVisible") Then
            ControlClick($hWnd, "", $hControl)
        EndIf
    Until 1
    Opt("WinTitleMatchMode", $oldMatchMode)
    Opt("WinSearchChildren", $oldChildMode)
    SetError($error)
EndFunc
缺点:刷新托盘图标时会看到鼠标移动到了托盘图标上,虽然只是一瞬间的事。

还有一个问题是,某次我将桌面主题换为windows经典后,上面的脚本却只能将结束的两个进程的其中一个的托盘图标隐藏,很怪异,刚刚又试了一下,却正常了。

方案二:  使用SysTray_UDF.au3,在结束进程前,先用_SysTrayIconVisible函数隐藏它的托盘图标,然后再结束它的进程。看起来就象是将托盘图标刷新了一下。本人感觉这个方法更好一些。

例子:ssh.au3

;自动调用ssh代理,第一次运行自动调用myentunnel和privoxy,再运行一次可结束myentunnel和privoxy,并隐藏myentunnel和privoxy的图标
#include "SysTray_UDF.au3"
If ProcessExists ( "myentunnel.exe" ) Or ProcessExists ( "privoxy.exe" ) Then
   _SysTrayIconVisible(1, _SysTrayIconIndex("privoxy.exe"));隐藏privoxy的托盘图标
   _SysTrayIconVisible(1, _SysTrayIconIndex("myentunnel.exe"));隐藏myentunnel的托盘图标
   ProcessClose("myentunnel.exe")
   ProcessClose("privoxy.exe")
   ProcessClose("Plink.exe")
   TraySetState (1);必须的,否则隐藏myentunnel和privoxy的图标后托盘处会有空白
Else
    ShellExecute (@ScriptDir & "\"&"privoxy.exe" , ".\config.txt" , @ScriptDir , "" ,@SW_HIDE)
        Run("myentunnel.exe")
EndIf
下载地址(本文中两个例子及其所用到的东西)
http://www.ucomo.com/space/FileD ... 579&DOMAIN=OWKJ

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2009-5-19 05:59:48 | 显示全部楼层
谢谢楼主奉献  收藏了
发表于 2009-5-19 12:30:54 | 显示全部楼层
学习了,感谢楼主分享
发表于 2009-5-21 13:47:32 | 显示全部楼层
_TrayIconDelete($nTrayIcon1);先删除图标,然后进行退出。
                        Exit

这样不行么?
发表于 2009-5-21 13:48:51 | 显示全部楼层
我开始也发现问题了,然后就按照下面的方式做了,效果很好,请大家指正
Func quit();确认退出程序弹出
        If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer
        $iMsgBoxAnswer = MsgBox(270625, "确定要退出 ", "退出,你的电脑将失去保护,确定要退出吗?")
        Select
                Case $iMsgBoxAnswer = 1 ;OK
                        _TrayIconDelete($nTrayIcon1);先删除图标,然后进行退出。
                        Exit
                Case $iMsgBoxAnswer = 2 ;Cancel
        EndSelect
        ;退出菜单
EndFunc
发表于 2009-5-21 18:26:54 | 显示全部楼层
习了,感谢楼主分享
发表于 2009-6-3 09:50:31 | 显示全部楼层
收藏起来..
发表于 2009-6-15 08:37:22 | 显示全部楼层
每次禁用了程序后,都会有图标很不爽
发表于 2009-6-20 09:25:40 | 显示全部楼层
我要UDF啊 可惜没钱
发表于 2009-6-20 09:25:48 | 显示全部楼层
发表于 2009-9-1 11:48:48 | 显示全部楼层
收藏起来。。。 感谢楼主分享!!
发表于 2009-9-18 20:22:31 | 显示全部楼层
不错,难道没有更好的办法?假如程序不是你的,杀掉进程怎么办。
发表于 2009-9-20 09:06:48 | 显示全部楼层
呵呵,温习一下。。
发表于 2009-9-24 18:01:57 | 显示全部楼层
哈哈..又学习了.!!!知道怎么隐藏托盘图标了.
发表于 2009-10-14 20:40:33 | 显示全部楼层
谢谢楼主奉献  收藏了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-28 23:35 , Processed in 0.085202 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表