久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Electron 應(yīng)用程序中捕獲單擊應(yīng)用程序窗口

How to catch the event of clicking the app window#39;s close button in Electron app(如何在 Electron 應(yīng)用程序中捕獲單擊應(yīng)用程序窗口關(guān)閉按鈕的事件)
本文介紹了如何在 Electron 應(yīng)用程序中捕獲單擊應(yīng)用程序窗口關(guān)閉按鈕的事件的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我希望在 Electron 應(yīng)用中捕捉點(diǎn)擊應(yīng)用窗口關(guān)閉按鈕的事件.

I wish to catch the event of clicking the app window's close button in Electron app.

我正在嘗試為 Mac OSX 開發(fā) Electron 應(yīng)用程序.我想隱藏應(yīng)用程序窗口,而不是像其他 Mac 應(yīng)用程序一樣在用戶單擊窗口的關(guān)閉按鈕時(shí)終止應(yīng)用程序.

I'm trying to develope Electron app for Mac OSX. I want to hide the app window, not to terminate the app when a user clicks the window's close button like other Mac apps.

但是,我無法檢測到系統(tǒng)應(yīng)該終止還是應(yīng)該隱藏,因?yàn)樵谌魏吻闆r下,browser-windowclose 事件都會(huì)被調(diào)用單擊關(guān)閉按鈕,關(guān)閉操作系統(tǒng)或使用退出命令終止應(yīng)用程序,Cmd+Q.

However, I can not detect wether the system should be terminated or it should be hidden, because in any case, a close event of browser-window is called when a close button is clicked, the OS is shut down or the app is terminated with quit command, Cmd+Q.

有什么方法可以捕捉到在 Electron 應(yīng)用中點(diǎn)擊應(yīng)用窗口關(guān)閉按鈕的事件?

Is there any way to catch the event of clicking the app window's close button in Electron app?

感謝您的幫助.

為了檢測點(diǎn)擊關(guān)閉按鈕的事件,我嘗試了這段代碼

To detect the event of clicking a close button, I tried this code

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

    mainWindow.on('close', function(e){
        if(!force_quit){
            e.preventDefault();
            mainWindow.hide();
        }
    });

    mainWindow.on('closed', function(){
        console.log("closed");
        mainWindow = null;
        app.quit();
    });

    app.on('activate-with-no-open-windows', function(){
        mainWindow.show();
    });
});

使用此代碼,當(dāng)單擊應(yīng)用程序窗口的關(guān)閉按鈕時(shí)應(yīng)用程序被隱藏,而當(dāng)鍵入 Cmd+Q 時(shí)應(yīng)用程序被終止.但是,當(dāng)我嘗試關(guān)閉操作系統(tǒng)時(shí),會(huì)阻止關(guān)閉事件.

With this code, the app is hidden when a close button of the app window is clicked, and the app is terminated when Cmd+Q is typed. However, when I try to shut down the OS, the shutdown event is prevented.

推薦答案

你可以使用 close 事件來捕捉它電子/blob/master/docs/api/browser-window.md#event-close" rel="noreferrer">瀏覽器窗口 api.您可以嘗試以下方法來驗(yàn)證這一點(diǎn)...

You can catch it by using the close event of the browser-window api. You can try the following to verify this...

var app = require('app');

var force_quit = false;

app.on('ready', function () {        
    mainWindow = new BrowserWindow({ width: 800, height: 600 });

    mainWindow.on('close', function() { //   <---- Catch close event

        // The dialog box below will open, instead of your app closing.
        require('dialog').showMessageBox({
            message: "Close button has been pressed!",
            buttons: ["OK"]
        });
    });
});

更新:

要分離功能,您可以執(zhí)行以下操作...

Update:

To separate functionality you can do the following...

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 事件處理程序 處理應(yīng)用程序 api 上的應(yīng)用程序關(guān)閉"事件.瀏覽器窗口關(guān)閉"事件仍由 mainWindow.on('close') 在瀏覽器窗口 api 上處理.

The above code uses the before-quit event handler to handle app "close" events on the app api. Browser-window "close" events are still handled on the browser-window api by mainWindow.on('close').

此外,will-quit 事件是在應(yīng)用完全關(guān)閉之前測試問題的更好地方.

Additionally, the will-quit event is a better place to test for problems before the app closes completely.

這篇關(guān)于如何在 Electron 應(yīng)用程序中捕獲單擊應(yīng)用程序窗口關(guān)閉按鈕的事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進(jìn)程中創(chuàng)建子窗口時(shí)如何修復(fù) BrowserWindow 不是構(gòu)造函數(shù)錯(cuò)誤) - IT屋-程序員軟件開發(fā)技術(shù)
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應(yīng)用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個(gè) loadURL 上執(zhí)行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內(nèi)使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲(chǔ)?)
How to access camera/webcamera inside electron app?(如何在電子應(yīng)用程序中訪問相機(jī)/網(wǎng)絡(luò)攝像頭?)
主站蜘蛛池模板: 四虎永久在线精品免费一区二 | 在线国产中文字幕 | 亚洲热在线视频 | 自拍偷拍亚洲视频 | 亚洲视频在线观看 | 黑人粗黑大躁护士 | 久久亚洲国产 | 天天躁日日躁狠狠很躁 | 日韩在线国产精品 | 一区二区三区久久 | 情侣黄网站免费看 | 日韩精品极品视频在线观看免费 | 在线观看视频一区二区三区 | 国产欧美精品一区 | 午夜欧美一区二区三区在线播放 | .国产精品成人自产拍在线观看6 | 亚洲国产精品va在线看黑人 | 高清久久久 | 日日射影院 | 精品国产一区三区 | 国产福利91精品 | 成人一区在线观看 | 色接久久 | 免费美女网站 | 麻豆91精品91久久久 | 在线视频第一页 | 亚洲精品99 | 久久午夜精品福利一区二区 | 中文天堂在线观看 | 午夜一区二区三区在线观看 | 一级黄a视频 | 久久99深爱久久99精品 | 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 国产精品国产三级国产aⅴ无密码 | 四虎成人精品永久免费av九九 | 久久亚洲一区 | 黄色片大全在线观看 | 国产精品99久久久久久大便 | 91精品国产一区二区三区 | 亚洲精品久久久久久久久久久 | 日韩在线播放av |