Electron 提供了一个实时构建桌面应用的纯 JavaScript 环境。Electron 可以获取到你定义在 package.json 中 main 文件内容,然后执行它。通过这个文件(通常我们称之为 main.js),可以创建一个应用窗口,这个应用窗口包含一个渲染好的 web 界面,还可以和系统原生的 GUI 交互。

具体来说,就是当你启动了一个 Electron 应用,就有一个主进程(main process )被创建了。这条进程将负责创建出应用的 GUI(也就是应用的窗口),并处理用户与这个 GUI 之间的交互。

但直接启动 main.js 是无法显示应用窗口的,在 main.js 中通过调用BrowserWindow模块才能将使用应用窗口。然后每个浏览器窗口将执行它们各自的渲染器进程( renderer process )。渲染器进程将会处理一个真正的 web 页面(HTML + CSS + JavaScript),将页面渲染到窗口中。鉴于 Electron 使用的是基于 Chrominum 的浏览器内核,你就不太需要考虑兼容的问题。

  1. const {app, BrowserWindow} = require('electron')
  2. const path = require('path')
  3. const url = require('url')
  4.  
  5. // Keep a global reference of the window object, if you don't, the window will
  6. // be closed automatically when the JavaScript object is garbage collected.
  7. let win
  8.  
  9. function createWindow() {
  10. // Create the browser window.
  11. win = new BrowserWindow({ width: 800, height: 600, autoHideMenuBar: true })
  12.  
  13. // and load the index.html of the app.
  14. win.loadURL(url.format({
  15. pathname: path.join(__dirname, 'index.html'),
  16. protocol: 'file:',
  17. slashes: true
  18. }))
  19.  
  20. // Open the DevTools.
  21. win.webContents.openDevTools()
  22.  
  23. // Emitted when the window is closed.
  24. win.on('closed', () => {
  25. // Dereference the window object, usually you would store windows
  26. // in an array if your app supports multi windows, this is the time
  27. // when you should delete the corresponding element.
  28. win = null
  29. })
  30. }
  31.  
  32. // This method will be called when Electron has finished
  33. // initialization and is ready to create browser windows.
  34. // Some APIs can only be used after this event occurs.
  35. app.on('ready', createWindow)
  36.  
  37. // Quit when all windows are closed.
  38. app.on('window-all-closed', () => {
  39. // On macOS it is common for applications and their menu bar
  40. // to stay active until the user quits explicitly with Cmd + Q
  41. if (process.platform !== 'darwin') {
  42. app.quit()
  43. }
  44. })
  45.  
  46. app.on('activate', () => {
  47. // On macOS it's common to re-create a window in the app when the
  48. // dock icon is clicked and there are no other windows open.
  49. if (win === null) {
  50. createWindow()
  51. }
  52. })
  53.  
  54. // In this file you can include the rest of your app's specific main process
  55. // code. You can also put them in separate files and require them here.

electron入门代码的更多相关文章

  1. electron 入门小白贴

    electron 入门小白贴 electron demo 跑起来! 毕设准备是做个 跨平台的做题的客户端,打算用 electron 来弄. 然而今天折腾了半天才终于吧demo给跑起来了.经历了许多的问 ...

  2. TensorFlow(1)注解入门代码

    学习当然要从官方的入门文档开始. 但是这篇入门对于从0开始的初学者似乎有些困难,尤其是对于神经网络知识还是一知半解的. 敲完理解一遍还是懵逼. TensorFlow经典入门代码学习备注如下. impo ...

  3. Mybatis最入门---代码自动生成(generatorConfig.xml配置)

    [一步是咫尺,一步即天涯] 经过前文的叙述,各位看官是不是已经被Mybatis的强大功能给折服了呢?本文我们将介绍一个能够极大提升我们开发效率的插件:即代码自动生成.这里的代码自动生成包括,与数据库一 ...

  4. 使用Maven编译运行Storm入门代码(Storm starter)(转)

    Storm 官方提供了入门代码(Storm starter),即 Storm安装教程 中所运行的实例(storm-starter-topologies-0.9.6.jar),该入门代码位于 /usr/ ...

  5. 1、Electron入门HelloWorld案例

    一.Electron是什么? 官网:https://electronjs.org/ Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. ...

  6. electron入门心得

    前言 前端开发桌面程序这个概念已经出现有一段时间了,这项技术也已经走向成熟,Github上nw和光electron的star就差不多有10w颗星了,github也衍生出了很多开源的桌面项目俨然成了一个 ...

  7. Electron的代码调试

    刚接触Electron,尝试调试程序时,竟无从下手,所以把这个过程做了下记录 参考工程 根据Electron的官方文档:使用 VSCode 进行主进程调试:https://electronjs.org ...

  8. electron入门笔记(三)- 引入bootstrap

    源码:https://github.com/sueRimn/electron-bootstrap 当引入jQuery和bootstrap文件时,会报错,原因是:electron 的 Renderer ...

  9. Electron入门笔记(一)-自己快速搭建一个app demo

    Electron学习-快速搭建app demo 作者: 狐狸家的鱼 Github: 八至 一.安装Node 1.从node官网下载 ,最好安装.msi后缀名的文件,新手可以查看安装教程进行安装. 2. ...

随机推荐

  1. ocr智能图文识别 tess4j 图文,验证码识别 分享及所遇到的问题

    自己对tess4j的使用总结 1,tess4j 封装了 tesseract-ocr 的操作 可以用很简洁的几行代码就实现原本tesseract-ocr 复杂的实现逻辑 如果你也想了解tesseract ...

  2. webuploader插件,我踩得坑

    我在目前的公司做的项目要么是原生写法去做项目,要么就是vue+webpack做项目,但是vue这部分只是用了模板template,vue其他的都没用. 有一个项目需要做上传图片的功能,老大扔给我一个插 ...

  3. HTML中那些不常用标签

    先思考一个问题:为什么H5里面又多了那么多看似没用的标签? 我们知道,<div>能干百分之99的标签能干的事,而标签的主要作用是用来包裹内容,只要把基本内容都包含进去不就好了??胡闹!不带 ...

  4. rabbitmq 死信邮箱配置(dead-letter)

    DLX,Dead-Letter-Exchange(死信邮箱)利用DLX,当消息在一个队列中变成死信后,它能被重新publish到另一个Exchange,这个Exchange就是DLX.消息变成死信一向 ...

  5. 创建单页web app, 如何在chrome中隐藏工具栏 地址栏 标签栏?

    问题描述: 为使用更大的屏幕空间,在访问web应用的使用,如何隐藏地址栏.工具栏? 解决办法: 1. chrome的application mode 选项--->更多工具---->添加到桌 ...

  6. redhat初始化yum源,使用阿里云yum源

    本篇文章介绍redhat7使用国内阿里云yum安装源的方法,源地址使用的https://mirrors.aliyun.com/对应使用Centos7下的安装源. 在学习<Linux就该这么学&g ...

  7. ASP.NET Core配置Kestrel 网址Urls

    ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Urls,使用多种方式配置Urls.让 ...

  8. fedora安装QQ

    只看重利益的TC根本没想到要维护和更新linux版本的QQ,所幸fedora linux的中文社区 (https://repo.fdzh.org) 对大家比较照顾,还是针对fedora做了wine Q ...

  9. 初学MySQL

    MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.   Mysql默认端口号: 3306 超级用户:root   prompt 修改提示符. ( ...

  10. spring注解一次 清除多个缓存

    @Caching(evict = { @CacheEvict(value="cacheName",key="#info.id+'_baojia'",before ...