循环浏览打开的 Windows 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8356046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:34:27  来源:igfitidea点击:

Cycle through open Windows applications

windowsvbscript

提问by Ian Burns

I need some help on which way to go with something that seemed at first like a very simple requirement.

我需要一些帮助,以了解如何处理起初看起来非常简单的要求。

I have to find a method of cycling through open applications on a Windows PC, with the objective of displaying the windows for say 30 seconds at a time on a large screen mounted on a wall. Typically there will be an MS Access report and a couple of web pages.

我必须找到一种在 Windows PC 上循环浏览打开的应用程序的方法,目的是在安装在墙上的大屏幕上一次显示窗口 30 秒。通常会有一个 MS Access 报告和几个网页。

My initial thinking was that I could open these apps manually on the PC, then run a VBScript to cycle through them. However there were two problems with this.

我最初的想法是我可以在 PC 上手动打开这些应用程序,然后运行 ​​VBScript 来循环浏览它们。然而,这有两个问题。

  1. Simulating the Alt+Tab keypress just toggles the two most recently used apps instead of cycling through them all, and
  2. There is no possibility I can see for the user to be able to escape out of the script using a keypress.
  1. 模拟 Alt+Tab 按键只会切换最近使用的两个应用程序,而不是循环浏览它们,并且
  2. 我无法看到用户能够使用按键退出脚本。

Can anyone suggest how I can achieve this using resources already available on a Windows (XP upwards) machine?

谁能建议我如何使用 Windows(XP 以上)机器上已有的资源来实现这一目标?

回答by Ian Burns

Turned out VBScript in WHS was the way to go. This seems to work.

原来 WHS 中的 VBScript 是要走的路。这似乎有效。

    '****************************************************************************************
' Script Name: ApplicationCycler.vbs
'      Author: Ian Burns
'        Date: 2 Dec 2011
' Description: VBScript for Windows Scripting Host. Cycles through any applications 
'              visible in the Task Bar giving them focus for a set period.
'       Usage: Save file to Desktop and double click to run. If it isn't already running,
'              it will start. If it is already running, it will stop.
'*****************************************************************************************
Option Explicit

Dim wshShell
Dim wshSystemEnv
Dim strComputer
Dim objWMIService
Dim colProcessList 
Dim objProcess
Dim intSleep

' Loop lasts 5 seconds
intSleep = 5000

Set wshShell = CreateObject("WScript.Shell")
' Volatile environment variables are not saved when user logs off
Set wshSystemEnv = wshShell.Environment("VOLATILE")

' Check to see if the script is already running
If len(wshSystemEnv("AlreadyRunning")) = 0 Then

    ' It isn't, so we set an environment variable as a flag to say the script IS running
    wshSystemEnv("AlreadyRunning") = "True"

    ' Now we go into a loop, cycling through all the apps on the task bar
    Do
        ' Simulate the Alt+Esc keypress
        wshShell.SendKeys "%+{Esc}"
        Wscript.Sleep intSleep
    Loop

Else

    ' It IS already running so kill any or all instances of it
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'WScript.exe'")
    For Each objProcess in colProcessList
        objProcess.Terminate()
    Next

    ' Delete the environment variable
    wshSystemEnv.Remove("AlreadyRunning")

    ' Tidy up
    Set wshSystemEnv = Nothing
    Set wshShell = Nothing
    Set objWMIService = Nothing
    Set colProcessList = Nothing

End If

回答by Makaveli84

For what it's worth, here's a "cleaner" solution (check my comment on the proposed solution) based on Ian Burns' script:

对于它的价值,这里有一个基于 Ian Burns 脚本的“更干净”的解决方案(查看我对建议解决方案的评论):

'****************************************************************************************
' Script Name: ApplicationCycler.vbs
'      Author: Ian Burns
'        Date: 2 Dec 2011
'   Edited By: Makaveli84
'   Edit Date: 30 Aug 2014
' Description: VBScript for Windows Scripting Host. Cycles through any applications 
'              visible in the Task Bar giving them focus for a set period.
'       Usage: Save file to Desktop and double click to run. If it isn't already running,
'              it will start. If it is already running, it will stop.
'*****************************************************************************************
Option Explicit

