Atom Electron - 使用 javascript 关闭窗口

IT技术 javascript electron
2021-02-25 14:29:10

我使用Electron(原原子壳层),并希望能有一个最低限度的框架窗口使三个OSX窗口按钮(关闭,最大化,最小化)来自可见范围内的HTML页面。

我设置了电子选项framefalse定义时BrowserWindow有一个边框,无框窗。

我想我可以用这样的东西来处理关闭按钮:

<a btn href="#" id="close" onclick="window.top.close(); return false"></a>

没有运气,可悲。知道如何实现这一目标吗?

3个回答

你必须访问您的主要过程所创建的BrowserWindow对象,并调用minimizemaximize以及close对方法。您可以使用该remotemodule访问它这是绑定所有三个按钮的示例:

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

假设你的最小,最大,关闭按钮有IDS min-btnmax-btnclose-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-visual-studio外壳您的问题与一些 css 一起涵盖,以正确定位按钮。

对于其他努力使按钮工作的人,请查看此线程
2021-05-11 14:29:10
我没有关闭按钮上的拖动效果,因此当我点击它们时它们没有反应。感谢您的精彩教程!
2021-05-16 14:29:10
注意; 如果您的窗口有 'resizable: false' 选项,则单击 max-btn 后,您无法将窗口恢复到其原始大小。github.com/electron/electron/issues/11451
2021-05-19 14:29:10

我已经声明了我的窗口:

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>

除非您隐藏窗口框架,否则会自动设置最小/最大/恢复内容。在这种情况下,这是一个简单的演示仓库: