【问题标题】:Start external exe with node.js on second monitor在第二台显示器上使用 node.js 启动外部 exe
【发布时间】:2017-05-18 15:13:36
【问题描述】:

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

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

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

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    看起来,直接这样做是不可能的。但是如果你从 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]);
        }
    }
    

    【讨论】:

    • 问题是针对使用 node.JS 的解决方案。
    • @PaulT。这是我自己的问题,答案是我的解决方法。如果你有只使用 node.js 的解决方案,如果你分享它会很棒。
    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多