BrowserWindow - win.setIcon(icon)
概述
设置窗口标题栏和任务栏的图标。
基本信息
| 属性 |
值 |
| 模块 |
BrowserWindow |
| 类型 |
Method |
| 鸿蒙支持 |
支持 |
OHOS 依赖与基本表现
| 属性 |
值 |
| 系统权限依赖 |
无需申请 |
| 主窗 |
支持 |
| 子窗 |
不适用 |
| 悬浮窗 |
不适用 |
| 添加JIT权限 |
支持 |
| 坚盾模式 |
支持 |
差异说明
| 属性 |
值 |
| 是否存在差异 |
存在差异 |
| 差异说明 |
setIcon 鸿蒙下应用图标主要由 hap 资源决定,运行时改窗口图标的生效位置/范围与桌面端不同 |
Demo
const { app, BrowserWindow, ipcMain, nativeImage } = require('electron');
const path = require('path');
let mainWindow = null;
let childWindow = null;
function sendLog(type, message) {
if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.webContents.send('log-message', { type, message, time: new Date().toLocaleTimeString() });
}
}
app.whenReady().then(() => {
mainWindow = new BrowserWindow({
width: 1400, height: 900,
webPreferences: { preload: path.join(__dirname, '..', 'preload.js'), contextIsolation: true, nodeIntegration: false },
title: 'Test setIcon',
});
mainWindow.loadFile(path.join(__dirname, '..', 'index.html'));
ipcMain.handle('method:setIcon', () => {
try { mainWindow.setIcon(nativeImage.createEmpty()); return 'setIcon() called with empty image'; } catch (e) { return { error: e.message }; }
});
});
app.on('window-all-closed', () => { app.quit(); });