java关闭按钮事件_如何捕获在Electron应用程序中单击应用程序窗口的关闭按钮的事件...

您可以使用browser-window api的 close 事件来捕获它 . 您可以尝试以下方法来验证这一点......

var app = require('app');

var force_quit = false;

app.on('ready', function () {

mainWindow = new BrowserWindow({ width: 800, height: 600 });

mainWindow.on('close', function() { //

// The dialog box below will open, instead of your app closing.

require('dialog').showMessageBox({

message: "Close button has been pressed!",

buttons: ["OK"]

});

});

});

更新:

要分离功能,您可以执行以下操作...

var app = require('app');

var BrowserWindow = require('browser-window');

var Menu = require('menu');

var force_quit = false;

var menu = Menu.buildFromTemplate([

{

label: 'Sample',

submenu: [

{label: 'About App', selector: 'orderFrontStandardAboutPanel:'},

{

label: 'Quit',

accelerator: 'CmdOrCtrl+Q',

click: function() {

force_quit=true; app.quit();

}

}

]

}]);

app.on('window-all-closed', function(){

if(process.platform != 'darwin')

app.quit();

});

app.on('ready', function(){

Menu.setApplicationMenu(menu);

mainWindow = new BrowserWindow({width:800, height:600});

// Continue to handle mainWindow "close" event here

mainWindow.on('close', function(e){

if(!force_quit){

e.preventDefault();

mainWindow.hide();

}

});

// You can use 'before-quit' instead of (or with) the close event

app.on('before-quit', function (e) {

// Handle menu-item or keyboard shortcut quit here

if(!force_quit){

e.preventDefault();

mainWindow.hide();

}

});

// Remove mainWindow.on('closed'), as it is redundant

app.on('activate-with-no-open-windows', function(){

mainWindow.show();

});

});

// This is another place to handle events after all windows are closed

app.on('will-quit', function () {

// This is a good place to add tests insuring the app is still

// responsive and all windows are closed.

console.log("will-quit");

mainWindow = null;

});

上面的代码使用 before-quit event handler来处理app api上的app "close"事件 . 浏览器窗口"close"事件仍由 mainWindow.on('close') 在浏览器窗口api上处理 .

此外, will-quit 事件是在应用完全关闭之前测试问题的更好地方 .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值