2017-02-28 36 views
0

我正在尝试编写一个允许用户选择屏幕区域的应用程序(如选择拍摄屏幕截图)。与Electron选择桌面屏幕的区域

folders in home

是,即使可能吗?

+0

关于上下文没有足够的信息。你只是想在屏幕上绘制选择矩形?什么是“屏幕”是一个电子应用程序的上下文:主窗口?用户的桌面? –

+1

@NoGrabbing“喜欢选择截图”。像剪切工具一样。他并不是指PC上的特定位置。 – Crowes

+0

就像截图的概念一样,它可以在任何地方完成。 –

回答

-1

要专门拍摄全屏幕照片,请使用以下代码(从Electron Demo App中拉出示例)。您可以构建此示例,并使用electron api中的screen,desktopCapturerrectangle模块自定义代码以获取特定的屏幕/显示,或选择特定的边界框(x/y坐标和像素区域)。

const electron = require('electron') 
const desktopCapturer = electron.desktopCapturer 
const electronScreen = electron.screen 
const shell = electron.shell 

const fs = require('fs') 
const os = require('os') 
const path = require('path') 

const screenshot = document.getElementById('screen-shot') 
const screenshotMsg = document.getElementById('screenshot-path') 

screenshot.addEventListener('click', function (event) { 
    screenshotMsg.textContent = 'Gathering screens...' 
    const thumbSize = determineScreenShotSize() 
    let options = { types: ['screen'], thumbnailSize: thumbSize } 

    desktopCapturer.getSources(options, function (error, sources) { 
    if (error) return console.log(error) 

    sources.forEach(function (source) { 
     if (source.name === 'Entire screen' || source.name === 'Screen 1') { 
     const screenshotPath = path.join(os.tmpdir(), 'screenshot.png') 

     fs.writeFile(screenshotPath, source.thumbnail.toPng(), function (error) { 
      if (error) return console.log(error) 
      shell.openExternal('file://' + screenshotPath) 
      const message = `Saved screenshot to: ${screenshotPath}` 
      screenshotMsg.textContent = message 
     }) 
     } 
    }) 
    }) 
}) 

function determineScreenShotSize() { 
    const screenSize = electronScreen.getPrimaryDisplay().workAreaSize 
    const maxDimension = Math.max(screenSize.width, screenSize.height) 
    return { 
    width: maxDimension * window.devicePixelRatio, 
    height: maxDimension * window.devicePixelRatio 
    } 
} 

,你可以去这个问题的其他途径有:

  1. 使用object.getClientRects()在DOM来指定要捕获特定的元素,尽管这将需要它们是什么先见之明。
  2. 在您的视图中添加事件侦听器,通过mouseClick,mouseMove等“绘制”您想要的形状。此stack overflow question的答案可以根据您的需要进行调整。
+0

谢谢,生病检查一下,看看它是否能解决我的问题,同时如果你帮我实现鼠标选择部分(如问题中的照片) –

+0

如果任何人都可以解释为什么答案没有upvoted,然后downvoted,这将不胜感激。答案解决OP的需要捕获屏幕的内容(如评论中提到的屏幕截图),并提供选项探索绘制选择框的方法。 –