You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何通过Electron实现外部应用窗口大小调整?

调整外部应用窗口大小(以MS PowerPoint为例)

方案一:通过PowerShell脚本调用(无需编译Native模块)

这种方法绕开了Native模块构建失败的问题,直接借助Windows PowerShell调用系统API:

  • 步骤:在Electron主进程中用child_process.exec执行PowerShell脚本,完成「获取PowerPoint窗口句柄→调用API调整大小」的流程

示例代码:

const { exec } = require('child_process');

// 调整PowerPoint窗口至800x600,左上角坐标(100,100)
const adjustPowerPointWindow = () => {
  const psScript = `
    Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class Win32 {
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    }
    "@
    // 查找PowerPoint主窗口(类名固定为PPTFrameClass)
    $hwnd = [Win32]::FindWindow("PPTFrameClass", $null)
    if ($hwnd -ne [IntPtr]::Zero) {
        // 0x0014 = SWP_NOZORDER(不改变窗口层级) + SWP_NOACTIVATE(不激活窗口)
        [Win32]::SetWindowPos($hwnd, [IntPtr]::Zero, 100, 100, 800, 600, 0x0014)
        Write-Host "窗口调整成功"
    } else {
        Write-Host "未找到PowerPoint窗口"
    }
  `;

  exec(`powershell -Command "${psScript}"`, (error, stdout, stderr) => {
    if (error) {
      console.error(`执行错误: ${error.message}`);
      return;
    }
    if (stderr) {
      console.error(`错误输出: ${stderr}`);
      return;
    }
    console.log(stdout);
  });
};

// 触发调整
adjustPowerPointWindow();

说明:

  • PPTFrameClass是PowerPoint主窗口的固定类名,可通过Spy++工具验证
  • 若需调整其他应用,替换对应的窗口类名即可

方案二:使用ffi-napi替代node-ffi(Native模块方案)

node-ffi已停止维护,改用兼容Node-API的ffi-napi,配合electron-rebuild解决构建问题:

  1. 安装依赖:
npm install ffi-napi ref-napi
  1. 安装编译工具并重建模块:
npm install --save-dev electron-rebuild
npx electron-rebuild
  1. 主进程代码实现:
const ffi = require('ffi-napi');
const ref = require('ref-napi');

// 定义数据类型
const IntPtr = ref.refType(ref.types.int32);
const user32 = new ffi.Library('user32', {
  'FindWindowA': [IntPtr, ['string', 'string']],
  'SetWindowPos': ['bool', [IntPtr, IntPtr, 'int', 'int', 'int', 'int', 'uint']]
});

const adjustPowerPointWindow = () => {
  // 查找PowerPoint窗口
  const hwnd = user32.FindWindowA('PPTFrameClass', null);
  if (!hwnd.isNull()) {
    const SWP_NOZORDER = 0x0004;
    const SWP_NOACTIVATE = 0x0010;
    // 设置窗口位置与大小
    const success = user32.SetWindowPos(hwnd, null, 100, 100, 800, 600, SWP_NOZORDER | SWP_NOACTIVATE);
    console.log(success ? '窗口调整成功' : '调整失败');
  } else {
    console.log('未找到PowerPoint窗口');
  }
};

adjustPowerPointWindow();

说明:

  • 若构建失败,需确保安装对应Electron版本的编译工具链(Windows下安装Visual Studio Build Tools)
  • 不同版本PowerPoint的窗口类名若有变化,需用Spy++重新确认

注意事项

  • 以上方案仅针对Windows系统,MacOS下可通过osascript调用AppleScript实现类似功能
  • 部分场景下需确保Electron应用拥有管理员权限

内容的提问来源于stack exchange,提问作者Nikunj Chaklasiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 14:20:56