index.js
/** * 單位:像素 * width:窗口寬度 height:窗口高度 * minWidth:窗口最小寬度 minHeight:窗口最小高度 * maxWidth:窗口最大寬度 maxHeight:窗口最大高度 * * 獲取和設置高度 * getSize() 返回數組,[0]:width [1]:height * setSize(width,height,flag) flag:為true時顯示動畫,僅限於Mac OS X * 獲取和設置坐標 * getPosition() 返回數組,[0]:x [1]:y * setPosition(x,y,flag) flag:為true時顯示動畫,僅限於Mac OS X */ /** * 全屏窗口 * fullscreen:true * 如果設置了width、heigh、x、y,系統會忽略這些屬性,仍然是全屏顯示 * maxWidth、maxHeight * fullscreenable:false,在Mac OS X會阻止單擊最大化按鈕隱藏菜單 * win.setFullScreen(true);方法可以動態將窗口設置為全屏狀態,但fullscreenenable屬性必須是true * 通過win.isFullScreen();方法可以獲取窗口是否為全屏 */ const {app,BrowserWindow} = require('electron'); function createWindow(){ win = new BrowserWindow({ //fullscreen:true, //fullscreenable:false, width: 800, height: 600,minWidth: 400, minHeight: 300, //maxWidth: 1000, maxHeight: 700, //,transparent: false, frame: true, show: false webPreferences:{ nodeIntegration: true, // 是否集成 Nodejs enableRemoteModule: true, contextIsolation: false, //,preload:path.join(__dirname,'index.js') } }); win.loadFile('index.html'); // win.setFullScreen(true); // win.isFullScreen(); win.on('closed',()=>{ console.log('closed') win=null; }); } app.on('ready',createWindow); app.on('window-all-closed',()=>{ console.log('window-all-closed'); if(process.platform!='darwin'){ } }); app.on('activate',()=>{ console.log('activate'); if(win==null){ createWindow(); } });
index.html

<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>窗口大小</title> <script src="event.js"></script> <body> <img src="./images/shj8.jpg"> <h1>書名:<i>山海經</i></h1> <br/> <br/> 出版社:<u>大地出版社</u> <br/> <br/> 原價:<strike>69</strike>元 促銷價:59 <br/> <br/> <button onclick="onClick_GetSizePosition()">獲得窗口大小和位置</button> <br/> <br/> <button onclick="onClick_SetSizePosition()">設置窗口大小和位置</button> </body> </html>
event.js

const remote = window.require('electron').remote; function onClick_GetSizePosition() { const win = remote.getCurrentWindow(); console.log("寬度:",win.getSize()[0]); console.log("高度:",win.getSize()[1]); console.log("X:",win.getPosition()[0]); console.log("Y:",win.getPosition()[1]); } function onClick_SetSizePosition() { const win = remote.getCurrentWindow(); win.setSize(400,400); win.setPosition(12,30); }