nik*_*ssc 5 html javascript node.js electron
我正在写一个Eletron节目.在程序中有一个索引窗口,由主进程(main.js)创建.在此窗口中有一个文件列表(图像).当我单击该列表中的一个文件时,我想启动第二个显示该文件的窗口.第二个窗口由索引窗口(index.js)的渲染器进程启动.如何在索引窗口的渲染器进程和第二个窗口的渲染器进程之间进行通信?
码:
从main.js中的主进程创建索引窗口:
let win;
function createWindow(){
// Create the browser window.
win = new BrowserWindow({width: 1024, height: 768, minWidth: 800, minHeight: 600, show: false, icon: 'files/images/icon.png'});
win.loadURL(`file://${__dirname}/files/html/index.html`);
win.once('ready-to-show', () => {
win.show()
})
// Emitted when the window is closed.
win.on('closed', () => {
win = null;
});
}
app.on('ready', createWindow);
Run Code Online (Sandbox Code Playgroud)
在index.html中,index.js(渲染器进程)启动:
<script src="../javascript/index.js"></script>
Run Code Online (Sandbox Code Playgroud)
在index.js function create_sprite_window()
中调用它来创建子窗口:
const fs = require('fs');
const path = require('path');
const {BrowserWindow} = require('electron').remote
let child_windows = [];
function create_child_window(URL, width, height){
let rem_win = require('electron').remote.getCurrentWindow();
let new_win = new BrowserWindow({width: width, height: height, minWidth: 400, minHeight: 300, show: false, parent: rem_win, minimizable: true, maximizable: true, skipTaskbar: true});
child_windows[child_windows.length] = new_win;
console.log(child_windows);
new_win.loadURL(URL);
new_win.once('ready-to-show', () => {
new_win.show()
})
return new_win;
}
function create_sprite_window(){
new_win = create_child_window(`file://${__dirname}/../html/sprite_manager.html`, 800, 400);
}
Run Code Online (Sandbox Code Playgroud)
子窗口存储在数组中child_windows
.
是否可以将图像的路径发送到第二个窗口,或者,可以从索引窗口?编辑<img>
第二个窗口的标记(将第二个窗口中的<img>
标记源设置为图像getElementById.src = path;
).
我自己找到了答案。为了在第二个渲染器窗口中显示正确的图像,我将 GET 参数添加到包含图像路径的 URL。