Open xj124456 opened 4 years ago
in main process, listen following message
ipcMain.on('capture-message', (event, arg) => {
const {api, ...rest} = arg
let rtv = null
switch (api) {
case 'getCurrentScreen':
const {x, y} = rest
const display = screen.getAllDisplays().filter(d => d.bounds.x === x && d.bounds.y === y)[0]
rtv = display
break
case 'isCursorInCurrentWindow':
const {
x: winX, y: winY, width, height
} = rest
const { x: ptX, y: ptY } = screen.getCursorScreenPoint()
rtv = ptX >= winX && ptX <= winX + width && ptY >= winY && ptY <= winY + height
break
default:
break
}
event.returnValue = rtv
})
in render process utils.js
import { ipcRenderer, remote } from 'electron'
let currentWindow = remote.getCurrentWindow()
export const getCurrentScreen = () => {
let { x, y } = currentWindow.getBounds()
const rtv = ipcRenderer.sendSync('capture-message', {api: 'getCurrentScreen', x, y})
return rtv
}
export const isCursorInCurrentWindow = () => {
let bound = currentWindow.getBounds()
const rtv = ipcRenderer.sendSync('capture-message', {api: 'isCursorInCurrentWindow', ...bound})
return rtv
}
试验了下,这个错误没了,但是点击截图按钮没效果了,不知道是不是我写的有问题
const useCapture = () => { globalShortcut.register('Esc', () => { if (captureWins) { captureWins.forEach(win => win.close()) captureWins = [] } })
globalShortcut.register('CmdOrCtrl+Shift+A', captureScreen)
ipcMain.on('capture-screen', (e, { type = 'start', screenId } = {}) => {
if (type === 'start') {
captureScreen()
} else if (type === 'complete') {
// nothing
} else if (type === 'select') {
captureWins.forEach(win => win.webContents.send('capture-screen', { type: 'select', screenId }))
}
})
ipcMain.on('capture-message', (event, arg) => {
console.log(11111)
const { api, ...rest } = arg
let rtv = null
switch (api) {
case 'getCurrentScreen':
const { x, y } = rest
const display = screen.getAllDisplays().filter(d => d.bounds.x === x && d.bounds.y === y)[0]
rtv = display
break
case 'isCursorInCurrentWindow':
const {
x: winX, y: winY, width, height,
} = rest
const { x: ptX, y: ptY } = screen.getCursorScreenPoint()
rtv = ptX >= winX && ptX <= winX + width && ptY >= winY && ptY <= winY + height
break
default:
break
}
event.returnValue = rtv
})
}
you miss the modification in utils.js, it's just a simple sync-communication between main and render processes
import { ipcRenderer, remote } from 'electron'
let currentWindow = remote.getCurrentWindow()
export const getCurrentScreen = () => {
let { x, y } = currentWindow.getBounds()
const rtv = ipcRenderer.sendSync('capture-message', {api: 'getCurrentScreen', x, y})
return rtv
}
export const isCursorInCurrentWindow = () => {
let bound = currentWindow.getBounds()
const rtv = ipcRenderer.sendSync('capture-message', {api: 'isCursorInCurrentWindow', ...bound})
return rtv
}
use electron-vue electron 7.1.7
I see the screen module in the Renderer process, is this reason? and hot to fix it