网站首页 > 技术文章 正文
最近一直捣鼓electron25整合vite4.x构建跨端桌面应用。今天给大家分享两种常用的electron截图方案。
方案一:截图dll
第一种是调用第三方提供的截图dll,通过node来只需exe文件截图。
const { join } = require('path')
const { execFile } = require('child_process')
// 调用dll截图
ipcMain.on('win-capture', () => {
let printScr = execFile(join(__dirname, '../resource/screenShot/PrintScr.exe'))
printScr.on('exit', (code) => {
if(code) {
console.log(code)
}
})
})
方案二:electron第三方截图插件
electron-screenshots 基于electron构建的截图工具。
安装组件
yarn add electron-screenshots
// or
npm i electron-screenshots -S
引入组件并调用截图。
const { globalShortcut } = require('electron')
const Screenshots = require('electron-screenshots').default
let screenshots = new Screenshots()
globalShortcut.register("ctrl+shift+q", () => {
screenshots.startCapture()
})
// 点击确定按钮回调事件
screenshots.on("ok", (e, buffer, bounds) => {
console.log("capture", buffer, bounds)
})
// 点击取消按钮回调事件
screenshots.on("cancel", () => {
console.log("capture", "cancel1")
if (screenshots.$win?.isFocused()) {
screenshots.endCapture();
}
})
screenshots.on("cancel", (e) => {
// 执行了preventDefault
// 点击取消不会关闭截图窗口
e.preventDefault()
console.log("capture", "cancel2")
})
// 点击保存按钮回调事件
screenshots.on("save", (e, buffer, bounds) => {
console.log("capture", buffer, bounds);
})
// esc取消
globalShortcut.register("esc", () => {
if (screenshots.$win?.isFocused()) {
screenshots.endCapture();
}
})
其实electron官方提供了desktop-capturer截图API,不过功能很有局限性。
OK,今天就分享到这里了。
猜你喜欢
- 2024-11-23 VC.NET全屏截屏或鼠标拖动出区域,截图效果「附源码」
- 2024-11-23 Star 17.3k!给它一张屏幕截图,即可一键克隆网页!
- 2024-11-23 一个好的前端年薪会有多少钱?
- 2024-11-23 57挑战之53,python实现客户端+服务端
- 2024-11-23 6个月转行成功WEB前端工程师的秘诀在这里
- 2024-11-23 分享 12 个超级实用的前端工具,可能就是你一直在寻找的!
- 2024-11-23 超级实用的截图工具分享—Snipaste
- 2024-11-23 前端革命时刻:前端代码是怎样智能生成的-图像分离篇
- 2024-11-23 推荐给前端开发的 5 款 Chrome 扩展
- 2024-11-23 这4个神器,帮你自动美化生成好看的网页截图
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- oracle面试 (55)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)