
3 changed files with 103 additions and 40 deletions
@ -1,49 +1,80 @@ |
|||
# electron-quick-start |
|||
|
|||
**Clone and run for a quick way to see Electron in action.** |
|||
|
|||
This is a minimal Electron application based on the [Quick Start Guide](https://electronjs.org/docs/tutorial/quick-start) within the Electron documentation. |
|||
|
|||
**Use this app along with the [Electron API Demos](https://electronjs.org/#get-started) app for API code examples to help you get started.** |
|||
|
|||
A basic Electron application needs just these files: |
|||
|
|||
- `package.json` - Points to the app's main file and lists its details and dependencies. |
|||
- `main.js` - Starts the app and creates a browser window to render HTML. This is the app's **main process**. |
|||
- `index.html` - A web page to render. This is the app's **renderer process**. |
|||
|
|||
You can learn more about each of these components within the [Quick Start Guide](https://electronjs.org/docs/tutorial/quick-start). |
|||
|
|||
## To Use |
|||
|
|||
To clone and run this repository you'll need [Git](https://git-scm.com) and [Node.js](https://nodejs.org/en/download/) (which comes with [npm](http://npmjs.com)) installed on your computer. From your command line: |
|||
# 项目介绍 |
|||
|
|||
## 如何通过electron调起外部应用 |
|||
|
|||
该场景适用于从网页中发起外部程序调用请求,并获取外部程序执行结果。(目前通过electron打开的网页均禁止了node执行,所以没法直接在项目中使用ipcRenderer,均通过preload中暴露的方法`window.api`) |
|||
|
|||
```javascript |
|||
// 发起调用命令 |
|||
window.api.send('spawn', { |
|||
command: '微信.exe', |
|||
args: null, // ['--disable-cache'] 多参数通过数组方式传递 |
|||
key: + new Date, // 唯一key,用于推送后续执行结果时进行关联 |
|||
options: { |
|||
onSuccess: data => {}, // 执行成功的回调,data为执行程序的输出 |
|||
onError: err => {}, // 执行失败的回调 |
|||
onClose: () => {}, // 关闭回调 |
|||
} |
|||
}) |
|||
|
|||
// 接收执行结果 |
|||
window.api.on('spawn-success', ({ pid, key }) => { |
|||
// pid 为以上调起程序后的pid(用于后续主动kill) |
|||
// key 为调起时传递的key |
|||
}) |
|||
|
|||
// 主动关闭程序 |
|||
window.api.send('kill-process', [pid]); |
|||
``` |
|||
|
|||
```bash |
|||
# Clone this repository |
|||
git clone https://github.com/electron/electron-quick-start |
|||
# Go into the repository |
|||
cd electron-quick-start |
|||
# Install dependencies |
|||
npm install |
|||
# Run the app |
|||
npm start |
|||
## 如何和外部程序进行通信(双向) |
|||
|
|||
该场景适用于在网页内监听外部程序发送的消息,并且向外部程序推送消息。 |
|||
|
|||
```javascript |
|||
// 接收外部程序的消息,如openMap通道(具体的通道配置请参考ipc.js) |
|||
window.api.on('openMap', data => { |
|||
// data 为json格式 |
|||
}) |
|||
|
|||
// 监听自定义通道,如custom-xxx(完全自定义,具体的名字可以和外部程序商量) |
|||
window.api.on('custom-xxx', data => {}) |
|||
|
|||
// 向外部程序推送消息 |
|||
/** |
|||
* 其中channel为必传参数,标识应该往哪个通道推送消息 |
|||
* 剩下的数据会原封不动的通过JSON序列化之后进行传递 |
|||
*/ |
|||
window.api.send('publisMessage', { |
|||
channel: 'custom-xxx', |
|||
author: 'application', |
|||
id: 1, |
|||
}) |
|||
``` |
|||
|
|||
Note: If you're using Linux Bash for Windows, [see this guide](https://www.howtogeek.com/261575/how-to-run-graphical-linux-desktop-applications-from-windows-10s-bash-shell/) or use `node` from the command prompt. |
|||
## 如何进行渲染进程(多窗口)间通信 |
|||
|
|||
## Resources for Learning Electron |
|||
该场景适用于通过electron打开的多窗口之间进行通信的方法。 |
|||
|
|||
- [electronjs.org/docs](https://electronjs.org/docs) - all of Electron's documentation |
|||
- [electronjs.org/community#boilerplates](https://electronjs.org/community#boilerplates) - sample starter apps created by the community |
|||
- [electron/electron-quick-start](https://github.com/electron/electron-quick-start) - a very basic starter Electron app |
|||
- [electron/simple-samples](https://github.com/electron/simple-samples) - small applications with ideas for taking them further |
|||
- [electron/electron-api-demos](https://github.com/electron/electron-api-demos) - an Electron app that teaches you how to use Electron |
|||
- [hokein/electron-sample-apps](https://github.com/hokein/electron-sample-apps) - small demo apps for the various Electron APIs |
|||
```javascript |
|||
// 通过框架中转消息(广播,会广播给发送者自己) |
|||
window.api.send('bridge', { |
|||
channel: 'map-connection', // 必传参数,完全自定义,框架会自动广播该事件 |
|||
mapData: {}, // 剩下的参数会直接透传 |
|||
}) |
|||
|
|||
## License |
|||
// 以上面的事件为例,在其他应用中监听该事件达到事件通信的目的 |
|||
window.api.on('map-connection', data => {}) |
|||
``` |
|||
|
|||
[CC0 1.0 (Public Domain)](LICENSE.md) |
|||
## 如何通过框架以新窗口的形式打开应用 |
|||
|
|||
打包文件通过 |
|||
该场景适用于做多屏展示效果,即通过打开多个窗口的形式同时展示多个应用,结合上面的应用间通信方法可以做到多屏互动的效果。 |
|||
|
|||
asar pack app app.asar 命令执行 |
|||
```javascript |
|||
// 手动打开一个窗口(可以是框架、也可以是应用本身) |
|||
window.api.send('openWindow', { |
|||
id: 'TIS_PLATFORM_KT', // 必传,标识窗口唯一性的,传递多次只会打开一个实例 |
|||
entry: 'http://localhost:7001/', // 必传,访问地址 |
|||
}); |
|||
``` |
@ -0,0 +1,22 @@ |
|||
const { BrowserWindow } = require('electron') |
|||
const { createWindow } = require('./lib/common') |
|||
|
|||
// props: { id: 'TIS_APP_TEMPLATE_KT', entry: '' }
|
|||
const create = props => { |
|||
const windows = BrowserWindow.getAllWindows() |
|||
const found = windows.find(w => w[props.id]) |
|||
if (found) { |
|||
found.show() |
|||
found.center() |
|||
return found |
|||
} |
|||
|
|||
const win = createWindow(props.entry, { |
|||
show: true |
|||
}) |
|||
win[props.id] = true |
|||
|
|||
return win |
|||
} |
|||
|
|||
module.exports = { create } |
Write
Preview
Loading…
Cancel
Save
Reference in new issue