qidao123.com技术社区-IT企服评测·应用市场

标题: electron vue 透明桌面事件穿透 [打印本页]

作者: 不到断气不罢休    时间: 2022-8-9 14:46
标题: electron vue 透明桌面事件穿透
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] 

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




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4