当前位置: 首页 > news >正文

electron在单例中实现双击打开文件,并重复打开其他文件

单实例的思路

  1. 首次通过双击文件打开应用
    • filePath传给render
  2. 使用中的应用,再次双击打开文件
    • 第一个实例创建时,同时创建一个通信服务器net.createServer()
    • 第二个实例创建时,连接第一个服务器net.createConnection()
    • 将再次打开的filePath传递给第一个实例
    • 然后在传递给render

1. 首次通过双击文件打开应用

在主进程展示的时候传递filePath

  mainWindow.on('ready-to-show', () => {//隐藏启动页if (loadingWindow && !loadingWindow?.isDestroyed()) {loadingWindow?.hide()loadingWindow?.removeAllListeners()loadingWindow?.destroy()}mainWindow.show()/*** @description 双击打开本地文件*/openFileFromDoubleClick(mainWindow)})

获取filePath并传递给render

export function openFileFromDoubleClick(mainWindow) {if (process.argv.length >= 2) {const argv = process.argv.slice(app.isPackaged ? 1 : 2)const filePath =argv.find((arg) => arg.endsWith('.krzj')) ||argv.find((arg) => arg.includes('--file'))?.split('=')[1]if (filePath && filePath.endsWith('.krzj')) {// 当页面加载完成后,获取到vue-ready事件后,发送open-file事件ipcMain.once('vue-ready', () => {mainWindow.webContents.send('open-file', filePath)})}}
}

2. 注册preload事件

 //双击打开文件onOpenFile: (callback: any) => ipcRenderer.on('open-file', callback),//消息传递send: (channel, data) => ipcRenderer.send(channel, data),

3. render接收信息

需要先通知主进程render加载完毕,才从主进程拿filePath,否则获取不到

onMounted(() => {// 在health接口返回后 获取双击打开的文件路径window.api.send('vue-ready')window.api.onOpenFile((event: any, path: string) => {if (path && route.path === '/file') {// 在当前页直接获取跳转openProjectFile(path)} else if (path && route.path !== '/file') {// 在非当前页则回来后获取跳转router.push('/file')openProjectFile(path)}})
})

4. 主进程创建通信服务器

// 锁定应用只能单列运行
const appSingleInstance = app.requestSingleInstanceLock()
if (!appSingleInstance) {// 第二个实例 - 连接第一个实例的服务器sendFilePathToFisrtInstance(PORT)app.quit()
} else {// 第一个实例 - 创建服务器 获取第二个实例发送的filepath 封装后不能再发送server = net.createServer((socket) => {socket.on('data', (data) => {mainWindow?.webContents.send('open-file', data.toString())})})server.listen(PORT)server.on('error', (err) => console.error('服务器错误:', err))
}

5. 第二个实例连接服务器

/*** @description 第二个实例 - 连接第一个实例的服务器* @export*/
export function sendFilePathToFisrtInstance(port: number) {const argv = process.argv.slice(app.isPackaged ? 1 : 2)const filePath =argv.find((arg) => arg.endsWith('.krzj')) ||argv.find((arg) => arg.includes('--file'))?.split('=')[1]if (filePath) {const client = net.createConnection({ port: port }, () => {client.write(filePath)client.end()})client.on('error', () => {})}
}

开发时如何本地测试打开多个文件

使用的是electron-vite,在package.json创建运行脚本,一条就是打开一个文件,可以开多个终端打开多个文件

 "open-file": "electron-vite dev -- --file \"D:/kr/untitled01.krzj\"","open-file1": "electron-vite dev -- --file \"D:/kr/untitled02.krzj\"","open-file2": "electron-vite dev -- --file \"D:/kr/untitled03.krzj\""

windows如何关联自定义文件关联启动

我是用的是electron-builder,然后在electron-builder.yml中配置就行,非常简单

# 设置自定义文件关联启动
fileAssociations:description: kingrayFile# 自定义文件后缀ext: krzj# 自定义文件图标icon: build/icons/win/icon.ico

文章转载自:

http://5SS4mOkO.yrbhf.cn
http://qzBWJG2m.yrbhf.cn
http://DLndI0OY.yrbhf.cn
http://cYsdQIxz.yrbhf.cn
http://SbjnMY7z.yrbhf.cn
http://90w0uty5.yrbhf.cn
http://J67mJ40D.yrbhf.cn
http://H9EejIli.yrbhf.cn
http://j610p56E.yrbhf.cn
http://vRUwGjcB.yrbhf.cn
http://zdEO4LGM.yrbhf.cn
http://2omDJRTA.yrbhf.cn
http://9dBXFhrk.yrbhf.cn
http://qhnA0pwh.yrbhf.cn
http://j71OtVWV.yrbhf.cn
http://h6lZvZqU.yrbhf.cn
http://3GgGuE0U.yrbhf.cn
http://I2gtO01K.yrbhf.cn
http://GiIcpzHc.yrbhf.cn
http://gP24IMGD.yrbhf.cn
http://uvsNnfRT.yrbhf.cn
http://7ms2cnK3.yrbhf.cn
http://DZsiJVRA.yrbhf.cn
http://faQ42TWG.yrbhf.cn
http://GJ4MpRPl.yrbhf.cn
http://nWyjmBlt.yrbhf.cn
http://OQ7ZNPKO.yrbhf.cn
http://db45JuQF.yrbhf.cn
http://A4pOH5GX.yrbhf.cn
http://9b0Iyw0e.yrbhf.cn
http://www.dtcms.com/a/248202.html

相关文章:

  • windows录频软件
  • 自己的服务器被 DDOS跟CC攻击了怎么处理,如何抵御攻击?
  • golang使用tail追踪文件变更
  • 目标检测标注格式
  • EFK架构日志采集系统
  • 国产智能体“双子星”:实在Agent vs Manus(核心架构与技术实现路径对比)
  • Screenpresso v2.1:轻量截图录屏工具安装使用指南
  • liquibase 集成 pt-online-schema-change
  • [Latex排版] 解决Something‘s wrong--perhaps a missing \item. 问题
  • Profinet转Modbus网关:破解热处理炉协议壁垒的温控通讯密码
  • 三网手机号实名认证功能-手机号实名认证接口-手机号身份核验
  • 叶片开关结构与工作原理
  • Git Switch 与 Git Restore 详解
  • kafka版本升级3.5.1-->3.9.1(集群或单体步骤一致)
  • 学习STC51单片机37(芯片为STC89C52RCRC)智能小车4(循迹小车、优化循迹小车解决转弯不平滑)
  • 基于深度学习的智能视频分析系统:技术与实践
  • (二十七)深度解析领域特定语言(DSL)第四章——词法分析:基于状态机的词法分析器
  • 【Code】COP FOR THE STRUCTURAL USE OF STEEL 2011 (2023 Edition)
  • 【递归、搜索与回溯】FloodFill算法(二)
  • leetcode21-合并两个有序链表
  • STM32外设学习之USB
  • 什么是 OpenSearch?- 比较 OpenSearch 及 Elasticsearch
  • jenkins连接git仓库
  • FreeSWITCH mod_curl 和 mod_xml_rpc 测试
  • Transformer 核心概念转化为夏日生活类比
  • 基于用户的协同过滤推荐算法实现(Java电商平台)
  • 基于物品的协同过滤推荐算法实现(Java电商平台)
  • visual studio小番茄插件某些快捷键失效
  • SQL Server 修改数据库名及物理数据文件名
  • OpenCV CUDA模块图像变形------对图像进行旋转操作函数rotate()