electron 学习
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <meta http-equiv= "X-UA-Compatible" content= "ie=edge" > <title>Document</title> </head> <body> <h1>Hello world !!!!!!!!!!!!!!</h1> </body> <script> document.write( 'node:' ) document.write(process.versions.node) document.write( 'chrome:' ) document.write(process.versions.chrome) document.write( 'electron:' ) document.write(process.versions.electron) console.log(process.versions) </script> </html> |
main.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | const electron = require( 'electron' ) const path = require( 'path' ) const app = electron.app const Menu = electron.Menu const MenuItem = electron.MenuItem const BrowserWindow = electron.BrowserWindow let mainWindow = null app. on ( 'ready' , createWindow) // 当所有的窗口都被关闭时触发。 // 默认的行为是控制退出程序;但如果你监听了此事件,你可以控制是否退出程序。 // 如果用户按下了 Cmd + Q,或者开发者调用了 app.quit(),Electron 会首先关闭所有的窗口然后触发 will-quit 事件,在这种情况下 window-all-closed 事件不会被触发。 // Quit when all windows are closed. app. on ( 'window-all-closed' , function() { if (process.platform !== 'darwin' ) { app.quit() } }) app. on ( 'activate' , () => { // 在macOS上,当单击dock图标并且没有其他窗口打开时, // 通常在应用程序中重新创建一个窗口。 if (win === null ) { createWindow() } }) function createWindow() { if (mainWindow) { mainWindow.show() return } mainWindow = new BrowserWindow({ backgroundColor: '#999' , width: 800, height: 600, x: 100, y: 100, icon: path.__dirname + '/src/asset/icon.ico' // frame: false }) console.log(process.env.GOOGLE_API_KEY) mainWindow.webContents.openDevTools() // mainWindow.loadFile('index.html') mainWindow.loadURL( 'file://' + __dirname + '/index.html' ) mainWindow. on ( 'minimize' , function() { mainWindow.hide() }) mainWindow. on ( 'maximize' , function() { console.log( 'maximize' ) }) mainWindow. on ( 'resize' , function() { // console.log('resized') }) mainWindow. on ( 'closed' , function() { mainWindow = null }) } |
package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "electron ."
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"electron": "^2.0.8"
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步