使用 VS2017 和 js 進行桌面程序開發 - electron 之 Hello Word


現在基於 js 和 web瀏覽器核心構建的 C/S 程序越來越多,比如微信桌面版(基於 duilibcef)、VS CODE(基於electron)等,出於了解的目的,最近學習了 electron。electron具體是什么,可以做什么,這里不做過多的介紹,網上很多相關的介紹,這里主要介紹在VS2017下怎么進行 electron 應用程序的開發。

一、環境搭建

  • 安裝 node.js 及 npm。
  • 安裝 vs2017 ,必須安裝 node.js web開發包。

二、創建空白 Node.js 控制台應用程序項目

創建好的項目結構入下:

三、編寫 electron Hello Word

參照 https://github.com/electron/electron/blob/master/docs/tutorial/quick-start.md

1、打開 package.json 加入:

"dependencies": {
        "electron": "^1.6.6"
    }

最后內容如下:

{
    "name": "electron-hello-word",
    "version": "0.0.0",
    "description": "ElectronHelloWord",
    "main": "app.js",
    "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 win

function 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 + 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.

3、新建 index.html 文件

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
</head>
<body>
    <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>.
</body>
</html>

四、設置和運行

1、NPM 安裝  electron 包

安裝成功后可看到,點擊解決方案上方的顯示所有文件,可以看到

2、設置項目 Node.exe路徑為:

.\node_modules\electron\dist\electron.exe

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM