Electron窗口首次显示为白色而非透明,点击任务栏预览才恢复的问题
解决Electron透明窗口首次显示白色背景的问题
针对你遇到的全屏透明窗口首次显示白背景、需点击任务栏预览才恢复正常的问题,试试以下几种解决办法:
明确设置窗口透明背景色
Electron默认窗口背景色为白色,即使开启transparent: true,也可能残留渲染痕迹。在窗口配置里添加全透明背景色:const borderWindow = new BrowserWindow({ width: screen.width, height: screen.height, x: 0, y: 0, webPreferences: { nodeIntegration: true }, frame: false, resizable: false, alwaysOnTop: true, hasShadow: false, parent: timerWindow, transparent: true, skipTaskbar: true, show: false, backgroundColor: '#00000000', // 添加上这行,全透明十六进制值 //fullscreen: true, /*this makes it so the border is around the whole screen, not above the taskbar*/ });在页面加载完成后显示并触发重绘
ready-to-show事件可能早于页面透明内容的渲染完成,改用did-finish-load事件,并通过重置窗口尺寸强制系统重绘:// 替换原有的ready-to-show事件监听 borderWindow.webContents.on('did-finish-load', () => { borderWindow.show(); // 重置尺寸触发系统重绘,修复透明异常 borderWindow.setSize(screen.width, screen.height); });临时隐藏再显示窗口强制刷新
如果前两种方法无效,试试在窗口显示后立刻隐藏再显示,强制系统重新渲染:borderWindow.once('ready-to-show', () => { borderWindow.show(); // 短延迟确保渲染流程走完 setTimeout(() => { borderWindow.hide(); borderWindow.show(); }, 50); });检查父窗口状态
因为你的窗口设置了parent: timerWindow,确保父窗口timerWindow已经完全渲染完成后再创建子窗口,比如把borderWindow的初始化逻辑放在父窗口的ready-to-show事件里,避免父窗口未就绪导致子窗口渲染异常。
内容的提问来源于stack exchange,提问作者stackers
相关产品推荐
相关产品推荐

