点击事件关闭 Electron 应用

Close Electron app on click event(点击事件关闭 Electron 应用)
本文介绍了点击事件关闭 Electron 应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经查看了有关无框窗口"的 Electron 文档,但我似乎无法为自己的工作制作一个按钮来关闭应用程序......

I have looked at the documentation for Electron regarding the 'frameless-window', but I just can't seem to make a button of my own work to close the application...

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

Any help would be appreciated! Thanks!

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>

推荐答案

在您的渲染器进程中(从 main.html 加载的 JavaScript)您应该能够加载 Electron 和 Node 模块.

In your renderer process (javascript loaded from main.html) you should be able to load Electron and Node modules.

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()
})

这篇关于点击事件关闭 Electron 应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Google apps script get range of bytes from binary file(谷歌应用程序脚本从二进制文件中获取字节范围)
Sending Multiple attachments with Google Script from Google Drive(使用 Google 脚本从 Google Drive 发送多个附件)
Distributing Google Apps Scripts for Sheets in your company network(在您的公司网络中分发适用于表格的 Google Apps 脚本)
Upload file to my google drive from anyone using javascript(使用 javascript 将文件从任何人上传到我的谷歌驱动器)
quot;Shared Drivequot; support in Google Apps Script(“共享驱动器Google Apps 脚本中的支持)
Angular 2+ HTTP POST and GDrive API. Resumable file upload with name(Angular 2+ HTTP POST 和 GDrive API.带名称的可恢复文件上传)