electron vue 透明桌面事件穿透

打印 上一主题 下一主题

主题 1000|帖子 1000|积分 3000

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
electron 透明桌面事件穿透

网上有很多方式都不怎么理想,这里有个新方式
我这里用到了remote  ,引用的包是  @electron/remote
  1. 在主流程main.js下引用
  2. import { screen } from 'electron'
  3. ipcMain.handle('get-screen-point', () => screen.getCursorScreenPoint());
复制代码
 
  1. 在渲染流程中,这里是vue
  2. import { ipcRenderer } from 'electron'
  3. import { BrowserWindow } from '@electron/remote'
  4. const win = BrowserWindow.getFocusedWindow()
  5. let t = null
  6.   mounted () {
  7.     window.addEventListener('mousemove', event => {
  8.       this.handleMouseMov(event, this)
  9.     });
  10.   },
  11.   methods: {
  12.     getScreenPoint: () => {
  13.       return ipcRenderer.invoke('get-screen-point')
  14.     },
  15.     handleMouseMov: (e, _this) => {
  16.       console.log(e, document)
  17.       if (e.target === document.documentElement) {
  18.         win.setIgnoreMouseEvents(true);
  19.         if (t) clearInterval(t);
  20.         t = setInterval(async () => {
  21.           _this.getScreenPoint().then(point => {
  22.             const elem = document.elementFromPoint(
  23.               Math.floor(point.x),
  24.               Math.floor(point.y)
  25.             );
  26.             if (elem !== document.documentElement) {
  27.               win.setIgnoreMouseEvents(false);
  28.               clearInterval(t);
  29.             }
  30.           })
  31.         }, 150);
  32.       }
  33.       else {
  34.         win.setIgnoreMouseEvents(false);
  35.       }
  36.     }
  37.   },
复制代码
 
  1. 在样式中
  2. body,html{
  3.   pointer-events: none
  4. }
  5. // 不穿透层
  6. #noPo{
  7.   pointer-events: auto;
  8. }
复制代码
 
 
[code][/code] 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

不到断气不罢休

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表