【问题标题】:Refreshing Windows TaskBar with VBScript for windows?使用 Windows 的 VBScript 刷新 Windows 任务栏?
【发布时间】:2011-01-09 16:14:59
【问题描述】:

有人在我公司制作了一个VBScript (.vbs) 脚本。它打开和关闭某些应用程序,该应用程序有一个托盘图标,每次关闭(通过控制台杀死)该应用程序时,托盘图标仍然存在。因此,在运行脚本一两天后,应用程序的数百万个图标仍保留在任务栏上。

VBScript中有没有让windows刷新任务栏并去掉这些图标的指令?

这是脚本:

*****************************************************************************************
' Progam Name: CheckConnection.vbs
' Program Purpose: This script check for an internec connection, the program pings to some domains,
'       wait some seconds (5 seconds) before the next try, if a domain is found, reset all counters,
'       and wait secondsBeforeNextCheck seconds before next check
'       If the programs raise nMaxRetriesBeforeStartProgram then kill the program that
'       manage the connection and start it again.
' Usage: This Script must be placed in ONE OF THE FOLLOWING PATHS
'   1) Start --> All Programs --> Start Up
'   2) HKLM\Software\Microsoft\Windows\CurrentVersion\Run
'   3)HKCU\Software\Microsoft\Windows\CurrentVersion\Run
'   4) Or you can create a task through:
'       Start --> All Programs --> Accessories-->System Tools --> Scheduled Tasks--> Add Scheduled Task
'       --> Follow the Wizard'
'   5) The program VZAccess Manager.exe must be configured to autoconnect through
'       Tools --> Preferences --> WWan --> Connect Options --> Automatically Connect (this must be checked)
'   6) Also to autorefresh the ip the vzaccess manager.exe must be configured with an script when c onnect.
' Author: Benito Lopez
' Date:  06152009
' Revision 1.1 : (11272009) -f was added to force the program VZAccess manager.exe to terminate.
' *****************************************************************************************
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
Dim objfso, objfile
Set objfso = CreateObject("Scripting.FileSystemObject")
Dim secondsBeforeNextCheck
Dim strDomains(6)
Dim nMaxRetriesBeforeStartProgram
Dim nCounterRetries

Const ForReading=1, ForWriting=2, ForAppending=8

strDomains (0) = "www.google.com"
strDomains (1) = "www.yahoo.com"
strDomains (2) = "www.chilpak.com"
strDomains (3) = "www.microsoft.com"
strDomains (4) = "www.flutec.com"
strDomains (5) = "www.flutec.com.mx"

strLogFilename = "c:\checkconnectionlog.log"
filepinger = "c:\pingresult.log"
nMaxRetriesBeforeStartProgram =5
secondsBeforeNextCheck = 60000
secondsBeforeNextTry = 15000
secondsBeforeAppStart = 2000
secondsAfterAppStart = 60000

Call Main()

Sub Main()
    set objfile = objfso.OpenTextFile(strLogFilename, 8,True)

    WHILE 1
        nCounterRetries = 0
        FOR i = 0 TO 5 STEP 1
            'Check the connection status if zero, means
            'There is no internet connection or the domain is down
            If not IsThereInternetConnection(strDomains(i)) THEN
                'Let suppose the domain is down, so we must try with the next domain
                nCounterRetries = nCounterRetries + 1
                IF nCounterRetries >= nMaxRetriesBeforeStartProgram THEN
                    'Close the program
                    targetprogram = "taskkill /im " & chr(34)  & "VZAccess Manager.exe" & chr(34) & " /f"
                    WshShell.Exec(targetprogram)
                    'Wait Applications Events 2 seconds
                    Wscript.Sleep(secondsBeforeAppStart)
                    'Start the program
                    targetprogram = chr(34) & "C:\Program Files\Verizon Wireless\VZAccess Manager\VZAccess Manager.exe" & chr(34) & " -m"
                    WshShell.Exec(targetprogram)
                    'Wait for the applications loads completely 1 seconds
                    WScript.Sleep(secondsAfterAppStart)
                    'Reset the counters
                    nCounterRetries = 0
                    Wscript.Sleep secondsBeforeNextCheck
                END IF
                'But we must wait some seconds before the next try
                Wscript.Sleep secondsBeforeNextTry

            ELSE
                'If we get a reply from the current domain,
                'Everything is OK,  The only we need to do is Wait until the next time to check
                'Reset Counter
                nCounterRetries = 0
                WScript.Sleep secondsBeforeNextCheck
            END IF
        NEXT
    Wend
End Sub

FUNCTION IsThereInternetConnection(strDomain)
on error resume next
    'Create some cons values
    'Make the ping
    Dim strRun
    strRun = "%comspec% /c ping -n 1 " & strDomain & " > " & filepinger
    Dim objwss
    Set objwss = CreateObject("WScript.Shell")
    'Hide the windows and wait the app to terminate
    WriteLog "Pinging " & strDomain & " ..."
    objwss.Run strRun,0,True
    Set objwss = Nothing

    ''Read the ping
    Dim strotf
    Dim fso
    Dim otf
    strotf=""
    set fso = CreateObject("Scripting.FileSystemObject")
    set otf = fso.OpenTextFile(filepinger,ForReading)
    strotf = otf.ReadAll
    fso.DeleteFile filepinger
    set fso = nothing
    set otf = nothing

    'Test the ping
    IF InStr(strotf,"Reply from")>0 THEN
        IsThereInternetConnection = TRUE
    ELSE
        IsThereInternetConnection = FALSE
    END IF
    WriteLog "Pinging Result " & IsThereInternetConnection & " Done."
End FUNCTION

Sub WriteLog(strLog)

    on error resume next

    Set objfile = objfso.GetFolder(strLogFilename)
    if objfile.size  > 50000 then
        objfso.Close
        objfso.DeleteFile strLogFilename
        set objfile = objfso.OpenTextFile(strLogFilename, 8,True)
    end if
    'Add some useful information
    strData = Now() & " - "
    objfile.WriteLine strData & strLog
End Sub

【问题讨论】:

    标签: windows vbscript


    【解决方案1】:

    系统托盘中的孤立图标在您将鼠标指针移到它们上方后消失。我想这可以通过将WM_MOUSEMOVE 消息发送到系统托盘窗口以编程方式完成,但 VBScript 无法访问 Windows API。

    我建议您使用一些外部实用程序来刷新系统托盘并从您的脚本中运行它。例如,CodeProject 上有 TrayIconBuster 实用程序,它可以每 5 秒清理一次托盘。 (但它需要 .NET Framework。)或者您可以自己编写一个类似的工具。

    要从 VBScript 代码运行应用程序,您可以使用 WshShell.RunWshShell.Exec 方法。您可以在脚本中找到示例。

    【讨论】:

      【解决方案2】:

      使用vb调用bat文件,里面有restart Explorer命令或reg文件

      Windows 注册表编辑器 5.00 版 [-HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify] [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\TrayNotify]

      【讨论】:

        【解决方案3】:

        程序告诉资源管理器戴上或取下图标。如果您与图标交互,资源管理器会尝试告诉程序执行任何操作并注意到它不存在并删除图标。

        Taskkill 可以做两件事。它可以要求程序关闭(程序可以弹出一个对话框“保存文件? - 是/否/取消”)并且程序按照设计或使用/f 关闭它终止程序而不通知程序,所以程序不能要求资源管理器删除图标。

        尝试不使用 /f 开关的 Taskkill。您可以随时调用它两次,然后使用 /f 开关。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-24
          • 1970-01-01
          • 1970-01-01
          • 2010-12-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多