当前位置: 首页 > news >正文

electron 主进程 和 渲染进程通信 ipcRenderer 和 mainWindow.webContents

electron 开发时最麻烦就是electron版本和node版本的选择和正确安装
electron 用npm安装时太慢容易报错,建议用cnpm i 进行安装

注意最新版渲染进程使用node
nodeIntegration: true, // 渲染进程可用node
contextIsolation: false, // 这个值影响nodeIntegration是否生效electron  的主进程的创建
function createWindow () {// Create the browser window.const mainWindow = new BrowserWindow({width: 800,height: 600,webPreferences: {nodeIntegration: true, // 渲染进程可用nodecontextIsolation: false, // 这个值影响nodeIntegration是否生效preload: path.join(__dirname, 'preload.js')}})// and load the index.html of the app.mainWindow.loadFile(path.join(__dirname, 'index.html'))// Open the DevTools.mainWindow.webContents.openDevTools()// 渲染进程使用remote remote.enable(mainWindow.webContents)//3require("./ipcMain/menu")require("./ipcMain/rightMenu")// 主进程发送消息mainWindow.webContents.send("mainMag","发财发财")
}app.on('ready', createWindow);
<!DOCTYPE html>
<html><head><meta charset="UTF-8" /><title>Hello World!</title><link rel="stylesheet" href="index.css" /></head><body><h1>💖 Hello World!</h1><p>Welcome to your Electron application.</p><h1>Hello World!</h1>We are using Node.js <span id="node-version"></span>,Chromium <span id="chrome-version"></span>,and Electron <span id="electron-version"></span>.<!-- You can also require other files to run in this process --><!-- <script src="./renderer.js"></script> --></body><script>// 引用node中的函数require('path')// 高版本,渲染线程打开新窗口方法,需要在主进程打开// const {BrowserWindow} = require("@electron/remote")// const win = new BrowserWindow({//         width:500,//         height:500,//     })// win.loadURL("https://www.baidu.com")const { ipcRenderer } = require("electron");window.addEventListener('contextmenu', (e) => {e.preventDefault()// 渲染进程发送消息ipcRenderer.send('show-context-menu')})// 渲染进程接受主进程消息ipcRenderer.on('context-menu-command', (e, command) => {// ...})ipcRenderer.on('show-context-menu-reply', (e, command) => {console.log("开心",command)})ipcRenderer.on('mainMag', (e, command) => {console.log(e)console.log("------------")console.log(command)})</script>
</html>
http://www.lryc.cn/news/226303.html

相关文章:

  • 关于VUE启动内存溢出
  • HBase学习笔记(1)—— 知识点总结
  • 【Linux】 awk命令使用
  • Sentinel网关限流
  • solidworks对电脑要求高吗?2023solidworks配置要求
  • 搭建神经网络(torch.nn的用法)
  • 卡码网语言基础课 | 11. 句子缩写
  • Surface RT 安装 Linux
  • C++中的函数重载:多功能而强大的特性
  • 数据分析实战 | K-means算法——蛋白质消费特征分析
  • HTTP协议详解-下(Tomcat)
  • acwing算法基础之搜索与图论--prim算法
  • Amazon EC2 Serial Console 现已在其他亚马逊云科技区域推出
  • hdlbits系列verilog解答(100输入逻辑门)-39
  • Python 中 Selenium 的屏幕截图
  • scrapy发json的post请求
  • 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
  • 自主开发刷题应用网站H5源码(无需后端无需数据库)
  • java 读取excel/word存入mysql
  • 11.(vue3.x+vite)组件间通信方式之ref与$parent、$children
  • [工业自动化-12]:西门子S7-15xxx编程 - PLC从站 - ET200 SP系列详解
  • 消息队列简介
  • SQL中实现汉字的拼音首字母查询
  • 今天知道LiveData的ktx是真的香
  • SpringBoot中的桥接模式
  • AI爆文变现脚本:易用且免费的自动写作脚本更新了
  • 代码随想录算法训练营Day 49 || 123.买卖股票的最佳时机III 、188.买卖股票的最佳时机IV
  • threejs(11)-精通着色器编程(难点)2
  • 配置cuda和cudnn出现 libcudnn.so.8 is not a symbolic link问题
  • “目标值排列匹配“和“背包组合问题“的区别和leetcode例题详解