关闭点击事件的电子应用程序

时间:2017-10-09 19:03:54

标签: javascript electron desktop-application atom-editor

我查看了Electron关于“无框架窗口”的文档,但我似乎无法按下自己的工作来关闭应用程序...

任何帮助将不胜感激!谢谢!



const electron = require('electron');
const url = require('url');
const path = require('path');

const {app, BrowserWindow} = electron;
let mainWindow;

// Listen for app to be ready
app.on('ready', function() {
  // create new window
  mainWindow = new BrowserWindow({width: 800, height: 600, frame: false});
  // Load html into window
  mainWindow.loadURL(url.format({
    pathname:path.join(__dirname,'main.html'),
    protocol: 'file:',
    slashes: true
  }));
  const closeApp = document.getElementById('closeApp');

  closeApp.addEventListener('click', () => {
    app.quit();
  });
});

<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Electron "Hello World"</title>
</head>
<body>
  <header>
    <p id="closeApp">close app</p>
  </header>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:8)

在渲染器过程中(从main.html加载的javascript),您应该能够加载电子和节点模块。

const {ipcRenderer} = require('electron');
const closeApp = document.getElementById('closeApp');
closeApp.addEventListener('click', () => {
    ipcRenderer.send('close-me')
});

main.js 您发布的脚本

const {ipcMain} = require('electron')
ipcMain.on('close-me', (evt, arg) => {
  app.quit()
})