问题
I'm just starting to play with Electron today. I need to be able to get the available window size, and to update it on the window resize.
It seems that this isn't as simple as it is in a conventional JS app. What's the recommended way for keeping track of the window size?
At the moment, I have my main process and a single renderer, with no plans to have more than 1 renderer/window open at a time.
I have tried to use the following, but it seems completely wrong so I must have misunderstood the docs.
const {BrowserWindow} = require('electron').remote
BrowserWindow.getSize()
EDIT:
Is it reasonable to keep track of the height by watching the body of the app? I can set this to 100% width/height and watch it, but it seems a bit of a hack.
Thanks Tom
回答1:
you can try
const electron = require('electron')
const remote = electron.remote
remote.getCurrentWindow().webContents.getOwnerBrowserWindow().getBounds()
Bounds will have co-ordinate and size of current window,
{
height: 1040,
width : 837,
x : 276,
y : 78
}
回答2:
This works in v4.0.4:
in renderer.js:
import { remote } from "electron";
console.log('size:', remote.getCurrentWindow().getSize());
// size: [1000, 700]
console.log('bounds:', remote.getCurrentWindow().getBounds());
// bounds: {height: 700, width: 1000, x: 226, y: 97}
in main.js:
import { BrowserWindow } from "electron";
let mainWindow: BrowserWindow
// ...after mainWindow is created
console.log('size:', mainWindow.getSize());
console.log('bounds:', mainWindow.getBounds());
来源:https://stackoverflow.com/questions/44005729/keeping-track-of-window-size-in-electron