![](/img/trans.png)
[英]Atom Electron Close and Minimize window using the html button and javascript
[英]Atom Electron - Close the window with javascript
我使用的是Electron
(原原子壳层),并希望能有一个最低限度的框架窗口使三个OSX窗口按钮(关闭,最大化,最小化)从HTML页面中可见。
在将BrowserWindow
定义为具有无边框、无框架窗口时,我将 Electron 选项frame
设置为false
。
我想我可以用这样的东西来处理关闭按钮:
<a btn href="#" id="close" onclick="window.top.close(); return false"></a>
没有运气,可悲。 知道如何实现这一目标吗?
您必须访问由主进程创建的 BrowserWindow 对象,并在其上调用minimize
、 maximize
和close
方法。 您可以使用remote
模块访问它。 这是绑定所有三个按钮的示例:
const remote = require('electron').remote;
document.getElementById("min-btn").addEventListener("click", function (e) {
var window = remote.getCurrentWindow();
window.minimize();
});
document.getElementById("max-btn").addEventListener("click", function (e) {
var window = remote.getCurrentWindow();
if (!window.isMaximized()) {
window.maximize();
} else {
window.unmaximize();
}
});
document.getElementById("close-btn").addEventListener("click", function (e) {
var window = remote.getCurrentWindow();
window.close();
});
假设您的 min、max、close 按钮的 ID 分别为min-btn
、 max-btn
和close-btn
。
您可以在此处查看 BrowserWindow 的完整文档以及您可能需要的其他功能: http ://electron.atom.io/docs/v0.28.0/api/browser-window/。
它还可以帮助您查看我写的关于构建一个看起来像 Visual Studio 的无边框窗口的教程: http : //www.mylifeforthecode.com/making-the-electron-shell-as-pretty-as-视觉工作室外壳。 您的问题与一些 css 一起涵盖,以正确定位按钮。
我已经声明了我的窗口:
const electron = require('electron')
const path = require('path')
const BrowserWindow = electron.remote.BrowserWindow
const notifyBtn = document.getElementById('notifyBtn')
notifyBtn.addEventListener('click',function(event){
const modalPath = path.join('file://', __dirname,'add.html')
let win = new BrowserWindow({ webPreferences: {nodeIntegration: true}, frame: false, transparent: true, alwaysOnTop:true, width: 400, height: 200 })
win.on('close',function(){win = null})
win.loadURL(modalPath)
win.show()
})
并关闭此:
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();
})
用另一种技术来回答这个问题。
与 Electron 相比,您想要在NW.js 中做的事情非常简单(总是如此)。
<a href="#" onclick="nw.Window.get().close()"></a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.