一、安裝nodejsjavascript
二、用npm全局安裝electron(能夠不全局安裝,後面在項目中安裝)html
三、建立一個目錄my_app,下面建立3個文件:package.json、main.js、index.htmljava
四、編緝index.html內容:node
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> We are using node <script>document.write(process.versions.node)</script>, Chrome <script>document.write(process.versions.chrome)</script>, and Electron <script>document.write(process.versions.electron)</script>. </body> </html>
五、編緝main.js內容:web
const {app, BrowserWindow,globalShortcut,ipcMain} = require('electron') const path = require('path') const url = require('url') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let win function createWindow () { // Create the browser window. win = new BrowserWindow({width: 800, height: 600,resizable:false}) //hide menubar win.setAutoHideMenuBar(true) // and load the index.html of the app. win.loadURL('file://'+__dirname+'/index.html') // Open the DevTools. //win.webContents.openDevTools() // Emitted when the window is closed. win.on('closed', () => { // Dereference the window object, usually you would store windows // in an array if your app supports multi windows, this is the time // when you should delete the corresponding element. win = null }) } // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.on('ready', createWindow) // Quit when all windows are closed. app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { app.quit() } }) app.on('activate', () => { // On macOS it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open. if (win === null) { createWindow() } }) // In this file you can include the rest of your app's specific main process // code. You can also put them in separate files and require them here.
六、編緝package.json內容:chrome
{ "name": "demo_app", "version": "0.1.0", "main": "main.js", }
七、運行npm
在demo_app目錄下,執行electron .json
若是沒有全局安裝electron,能夠在目錄下執行npm install --save-dev electron. 而後在執行在demo_app/node_modules/.bin/目錄下執行electron ../../windows
--save-dev會在package.json中加入一行:app
"devDependencies": {
"electron": "^1.4.15"
}
八、打包
使用electron-package進行打包,生成目錄demo_app_release。
electron-packager ./demo_app --platform=win32 --out ../demo_app_release --version 1.4.13 --overwrite --icon=./images/app.ico
九、腳本與依賴
修改package.json:
{ "name": "demo_app", "version": "0.1.0", "main": "main.js", "scripts": { "start": "electron .", "package": "electron-packager ./ demo_app --platform=win32 --out ../demo_app_release --version 1.4.13 --overwrite --icon=./images/app.ico" }, "author": "gelare", "devDependencies": { "electron": "^1.4.15" }, "dependencies": { "log4js": "^1.1.0" } }
在demo_app目錄下
執行npm install會安裝依賴,包括electron與log4js。
執行npm start會運行程序。
執行npm run-script package進行打包。
十、快捷鍵
添加ctrl+q,退出程序,在createWindow函數最後加上:
globalShortcut.unregisterAll(); globalShortcut.register('ctrl+q',function(){ app.quit(); });
註冊快捷鍵向主頁面發送事件:
globalShortcut.register('alt+c',()=>{ win.webContents.send('global-shortcut','change'); });
接收與處理事件:
修改index.html,引入腳本:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> We are using node <script>document.write(process.versions.node)</script>, Chrome <script>document.write(process.versions.chrome)</script>, and Electron <script>document.write(process.versions.electron)</script>. <script src="index.js"></script> </body> </html>
增長index.js:
const { ipcRenderer, desktopCapturer, } = require('electron'); ipcRenderer.on('global-shortcut',(event,arg) => { switch(arg) { case 'change': var body = document.querySelector('body'); body.style.backgroundColor = "lightblue"; break; } })
十一、菜單