是否有一个命令可以在 Windows XP 的 .bat 文件中使用,使命令行窗口出现在前台?
是否有一个命令可以在 Windows XP 的 .bat 文件中使用,使命令行窗口出现在前台?
nircmd可以完成这个任务,但需要一些脚本编写。
nircmd win activate "titleofwindow"
你需要知道正在执行的cmd窗口的标题(你可以通过在Windows中使用TITLE命令来设置它)。
因此:
TITLE %SOME_UNIQUE_VALE%
nircmd win activate %SOME_UNIQUE_VALE%
这应该可以解决问题。
请注意,一些恶意软件工具利用NirCmd可执行文件(它无需部署且非常强大); 这可能会给您带来问题。
示例使用file1.bat:
....
[your code here]
start C:\file2.bat
exit
这个命令会关闭file1.bat文件并打开第二个.bat文件,你可以在其中继续编写代码。这个第二个.bat文件的命令提示符会显示在其他窗口前面。
我曾经也遇到了类似的问题,不得不开发了一个简单的C#控制台应用程序来将窗口置于最前。通过将窗口标题作为参数传递来选择要操作的窗口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
const int SW_RESTORE = 9;
public static void bringToFront(string title)
{
// Get a handle to the Calculator application.
IntPtr handle = FindWindow(null, title);
// Verify that Calculator is a running process.
if (handle == IntPtr.Zero)
{
return;
}
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
Console.WriteLine("Founded ");
SetForegroundWindow(handle);
}
static void Main(string[] args)
{
if (args.Length > 0)
bringToFront(args[0]);
else
Console.WriteLine("specify program window title");
}
}
}
我的批处理脚本代码类似于下面的内容:
tasklist /FI "IMAGENAME eq program.exe" | find "program.exe" 如果错误级别为1,那么就运行 program.exe 文件;否则,运行 BringToFront.exe 工具并将其应用到“Program Window Title”窗口。
从批处理文件中不行。如果您想要激活一个窗口,您必须使用SetActiveWindow()。如果您不想涉足Windows编程但仍想激活窗口和其他简单的操作,我强烈推荐使用Autoit。您可以从批处理文件中调用此程序来完成任务。
CMDOW对于此类任务和其他需要增加功能的DOS编程任务也非常有用。使用简单,文档详细。但要注意防病毒程序——CMDOW具有隐藏窗口的能力,这可能会被您的防病毒程序检测为病毒。只需将其添加到您的例外列表中。CMDOW完全可移植,绝对不是病毒,如果您担心它被第三方用于隐藏某些内容,只需将其放在某个不明显的文件夹中即可。
Ctrl+Shift+Esc
,那就会启动任务管理器。Shift+Tab
将选择选项卡栏。按几次左箭头最终会选择“应用程序”选项卡(因为它不循环且在最左边,所以没问题)。Tab
返回列表,输入窗口名称,然后按 Enter
。 - ArtOfWarfare