“Bring to front” for Windows XP command shell

后端 未结 7 1228
误落风尘
误落风尘 2020-12-09 13:20

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

7条回答
  •  [愿得一人]
    2020-12-09 13:33

    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.

    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")

提交回复
热议问题