在第二台显示器上用 node.js 启动外部 exe
Start external exe with node.js on second monitor
您知道是否可以在特定显示器上启动软件吗?以及如何做到这一点?
我的计算机上有一个 node.js 服务器 (windows 10),我需要启动一些外部软件(我无法更改该外部软件中的代码,因为我只有exe文件)。最终应用程序应该在具有真实监视器和第二个假监视器的设置上运行。在第一个显示器上是全屏浏览器 window。有一个启动和停止按钮(它们已经工作)来启动和停止外部软件。在第二台显示器(不可见)上应启动外部软件。
如果一切正常,我可以连接那台计算机上的每个遥控器并查看第二个屏幕。在那里我可以使用外部软件。并且在那台计算机的监视器 (1) 上始终只有浏览器 window 可见。
为了测试,我使用 notepad.exe 作为外部软件。
如果我单击开始,软件会在我的主显示器 (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
,有了它,就可以在你想要的屏幕上设置 window。
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 服务器 (windows 10),我需要启动一些外部软件(我无法更改该外部软件中的代码,因为我只有exe文件)。最终应用程序应该在具有真实监视器和第二个假监视器的设置上运行。在第一个显示器上是全屏浏览器 window。有一个启动和停止按钮(它们已经工作)来启动和停止外部软件。在第二台显示器(不可见)上应启动外部软件。 如果一切正常,我可以连接那台计算机上的每个遥控器并查看第二个屏幕。在那里我可以使用外部软件。并且在那台计算机的监视器 (1) 上始终只有浏览器 window 可见。
为了测试,我使用 notepad.exe 作为外部软件。 如果我单击开始,软件会在我的主显示器 (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
,有了它,就可以在你想要的屏幕上设置 window。
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]);
}
}