Dim wshShell
Dim wshSystemEnv
Dim intCycle
Dim intSleep
Dim intTimer

' Cycle every 5 seconds / Check on/off status every 250 milliseconds
intCycle = 5000: intSleep = 250: intTimer = intCycle

Set wshShell = CreateObject("WScript.Shell")
' Volatile environment variables are not saved when user logs off
Set wshSystemEnv = wshShell.Environment("VOLATILE")

' Check to see if the script is already running
If len(wshSystemEnv("AlreadyRunning")) = 0 Then

    ' It isn't, so we set an environment variable as a flag to say the script IS running
    wshSystemEnv("AlreadyRunning") = "True"

    ' Now we go into a loop, cycling through all the apps on the task bar
    Do While len(wshSystemEnv("AlreadyRunning")) > 0
        ' Simulate the Alt+Esc keypress
        If intTimer >= intCycle Then
            wshShell.SendKeys "%+{Esc}"
            intTimer = 0
        End If
        intTimer = intTimer + intSleep
        Wscript.Sleep intSleep
    Loop

Else
    ' Delete the environment variable
    wshSystemEnv.Remove("AlreadyRunning")
End If


' Tidy up
Set wshSystemEnv = Nothing
Set wshShell = Nothing

回答by Dave

It's late and I did need a way to turn this excellent solution on and off manually. Therefore, tested on xp Pro sp3:

很晚了,我确实需要一种方法来手动打开和关闭这个出色的解决方案。因此,在 xp Pro sp3 上测试:

'****************************************************************************************
' Script Name: Turn ON Loop thru open apps.vbs
'      Author: Ian Burns
'        Date: 2 Dec 2011
' Description: VBScript for Windows Scripting Host. Cycles through any applications 
'              visible in the Task Bar giving them focus for a set period.
'       Usage: Save file to Desktop and double click to run. If it isn't already running,
'              it will start. If it is already running, it will stop.
'*****************************************************************************************

' http://stackoverflow.com/questions/8356046/cycle-through-open-windows-applications

Option Explicit

Dim wshShell
Dim wshSystemEnv
Dim strComputer
Dim objWMIService
Dim colProcessList 
Dim objProcess
Dim intSleep

' Do Loop lasts 5 seconds
intSleep = 5000

Set wshShell = CreateObject("WScript.Shell")
' Volatile environment variables are not saved when user logs off
Set wshSystemEnv = wshShell.Environment("VOLATILE")

If len(wshSystemEnv("AlreadyRunning")) = 0 Then

    ' It isn't, so we set an environment variable as a flag to say the script IS running
    wshSystemEnv("AlreadyRunning") = "True"

    ' Now we go into a loop, cycling through all the apps on the task bar
    do
        ' Simulate the Alt+Esc keypress
        wshShell.SendKeys "%+{Esc}"
        Wscript.Sleep intSleep
    loop

    ' Tidy up
    Set wshSystemEnv = Nothing
    Set wshShell = Nothing
    Set objWMIService = Nothing
    Set colProcessList = Nothing

End If

and:

'****************************************************************************************
' Script Name: Turn OFF Loop thru open apps.vbs
'      Author: Dave
'        Date: 1 Mar 2012
' Description: Turns off the above.
' '*****************************************************************************************
Option Explicit

Dim wshShell
Dim wshSystemEnv
Dim strComputer
Dim objWMIService
Dim colProcessList 
Dim objProcess
Dim intSleep

Set wshShell = CreateObject("WScript.Shell")
' Volatile environment variables are not saved when user logs off
Set wshSystemEnv = wshShell.Environment("VOLATILE")

    ' It IS already running so kill any or all instances of the above
    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & _
                        "{impersonationLevel=impersonate}!\" & _
                        strComputer & "\root\cimv2")

    Set colProcessList = objWMIService.ExecQuery _
                           ("Select * from Win32_Process Where Name = 'WScript.exe'")

    For Each objProcess in colProcessList
        objProcess.Terminate()
    Next

    ' Delete the environment variable
    wshSystemEnv.Remove("AlreadyRunning")

    ' Tidy up
    Set wshSystemEnv = Nothing
    Set wshShell = Nothing
    Set objWMIService = Nothing
    Set colProcessList = Nothing