电子 - 从渲染器进程调整窗口大小

时间:2017-12-03 22:57:33

标签: javascript resize electron

我正在开发一个Electron应用程序,作为界面的一部分,我希望一旦发生某些事情就增加窗口的大小(我现在将其绑定到一个按钮),这样我就可以显示其他数据。我尝试使用在onclick=resize()上激活的以下代码执行此操作:

require('./renderer.js');

let remote = require('electron').remote;
function resize() {
    let win = remote.getCurrentWindow().setBounds({
        height: 1000
    });
}

但是,我在窗口/浏览器控制台中收到以下错误:

Uncaught Error: Could not call remote function 'setBounds'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
Error: Could not call remote function 'setBounds'. Check that the function signature is correct. Underlying error: Error processing argument at index 0, conversion failure from #<Object>
    at callFunction (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:257:11)
    at EventEmitter.<anonymous> (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:357:5)
    at emitMany (events.js:127:13)
    at EventEmitter.emit (events.js:204:7)
    at WebContents.<anonymous> (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:256:13)
    at emitTwo (events.js:106:13)
    at WebContents.emit (events.js:194:7)
    at callFunction (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:257:11)
    at EventEmitter.<anonymous> (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\rpc-server.js:357:5)
    at emitMany (events.js:127:13)
    at EventEmitter.emit (events.js:204:7)
    at WebContents.<anonymous> (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\browser\api\web-contents.js:256:13)
    at emitTwo (events.js:106:13)
    at WebContents.emit (events.js:194:7)
    at metaToValue (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:234:13)
    at Object.remoteMemberFunction (C:\Users\Thomas\AppData\Roaming\npm\node_modules\electron\dist\resources\electron.asar\renderer\api\remote.js:118:18)
    at resize (file:///D:/Documents/Development/Projects/ShortenMeURL/V1/index.html:41:45)
    at HTMLButtonElement.onclick (file:///D:/Documents/Development/Projects/ShortenMeURL/V1/index.html:22:86)

有关如何解决此问题的任何建议?

3 个答案:

答案 0 :(得分:1)

只需使用渲染器中的普通旧javascript,如果只是调整大小,我就不会在main和渲染器进程之间添加不必要的消息;)

window.resizeTo(1000,900);

答案 1 :(得分:1)

Rectangle对象的定义(这是setBounds的第一个参数)比你预期的更严格。由于其属性没有默认值,因此您必须定义所有属性。

例如:

remote.getCurrentWindow().setBounds({
    x: 1621,
    y: 611,
    width: 10,
    height: 1000
});

此外,如果您只想调整尺寸,可以使用BrowserWindow的setSize

答案 2 :(得分:0)

如果有人遇到类似的错误,请确保提供整数值,例如123,而不是123,45Math.round确定。

可悲的是,我在文档中没有提到这一点。