运行 Windows 应用程序而不显示其 GUI

All*_*ons 5 windows-7 powershell shell-script vbscript

有没有办法在不显示 GUI 窗口的情况下运行 Windows 应用程序?

我有一个第三方 Windows 应用程序,它没有任何命令行参数或开关来抑制其 GUI。我需要在后台启动它,并且仅使用该第三方应用程序发布的数据交换 API 以编程方式与其交互。

我尝试使用隐藏复选框在任务计划程序中创建任务,但即使这样,当我手动启动任务时,应用程序的窗口也会显示。我猜这个应用程序被编程为在启动后自动聚焦。

我需要这个适用于 Windows 7 的解决方案。

谢谢。

Ƭᴇc*_*007 3

也许您可以通过 PowerShell 使用 Win32 API 来查找和隐藏目标应用程序的窗口。

示例代码:

$definition = @"    
      [DllImport("user32.dll")]
      static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

      public static void Show(string wClass, string wName)
      {
         IntPtr hwnd = FindWindow(wClass, wName);
         if ((int)hwnd > 0)
            ShowWindow(hwnd, 1);
      }

      public static void Hide(string wClass, string wName)
      {
         IntPtr hwnd = FindWindow(wClass, wName);
         if ((int)hwnd > 0)
            ShowWindow(hwnd, 0);
      }
"@

add-type -MemberDefinition $definition -Namespace my -Name WinApi

[my.WinApi]::Hide('Notepad', 'Untitled - Notepad')
Run Code Online (Sandbox Code Playgroud)

来自Aryadev 对“Hide a Window with Powershell ISE?”的回答的源代码 在 StackOverflow 上。