登录  /  注册
首页 > web前端 > js教程 > 正文

简述利用VS2017 和 js 进行桌面程序开发教程

巴扎黑
发布: 2017-07-22 14:03:39
原创
6651人浏览过

现在基于 js 和 web浏览器核心构建的 c/s 程序越来越多,比如微信桌面版(基于 duilib 和 cef)、vs code(基于electron)等,出于了解的目的,最近学习了 electron。electron具体是什么,可以做什么,这里不做过多的介绍,网上很多相关的介绍,这里主要介绍在vs2017下怎么进行 electron 应用程序的开发。

一、环境搭建

  • 安装 node.js 及 npm。

  • 安装 vs2017 ,必须安装 node.js web开发包。

二、创建空白 Node.js 控制台应用程序项目

创建好的项目结构入下:

三、编写 electron Hello Word

参照 :

1、打开 package.json 加入:

"dependencies": {"electron": "^1.6.6"}
登录后复制

最后内容如下:

{"name": "electron-hello-word","version": "0.0.0","description": "ElectronHelloWord","main": "app.js?1.1.11","author": {"name": "Starts_2000"},"dependencies": {"electron": "^1.6.6"}
}
登录后复制

2、打开 app.js,输入以下内容:

'use strict';

const { app, BrowserWindow } = 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 })// and load the index.html of the app.    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true}))// 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 + Qif (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.
登录后复制

3、新建 index.html 文件

nbsp;html&gt;<meta><title>Hello World!</title><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>.
登录后复制

四、设置和运行

1、NPM 安装  electron 包

安装成功后可看到,点击解决方案上方的显示所有文件,可以看到

2、设置项目 Node.exe路径为:

.\node_modules\electron\dist\electron.exe
登录后复制

运行解决方案,就可以看到electron 的 Hello Word示例了:

以上就是简述利用VS2017 和 js 进行桌面程序开发教程的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号