electron 学习

index.html

<!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

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"
}
}
posted @ 2018-08-25 15:56  高中国流  阅读(213)  评论(0编辑  收藏  举报