Electron 建立一個空白的界面

添加應用

首先添加一個Lorikeet版本的Electron應用。html

 1 'use strict'
 2 const electron = require('electron');
 3 const app = electron.app;
 4 const BrowserWindow = electron.BrowserWindow;
 5 
 6 let mainWindow = null;
 7 
 8 app.on('window-all-closed', () => {
 9     if (process.platform !== 'darwin') {
10         app.quit();
11     }
12 });
13 
14 app.on('ready', () => {
15     mainWindow = new BrowserWindow();
16     mainWindow.loadURL(`file://${app.getAppPath()}/index.html`);
17     mainWindow.on('closed', () => {
18         mainWindow = null;
19     });
20 });

 

菜單設置

 1. 永久隱藏菜單

const electronMenu=electron.Menu;
electronMenu.setApplicationMenu(null);
另:也能夠直接設置mainWindow.setMenu(null);
 1 'use strict'
 2 const electron = require('electron');
 3 const app = electron.app;
 4 const BrowserWindow = electron.BrowserWindow;
 5 const electronMenu=electron.Menu;
 6 
 7 let mainWindow = null;
 8 
 9 app.on('window-all-closed', () => {
10     if (process.platform !== 'darwin') {
11         app.quit();
12     }
13 });
14 
15 app.on('ready', () => {
16     electronMenu.setApplicationMenu(null);
17     mainWindow = new BrowserWindow();
18     mainWindow.loadURL(`file://${app.getAppPath()}/index.html`);
19     mainWindow.on('closed', () => {
20         mainWindow = null;
21     });
22 });
View Code

2. 自動隱藏菜單

設置BrowserWindow的屬性,autoHideMenuBar: true。
設置autoHideMenuBar隱藏後,按Alt,可顯示出菜單選項。
 1 'use strict'
 2 const electron = require('electron');
 3 const app = electron.app;
 4 const BrowserWindow = electron.BrowserWindow;
 5 
 6 let mainWindow = null;
 7 
 8 app.on('window-all-closed', () => {
 9     if (process.platform !== 'darwin') {
10         app.quit();
11     }
12 });
13 
14 app.on('ready', () => {
15     mainWindow = new BrowserWindow({autoHideMenuBar:true});
16     mainWindow.loadURL(`file://${app.getAppPath()}/index.html`);
17     mainWindow.on('closed', () => {
18         mainWindow = null;
19     });
20 });
View Code

另:自定義菜單設置,可參考http://www.javashuo.com/article/p-nyqajwms-bd.htmlgit

窗口設置

 直接設置Window的屬性github

mainWindow = new BrowserWindow({width:600,height:400,icon: 'images/myIcon.ico'});
另:若是須要隱藏window的標題欄,能夠設置frame:false;

圖上:Demosegmentfault

 

 注:GitHub官方文檔與Demo: electron-api-demos-Zh_CNapi

相關文章
相關標籤/搜索