首先创建窗口的时候,加一个属性show:false,创建完窗口先让它不显示,然后加载页面,加载完页面再显示窗口。
例子如下:
const {app,BrowserWindow} = require('electron');
function createWindow(){
win = new BrowserWindow({
show:false,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('ready-to-show',()=>{
win.show()
});
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({
show:false,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
win.on('ready-to-show',()=>{
win.show()
});
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();
}
});