在第二个监视器上使用node.js启动外部exe

时间:2017-05-12 06:14:13

标签: node.js windows exec remote-desktop multiple-monitors

您知道吗,是否可以在特定显示器上启动软件?以及如何做到这一点?

我的计算机上有一个node.js服务器(Windows 10),我需要启动一些外部软件(我无法更改该外部软件中的代码,因为我只有exe文件)。最终的应用程序应该使用真实监视器和第二个假监视器进行设置。在第一个监视器上是一个全屏浏览器窗口。有一个启动和停止按钮(它们已经工作)来启动和停止外部软件。在第二台显示器(不可见)上应启动外部软件。 如果一切都以这种方式工作,我可以连接该计算机上的每个遥控器并查看第二个屏幕。在那里我可以使用外部软件。并且在该计算机的监视器(1)上始终只有浏览器窗口可见。

要测试我使用notepad.exe作为外部软件。 如果我单击开始,软件将在我的主显示器(1)上打开,但它应该从第二台显示器上启动。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

看起来,直接做到这一点是不可能的。但是如果你从node.js开始一个小帮手程序就不那么困难了。您可以使用execFile()查看:nodejs docs

启动它

我用一些C#控制台应用程序创建了以下函数:

    [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
    public static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "ShowWindow")]
    public static extern bool ShowWindow(IntPtr hWnd, int cmdShow);

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);

然后我遍历System.Windows.Forms.Screen.AllScreens并以Process我的可执行文件开始。从Process开始,我得到MainWindowHandle,然后可以在屏幕上设置所需的窗口。

 static void Main(string[] args)
    {
        if (args.Length == 1 && args[0].Equals("h"))
        {
            Console.Out.WriteLine("Command: [displayName] [executable] [arguments for executable]");
            return;
        }

        if (args.Length < 2)
        {
            Console.Out.WriteLine("arguments not correct. Should be: [displayName] [executable1] [arguments for executable]");
            return;
        }

        foreach (var screen in Screen.AllScreens)
        {
            Console.Out.WriteLine(screen.DeviceName);
            if (screen.DeviceName.Equals(args[0]))
            {
                var process = new Process();

                process.StartInfo.FileName = args[1];
                string[] arguments = args.Skip(2) as string[];
                if (arguments != null) process.StartInfo.Arguments = string.Join(" ", arguments);

                process.Start();

                var hwnd = process.MainWindowHandle;
                Console.Out.WriteLine("while get process.MainWindowHandle");
                while (!process.HasExited)
                {
                    process.Refresh();
                    if (process.MainWindowHandle.ToInt32() != 0)
                    {
                        hwnd = process.MainWindowHandle;
                        break;
                    }
                }
                Console.Out.WriteLine("windowHandle: " + hwnd);

                MoveWindow(hwnd, screen.WorkingArea.X, screen.WorkingArea.Y, screen.WorkingArea.Width, screen.WorkingArea.Height, true);
                ShowWindow(hwnd, SwMaximize);
                SetForegroundWindow(hwnd);
                // Waits here for the process to exit.
                process.WaitForExit();
                return;
            }
        }
        Console.Out.WriteLine("screen not found: " + args[0]);
    }
}