博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
electron学习
阅读量:5839 次
发布时间:2019-06-18

本文共 3647 字,大约阅读时间需要 12 分钟。

hot3.png

1、安装nodejs

2、用npm全局安装electron(可以不全局安装,后面在项目中安装)

3、创建一个目录my_app,下面创建3个文件:package.json、main.js、index.html

4、编缉index.html内容:

      
Hello World!

Hello World!

We are using node , Chrome , and Electron .

5、编缉main.js内容:

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 winfunction 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.

6、编缉package.json内容:

{  "name": "demo_app",  "version": "0.1.0",  "main": "main.js",}

7、运行

在demo_app目录下,执行electron .

如果没有全局安装electron,可以在目录下执行npm install --save-dev electron. 然后在执行在demo_app/node_modules/.bin/目录下执行electron ../../

--save-dev会在package.json中加入一行:

  "devDependencies": {

    "electron": "^1.4.15"
  }

8、打包

使用electron-package进行打包,生成目录demo_app_release。

electron-packager ./demo_app --platform=win32 --out ../demo_app_release --version 1.4.13 --overwrite --icon=./images/app.ico

9、脚本与依赖

修改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进行打包。

10、快捷键

添加ctrl+q,退出程序,在createWindow函数最后加上:

globalShortcut.unregisterAll(); globalShortcut.register('ctrl+q',function(){    app.quit();  });

注册快捷键向主页面发送事件:

globalShortcut.register('alt+c',()=>{    win.webContents.send('global-shortcut','change');  });

接收与处理事件:

修改index.html,引入脚本:

      
Hello World!

Hello World!

We are using node , Chrome , and Electron .

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

11、菜单

 

转载于:https://my.oschina.net/u/136074/blog/828470

你可能感兴趣的文章
【HDOJ】1462 Word Crosses
查看>>
IIS 7上部署PHP【后续一】
查看>>
elasticSearch新认知
查看>>
嵌入式软件设计第8次实验报告
查看>>
使用 phpStorm 开发
查看>>
Hadoop基础-HDFS的API实现增删改查
查看>>
使用mysql存放Ambari元数据的配置案例
查看>>
在gazebo中启动TurtleBot模拟器出错的解决办法
查看>>
网站地图获取
查看>>
git总结三、关于分支下——团队合作中最重要的合并分支
查看>>
应用程序域
查看>>
Js中遇到的坑点汇总
查看>>
一个页面重构工程师眼中的“用户体验”
查看>>
JS2 -- 继承
查看>>
bootstrap-table表格插件获取分页参数
查看>>
《UML大战需求分析》-读后感一
查看>>
[HNOI2008]神奇的国度
查看>>
字符串转换成js的日期格式
查看>>
MYSQL数据库
查看>>
Chrome
查看>>