Browse Source

增加截图案例
Signed-off-by: Caner <5658514@qq.com>

Caner 3 years ago
parent
commit
19f85c88e5
3 changed files with 36 additions and 0 deletions
  1. 36 0
      main.js
  2. BIN
      public/screen/PrScrn.dll
  3. BIN
      public/screen/PrintScr.exe

+ 36 - 0
main.js

@@ -5,6 +5,34 @@ let win; let
   loadingWin
 Menu.setApplicationMenu(null) // 去掉菜单栏
 app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
+
+
+// 截图
+function screenShot() {
+  return new Promise((res, rej) => {
+    const file = execFile(`${path.resolve(__dirname, 'screen/PrintScr.exe')}`)
+    file.on('exit', (e) => {
+      if (e === 1) {
+        // 剪切板读buffer图片
+        const clip = clipboard.readImage()
+        if (clip.isEmpty()) {
+          // eslint-disable-next-line prefer-promise-reject-errors
+          rej(null)
+        } else {
+          // 转成buffer base64
+          const png = clipboard.readImage().toPNG()
+          const img = 'data:image/png;base64,' + png.toString('base64')
+          res(img)
+        }
+      } else {
+        // eslint-disable-next-line prefer-promise-reject-errors
+        rej(null)
+      }
+    })
+  })
+}
+
+
 // 创建loading 窗口
 const showLoading = () => {
   loadingWin = new BrowserWindow({
@@ -55,6 +83,14 @@ app.on('ready', async () => {
     loadingWin.close()
     win.show()
   })
+  // 监听截图
+  ipcMain.once('screen', () => {
+    screenShot().then(res => {
+      if (!res) return
+      // 发送到子进程
+      win.webContents.send('screenData', res)
+    })
+  })
 })
 
 app.on('window-all-closed', () => {

BIN
public/screen/PrScrn.dll


BIN
public/screen/PrintScr.exe