当前位置: 首页 > 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.lryc.cn/news/621635.html

相关文章:

  • ABP vNext+ WebRTC DataChannel 低延迟传感推送
  • Tokenizer(切词器)的不同实现算法
  • 代码随想录刷题Day33
  • 分库分表和sql的进阶用法总结
  • AI架构师生存手册:图解避坑MCP工具链/智能体RAG/推理蒸馏实战
  • 【LINUX网络】HTTP协议基本结构、搭建自己的HTTP简单服务器
  • 日本CN2服务器租用多少钱
  • MySQL约束知识点
  • JavaScript 逻辑运算符与实战案例:从原理到落地
  • 流处理、实时分析与RAG驱动的Python ETL框架:构建智能数据管道(上)
  • JavaScript 基础实战:DOM 操作、数据类型与常见需求实现
  • IT资讯 | VMware ESXi高危漏洞影响国内服务器
  • 从应用场景看国产化FPGA潜力,紫光同创研讨会武汉·北京站回顾
  • Linux搭建ftp服务器
  • Unity:PlayerPrefs笔记
  • Python入门第3课:Python中的条件判断与循环语句
  • 戴永红×数图:重构零售空间价值,让陈列创造效益!
  • Vue 侦听器(watch 与 watchEffect)全解析1
  • 从CAD数据访问到3D协作,HOOPS SDK如何提升PLM解决方案竞争力?
  • 机械学习---- PCA 降维深度解析
  • p5.js 3D盒子的基础用法
  • 用 Enigma Virtual Box 将 Qt 程序打包成单 exe
  • Helm 常用命令 + Bitnami 中间件部署速查表
  • 北京JAVA基础面试30天打卡10
  • JVM核心原理与实战优化指南
  • Java 调用 Python 脚本:实现 HelloWorld
  • Android 欧盟网络安全EN18031 要求对应的基本表格填写
  • 电脑上练打字用什么软件最好:10款打字软件评测
  • 【Linux】编辑器vim的使用
  • IDE:vscode的vue3模板