electron设置窗口图标支持windows,不支持mac。
只需要在创建窗口的时候加一个icon属性,就可以设置窗口图标。
注意windows下的目录分隔符是反斜杆,反斜杆在js中是转义符,所以写成\\
例子如下:
const {app,BrowserWindow} = require('electron');
function createWindow(){
win = new BrowserWindow({
icon:'.\\static\\images\\favicon.ico',
width:800,
height:600,
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({
icon:'.\\static\\images\\favicon.ico',
width:800,
height:600,
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();
}
});
除了通过属性的方式来设置,还可以通过setIcon()方法来设置窗口图片
例子如下:
const {app,BrowserWindow} = require('electron');
function createWindow(){
win = new BrowserWindow({
width:800,
height:600,
webPreferences: {
nodeIntegration: true
}
});
win.loadFile('index.html');
if(process.platform != 'darwin'){
//如果不是mac系统,设置窗口图片
win.setIcon('.\\static\\images\\favicon.ico')
}
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(process.platform != 'darwin'){
//如果不是mac系统,设置窗口图片
win.setIcon('.\\static\\images\\favicon.ico')
}
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();
}
});