owkj 发表于 2009-5-4 20:45:37

autoit刷新托盘图标

本帖最后由 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
      While $y < $posWin + $posWin
            Local $x = $posWin
            While $x < $posWin + $posWin
                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, "int", $posStart)
      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/FileDetail.aspx?FileID=833579&DOMAIN=OWKJ

hanshiqi2000 发表于 2009-5-19 05:59:48

谢谢楼主奉献收藏了

yy517 发表于 2009-5-19 12:30:54

学习了,感谢楼主分享

nxbigdaddy 发表于 2009-5-21 13:47:32

_TrayIconDelete($nTrayIcon1);先删除图标,然后进行退出。
                        Exit

这样不行么?

nxbigdaddy 发表于 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

redapple2008 发表于 2009-5-21 18:26:54

习了,感谢楼主分享

jhkdqaa 发表于 2009-6-3 09:50:31

收藏起来..

radium 发表于 2009-6-15 08:37:22

每次禁用了程序后,都会有图标很不爽

yjcblue 发表于 2009-6-20 09:25:40

我要UDF啊 可惜没钱

yjcblue 发表于 2009-6-20 09:25:48

http://www.autoitx.com/forum.php?mod=viewthread&tid=6862&highlight=%CB%A2%D0%C2%CD%D0%C5%CC

ollydbg 发表于 2009-9-1 11:48:48

收藏起来。。。 感谢楼主分享!!

ccxw1983 发表于 2009-9-18 20:22:31

不错,难道没有更好的办法?假如程序不是你的,杀掉进程怎么办。

menfan 发表于 2009-9-20 09:06:48

呵呵,温习一下。。

chengjinn 发表于 2009-9-24 18:01:57

哈哈..又学习了.!!!知道怎么隐藏托盘图标了.

analyzer 发表于 2009-10-14 20:40:33

谢谢楼主奉献收藏了
页: [1] 2 3 4
查看完整版本: autoit刷新托盘图标