electron点右上角的关闭按钮,怎么实现隐藏到托盘?

托盘已经做好,并且能在显示器的右下角显示,现在就是不知道点击右上角的红色小叉叉,怎么让窗口隐藏?而且要求双击托盘里的图标后,窗口显示出来
图片描述

应该操作的是app呢还是mainWindow呢?

阅读 13.3k
4 个回答

已实现,使用Electron ipcMain 模块可以做到

求助整个流程是怎么实现的,可以详细讲解下嘛,我只简单实现主页面点击关闭按钮,我的托盘也不知道怎么实现。
app.on('window-all-closed', function(){

// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
    app.quit();
}

});

文档里面有写.

electron/docs/api/browser-window.md

Event: 'close'

Returns:

event Event
Emitted when the window is going to be closed. It's emitted before the beforeunload and unload event of the DOM. Calling event.preventDefault() will cancel the close.

我尝试如下可以解决问题:

// let mainWindow: Electron.BrowserWindow;
mainWindow.on("close", (e:Event) => {
    e.preventDefault();
    mainWindow.hide();
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    // mainWindow = null;
});
// 展示使用 mainWindow.show();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进