Keeping track of window size in electron

佐手、 提交于 2019-12-13 07:37:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!