Windows XP 命令外壳的“置于前面”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/557166/
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-09 06:04:21  来源:igfitidea点击:

"Bring to front" for Windows XP command shell

windowsbatch-filewindows-shell

提问by Lauren Samuels

Is there a command I can put into a Windows XP .bat file to bring the command shell to the front?

是否可以将命令放入 Windows XP .bat 文件中以将命令外壳置于最前面?

采纳答案by ryeguy

From a batch file, no. If you want to activate a window you have to use SetActiveWindow(). If you don't want to get dirty with windows programming but still want to activate windows and simple stuff like that, I highly recommend checking out Autoit. You could always call this program from your batchfile to have it do the task.

从批处理文件中,没有。如果你想激活一个窗口,你必须使用SetActiveWindow()。如果您不想弄脏 Windows 编程,但仍想激活 Windows 和类似的简单东西,我强烈建议您查看Autoit。你总是可以从你的批处理文件中调用这个程序来让它完成任务。

回答by ShuggyCoUk

nircmdwill do this, though it involves a little scripting.

nircmd将执行此操作,尽管它涉及一些脚本。

nircmd win activate "titleofwindow"

You basically need to know the title of the cmd window you are executing (you can set this via the TITLE command in windows)

你基本上需要知道你正在执行的 cmd 窗口的标题(你可以通过 windows 中的 TITLE 命令设置它)

thus:

因此:

TITLE %SOME_UNIQUE_VALE%
nircmd win activate %SOME_UNIQUE_VALE%

should do the trick.

应该做的伎俩。

Note some malware tools make use of the NirCmd executable (it requires no deployment as is very powerful) this may cause you problems.

请注意,一些恶意软件工具使用 NirCmd 可执行文件(它不需要部署,因为它非常强大)这可能会给您带来问题。

回答by Rodger

Another way to get the cmd prompt window to show in front is by ending file1.bat with a command to call a second file2.bat file, followed by an exit command.

使 cmd 提示窗口显示在前面的另一种方法是使用调用第二个 file2.bat 文件的命令结束 file1.bat,然后是退出命令。

EXAMPLE using file1.bat

示例使用 file1.bat

....
[your code here]
start C:\file2.bat
exit

This closes file1.bat and opens a second .bat file where you can continue with your code. This second .bat command prompt will open in front of other windows

这将关闭 file1.bat 并打开第二个 .bat 文件,您可以在其中继续您的代码。第二个 .bat 命令提示符将在其他窗口前面打开

回答by Panciz

I had a similar problem and I had to develop a simple C# console application that brings to front a Window. The windows is selected using the window title pass as argument.

我有一个类似的问题,我不得不开发一个简单的 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");

        }
    }
}

the code of my batch script is then something similar to

我的批处理脚本的代码然后类似于

tasklist /FI "IMAGENAME eq program.exe" | find "program.exe" if errorlevel 1 (program.exe) else (BringToFront.exe "Program Window Title")

tasklist /FI "IMAGENAME eq program.exe" | 如果 errorlevel 1 (program.exe) else (BringToFront.exe "Program Window Title"),则找到“program.exe”

回答by Shadeclan

CMDOWis also useful for this and for other DOS programming tasks where a little added functionality is needed. Simple to use and well documented. Mind your anti-virus program, though - CMDOW has the ability to hide windows which your anti-virus program will pick up as a possible virus. Just add it to your exception list. CMDOW is completely portable, is definitely NOT a virus and if you have any concerns about it being used by a third party to hide something, simply tuck it away in some non obvious folder somewhere.

CMDOW对此以及其他需要一些附加功能的 DOS 编程任务也很有用。使用简单且有据可查。不过,请注意您的防病毒程序 - CMDOW 能够隐藏您的防病毒程序将其视为可能的病毒的窗口。只需将其添加到您的例外列表中即可。CMDOW 是完全可移植的,绝对不是病毒,如果您担心第三方使用它来隐藏某些东西,只需将其隐藏在某个不明显的文件夹中即可。

回答by user1491819

Another quick way to switch to a window by name is via Ctrl+Shift+Esc, which opens Task Manager. Then just type the first few letters of the windows title to select the process, then hit Enter.

另一种按名称切换到窗口的快速方法是通过 Ctrl+Shift+Esc,它会打开任务管理器。然后只需键入窗口标题的前几个字母来选择进程,然后按 Enter。