electron把窗口设置为全屏很简单,只需要在创建窗口的时候加一个属性fullscreen,把fullscreen属性设置为true。
fullscreen设置为true后,如果还有width、height、x、y这些属性,系统会自动忽略这些属性。
例子如下:
const {app,BrowserWindow} = require('electron')
function createWindow(){
win = new BrowserWindow({
fullscreen:true,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});
function createWindow(){
win = new BrowserWindow({
fullscreen:true,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});
除了可以在创建窗口的时候,通过fullscreen属性设置全屏外,还可以通过setFullScreen()方法把窗口设置为全屏,isFullScreen()可以判断当前窗口是否为全屏。
例子如下:
const {app,BrowserWindow} = require('electron')
function createWindow(){
win = new BrowserWindow({
width:800,
height:600,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('index.html')
//如果窗口不是全屏,就设置为全屏
if(!win.isFullScreen()){
win.setFullScreen(true)
}
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});
function createWindow(){
win = new BrowserWindow({
width:800,
height:600,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile('index.html')
//如果窗口不是全屏,就设置为全屏
if(!win.isFullScreen()){
win.setFullScreen(true)
}
win.on('closed',()=>{
console.log('closed');
win = null;
});
}
app.allowRendererProcessReuse = true;
app.on('ready',createWindow);
app.on('window-all-closed',()=>{
console.log('window-all-closed');
if(process.platform != 'darwin'){
app.quit();
}
});
app.on('activate',()=>{
console.log('activate');
if(win == null) {
createWindow();
}
});