还有什么其他方法可以关闭电子5.0.1中的无框窗口

时间:2019-05-18 22:08:31

标签: electron

我有一个内部窗口,该窗口是无框架的,我创建了一个关闭窗口的按钮。添加事件监听器后,它不会关闭..它不会返回任何错误

const electron = require('electron')
const path = require('path')
const remote = electron.remote

const closeBtn = document.getElementById('closeBtn')

closeBtn.addEventListener('click', function (event) {
    var window = remote.getCurrentWindow();
    window.close();
})

1 个答案:

答案 0 :(得分:0)

使用ipcRenderer.send()函数将ipc事件发送到主进程

const electron = require('electron')
const path = require('path')
const remote = electron.remote
const ipcRenderer = require('electron').ipcRenderer;
const closeBtn = document.getElementById('closeBtn')

closeBtn.addEventListener('click', function (event) {
       ipcRenderer.send('close-window');
})

在主流程中,捕获关闭事件并关闭所需的窗口

const ipcMain = require('electron').ipcMain;
ipcMain.on('close-window',()=>{

    //if mainWindow is the window object
    mainWindow.close();

})