node 启动本地应用程序并设置窗口大小和屏幕显示位置

前言

node 启动windows应用程序是没有问题的,但是设置窗口大小和屏幕显示位置是无法完成的。 我这里用到的是exec 执行powershell。

node 代码

应用程序如果设置了最小窗口宽度限制,则会触发应用程序的宽度,就会设置失败, 句柄一定要是对的。

js 复制代码
const { exec } = require("child_process");
const path = require("path");
// 直接调用
// 执行 PowerShell 脚本,传入参数
// scriptPath 下面创建的.ps1文件
const scriptPath = const fullPath = path.resolve(__dirname, './openwindows.ps1')
// programName exe 文件路径
const scriptPath = "D:\softwarellApifoxApifox.exe"
// windowTitle 启动程序的句柄
const windowTitle = "Apifox"
// x y 窗口放置的位置,width height 设置窗口的宽高
exec(
    `powershell -ExecutionPolicy Bypass -File "${scriptPath}" -programName "${programName}" -windowTitle "${windowTitle}" -x ${x} -y ${y} -width ${width} -height ${height}`,
    (err, stdout, stderr) => {
      console.log(err, stdout, stderr);
      if (err) {
        console.error("执行失败:", err);
      } else {
        console.log("窗口已移动");
      }
    }
  );

创建.ps1文件

powershell 复制代码
param(
    [string]$programName = "notepad.exe",   # 默认程序名称
    [string]$windowTitle = "无标题 - 记事本", # 默认窗口标题
    [int]$x = 300,                          # 默认 X 坐标
    [int]$y = 200,                          # 默认 Y 坐标
    [int]$width = 800,                      # 默认宽度
    [int]$height = 600                      # 默认高度
)

# 启动指定程序
$proc = Start-Process -FilePath $programName -PassThru

# 等待窗口打开
Start-Sleep -Seconds 3

# 加载 user32.dll 函数(修正后的 C# 代码)
$cSharpCode = @'
using System;
using System.Runtime.InteropServices;

public class Win32 {
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
'@

Add-Type -TypeDefinition $cSharpCode -Language CSharp

$process =  Get-Process -Name $windowTitle | Where-Object { $_.MainWindowHandle -ne 0 }

if ($process) {
	Write-Host 'error2'
    $hwnd = $process.MainWindowHandle
    if ($hwnd -eq 0) {
        Write-Host "进程存在,但无主窗口(可能是后台运行或最小化)。"
    } else {
        Write-Host "窗口句柄: $hwnd"
    }
} else {
    Write-Host "未找到进程 '$processName'。"
}
# 3. 最终检查
if ($hwnd -eq [IntPtr]::Zero) {
	Write-Host 'error'
	
    Write-Host "错误:未找到窗口 '$windowTitle' 或进程 '$processName'。"
    Write-Host "请检查:"
    Write-Host "1. 窗口标题是否匹配(当前标题:$(Get-Process | Where-Object { $_.MainWindowTitle -like $windowTitle } | Select-Object -ExpandProperty MainWindowTitle))"
    Write-Host "2. 是否以管理员身份运行脚本"
    exit
}

if ($hwnd -ne [IntPtr]::Zero) {
    # 设置位置与大小:x, y, width, height
    [Win32]::MoveWindow($hwnd, $x, $y, $width, $height, $true)
    Write-Host "窗口已调整位置和大小"
} else {
    Write-Host "未找到窗口:$windowTitle"
}
相关推荐
Uyker13 分钟前
空间利用率提升90%!小程序侧边导航设计与高级交互实现
前端·微信小程序·小程序
bin915322 分钟前
DeepSeek 助力 Vue3 开发:打造丝滑的日历(Calendar),日历_天气预报日历示例(CalendarView01_18)
前端·javascript·vue.js·ecmascript·deepseek
江城开朗的豌豆23 分钟前
JavaScript篇:反柯里化:让函数'反悔'自己的特异功能,回归普通生活!
前端·javascript·面试
江城开朗的豌豆30 分钟前
JavaScript篇:数字千分位格式化:从入门到花式炫技
前端·javascript·面试
henujolly2 小时前
网络资源缓存
前端
yuren_xia5 小时前
Spring Boot中保存前端上传的图片
前端·spring boot·后端
普通网友6 小时前
Web前端常用面试题,九年程序人生 工作总结,Web开发必看
前端·程序人生·职场和发展
站在风口的猪11087 小时前
《前端面试题:CSS对浏览器兼容性》
前端·css·html·css3·html5
青莳吖9 小时前
使用 SseEmitter 实现 Spring Boot 后端的流式传输和前端的数据接收
前端·spring boot·后端
CodeCraft Studio9 小时前
PDF处理控件Aspose.PDF教程:在 C# 中更改 PDF 页面大小
前端·pdf·c#