Electron截屏功能

# Electron截屏功能
window下增加该功能,可以调用三方的exe文件然后通过node.js的原生模块,execFile启动该exe文件。
mac下则可以通过screencapture 来调用系统的截屏功能来实现

 

globalShortcut.register('CommandOrControl+Alt+Z', function () {
  if (process.platform === 'darwin') {
    handleScreenShots()
  } else {
    screenWindow()
  }
})
function screenWindow() {
  console.log('__dirname', __dirname)
  let url = path.resolve(__dirname, '../../qq/PrScrn.exe')
  // let url = path.resolve(__dirname, '../extraResources/PrintScr.exe')

  if (isDevelopment && !process.env.IS_TEST) {
    // 生产环境
    url = path.join(__dirname, '/qq/PrintScr.exe')
  }
  console.log(url + '截图工具路径')
  let screenWindow = execFile(url)
  screenWindow.on('exit', (code) => {
    mainWindow.restore()
    if (code) console.log(code)
  })
}
function handleScreenShots() {
  exec(`screencapture -i -U -c`, (error, stdout, stderr) => {
    console.log('308', error)
  })
}

screencapture

-c 强制截图保存到剪贴板而不是文件中
-C 截图时保留光标(只在非交互模式下有效)
-d display errors to the user graphically(不知道啥意思)
-i 交互模式截取屏幕。可以是选区或者是窗口。按下空格可切换截屏模式
-m 只截取主显示器(-i模式下无效)
-M 截图完毕后,会打开邮件客户端,图片就躺在邮件正文中
-o 在窗口模式下,不截取窗口的阴影
-P 截图完毕后,在图片预览中打开
-s 只允许鼠标选择模式
-S 窗口模式下,截取屏幕而不是窗口
-t png 指定图片格式,模式是png。可选的有pdf, jpg, tiff等
-T 延时截取,默认为5秒。
-w 只允许窗口截取模式
-W 开始交互截取模式,默认为窗口模式(只是默认模式与-i不同)
-x 不播放声效
-a do not include windows attached to selected windows(不懂)
-r 不向图片中加入dpi信息
-l<windowid> 抓取指定windowid的窗口截图
-R<x,y,w,h> 抓取指定区域的截图
-B<bundleid> 截图输出会被bundleid指出的程序打开
-U 打开截屏操作版
需要注意的是,调用这个功能的时候需要系统授权,否则截屏时,将会报错,不能从window创建图片。

 

posted @ 2020-04-21 14:23  狗尾草的博客  阅读(4129)  评论(0编辑  收藏  举报