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

解决Electron透明窗口点击不影响其他应用

遇到的问题:

在electron透明窗口点击,影响窗口下的应用接受不到点击事件

解决方案:

CSS+IgnoreMouseEvents

实现原理:

主进程默认设置禁用目标窗口鼠标事件(禁用之后能检测到mousemove),UI进程检测页面的的mousemove事件,判断是否是透明区域,如果是透明区域则禁用鼠标事件,如果不是透明区域则关闭(渲染进程根节点设置point-events: none, 需要检测的界面设置point-events: unset)

实现代码:

下面是渲染进程下面👇

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Electron透明窗口示例</title><style>body {pointer-events: none;}.container {width: 500px;height: 500px;display: flex;justify-content: center;align-items: center;pointer-events: none;}.targetArea {width: 250px;height: 250px;background-color: #000;pointer-events: auto;text-align: center;line-height: 250px;color: #fff;font-size: 20px;}</style>
</head>
<body><div class="container"><div class="targetArea">目标区域</div></div><script src="./renderer1.js" type="module"></script>
</body>
</html>
const { ipcRenderer } = require('electron');(function() {try {console.log('🐊 Smart mouse events injection started');// 智能鼠标事件穿透实现function setupSmartMouseEvents() {console.log('🐊 Setting up smart mouse events...');// 监听鼠标移动事件window.addEventListener("mousemove", (event) => {try {// 关键判断:如果鼠标在透明区域(event.target是document.documentElement)const isInTransparentArea = event.target === document.documentElement;console.log(isInTransparentArea ? '不透明区域' : '透明区域')if (isInTransparentArea) {// 在透明区域:启用鼠标事件穿透ipcRenderer.send('set-ignore-mouse-events', true);} else {// 在内容区域:禁用鼠标事件穿透,让当前窗口处理事件ipcRenderer.send('set-ignore-mouse-events', false);}} catch (error) {console.error('🐊 Error in mousemove handler:', error);}});// 初始设置:启用穿透try {ipcRenderer.send('set-ignore-mouse-events', true);console.log('🐊 Initial mouse penetration enabled');} catch (error) {console.error('🐊 Error setting initial mouse penetration:', error);}}// DOM加载完成后设置鼠标事件if (document.readyState === 'loading') {document.addEventListener('DOMContentLoaded', setupSmartMouseEvents);} else {setupSmartMouseEvents();}} catch (error) {console.error('🐊 Error in smart mouse events injection:', error);}
})()

下面是主进程代码👇

这里是主进程代码
function createWindow() {// 创建浏览器窗口mainWindow = new BrowserWindow({width: 800,height: 600,frame: false,transparent: true,webPreferences: {nodeIntegration: true,contextIsolation: false,enableRemoteModule: true}});// 加载应用的 index.htmlmainWindow.loadFile('src/mousevent.html');// 设置全局引用global.mainWindow = mainWindow;ipcMain.on('set-ignore-mouse-events', (event, value) => {console.log('set-ignore-mouse-events', value)mainWindow.setIgnoreMouseEvents(value, { forward: true });});
}
http://www.xdnf.cn/news/1301257.html

相关文章:

  • 启动electron桌面项目控制台输出中文时乱码解决
  • 下载及交叉编译zlib库,记录
  • 解决ECharts图表上显示的最小刻度不是设置的min值的问题
  • 从源码到可执行文件:hello.c 的二进制之旅
  • 【Golang】:数据类型
  • Wi-Fi 与蜂窝网络(手机网络)的核心区别,以及 Wi-Fi 技术未来的发展方向
  • Redisson分布式锁实战指南:原理、用法与项目案例
  • GPT 解码策略全解析:从 Beam Search 到 Top-p 采样
  • 流处理、实时分析与RAG驱动的Python ETL框架:构建智能数据管道(上)
  • CPU、内存、存储:生信分析任务的服务器配置精要
  • 第20章 LINQ 笔记
  • 8.15网络编程——UDP和TCP并发服务器
  • 【数据分享】上市公司创新韧性数据(2007-2023)
  • 数据驱动测试提升自动化效率
  • 终极手撸cpu系列-详解底层原理-CPU硬核解剖:从0和1到 看透CPU逻辑设计内部原理,弄清楚现代多线程cpu工作原理
  • Microsoft Visual Studio常用快捷键和Windows系统常用快捷键的整理
  • Linux-地址空间
  • 开发避坑指南(27):Vue3中高效安全修改列表元素属性的方法
  • 【学习笔记】NTP服务客户端配置
  • Go语言中安全停止Goroutine的三种方法及设计哲学
  • 前瞻性技术驱动,枫清科技助力制造企业借助大模型完成生产力转化
  • zabbix部署问题后常见问题
  • 新手入门Makefile:FPGA项目实战教程(二)
  • 【CV 目标检测】②R-CNN模型
  • 【Redis】分布式系统的演化过程
  • MyBatis的基本用法和配置方式
  • Highcharts Dashboards | 打造企业级数据仪表板:从图表到数据驾驶舱
  • 全球电商业财一体化:让出海品牌实现“看得见的增长“
  • demo 通讯录 + 城市选择器 (字母索引左右联动 ListItemGroup+AlphabetIndexer)笔记
  • Nginx反向代理与缓存实现