WScript.Shell AppActivate并非每次都起作用 [英] WScript.Shell AppActivate doesn't work every time

查看:1089
本文介绍了WScript.Shell AppActivate并非每次都起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些脚本可以帮助您在CMD中运行程序.启动序列的第一个脚本在启动时运行.在此过程中,我正在运行一个VBScript,试图将其重点放在CMD上并使其全屏显示.但是,有时它可以工作,有时却不起作用,并且我没有做任何更改,只是一遍又一遍地重启计算机.它并不总是专注于cmd窗口,因此问题应该出在AppActivate方法上.

I have a few scripts to help running a program in CMD. The first script that starts the sequence is run on startup. In the process, I'm running a VBScript with which I'm trying to focus on the CMD and make it fullscreen. However, sometimes it works and sometimes it doesn't and I'm not changing anything, just restarting the computer over and over again. It doesn't always focus on the cmd window so the problem should be with the AppActivate method.

这是启动时运行的第一个脚本的代码:

Here is the code for the first script that runs on startup:

@echo off
TITLE masterBat
START "Master" /B /W myApp.bat
shutdown.exe /s /t 00

这是myApp.bat的代码:

Here is the code for myApp.bat:

@echo off
cscript fullscreen.vbs
cd <path>
CMD /C <command>
exit

这是fullscreen.vbs的代码:

Set ws = WScript.CreateObject("WScript.Shell")
ws.AppActivate "masterBat - myApp.bat"
ws.SendKeys "{F11}"
Set ws = Nothing

当我试图使其全屏显示时,AppActivate调用中的标题就是cmd上的标题.我还尝试在脚本中添加一些WScript.Sleep调用,但这似乎无济于事.

The title in the AppActivate call is the title on the cmd when I'm trying to make it fullscreen. I've also tried adding some WScript.Sleep calls in the script but it doesn't seem to help.

推荐答案

Wscript.sleep将使情况变得更糟.你不想睡觉.

Wscript.sleep will make it worse. You don't want to sleep.

有规则.您的程序必须遵守以下规则之一才能设置前景窗口.

There are rules. Your program MUST obey one of these rules to be allowed to set the foreground window.

来自 https://docs. microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setforegroundwindow

系统限制可以设置前台窗口的进程.仅当满足以下条件之一时,进程才能设置前景窗口:

The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

•该过程是前台过程.

•该进程由前台进程启动.

•The process was started by the foreground process.

•该进程收到了最后一个输入事件.

•The process received the last input event.

•没有前台进程.

•该进程正在调试中.

•前台过程不是现代应用程序或启动程序 屏幕.

•The foreground process is not a Modern Application or the Start Screen.

•前景未锁定(请参见LockSetForegroundWindow).

•The foreground is not locked (see LockSetForegroundWindow).

•前景锁定超时已到期(请参阅 SystemParametersInfo中的SPI_GETFOREGROUNDLOCKTIMEOUT.)

•The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).

•没有菜单处于活动状态.

•No menus are active.

当用户使用时,应用程序无法将窗口强制到前台 正在使用另一个窗口.而是,Windows闪烁任务栏 窗口中的按钮以通知用户.

An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

这意味着如果脚本没有用户界面(因为它不能是前台窗口),那么从脚本开始设置另一个窗口起,您有两秒钟的时间(FOREGROUNDLOCKTIMEOUT).一个消息框是您要做的第一件事(直到发生其他事情).此外,Wscript的消息框超时-WshShell.Popup.

What this means is you have two seconds (FOREGROUNDLOCKTIMEOUT) from when your script starts to set another window if your script doesn't have a user interface (as it can't be the foreground window). A message box as the first thing you do will comply (till something else happens). Also Wscript has a message box that times out - WshShell.Popup.

这是为了防止用户感到无聊并且现在正在使用另一个程序时启动缓慢的程序,然后再转移焦点.

This is to prevent slow starting programs, when the user has got bored and is now working in another program, from then stealing the focus.

预防性评论通常,人们会想与我争论.向微软投诉不是我.而且你不能与规则争论.您可以遵守它们.

Preventive comment Normally people want to argue with me about this. Complain to Microsoft not me. And you can't argue with rules. You can just comply with them.

这篇关于WScript.Shell AppActivate并非每次都起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