美文网首页
electron右键打开文件

electron右键打开文件

作者: 我叫Aliya但是被占用了 | 来源:发表于2020-11-17 10:50 被阅读0次
实现这样的功能
// package.json
build: {
  appId: 'com.gitee.xxx',
  fileAssociations: {
    ext: 'md',
    role: 'Viewer',
  },
}
// background.js
let openFilePath = '';

app.on('open-file', (event, path) => {
  // 当用户想要在应用中打开一个文件时发出。
  // 事件通常在应用已经打开,并且系统要再次使用该应用打开文件时发出。
  // 也会在一个文件被拖到 dock 并且还没有运行的时候发出。
  event.preventDefault();
  if (win) SendMsgToRenderer(win.webContents, 'user-open-file', path);
  else openFilePath = path;
});

app.on('ready', async () => {
  win = createWindow();
  openFilePath &&
    SendMsgToRenderer(win.webContents, 'user-open-file', openFilePath);
});
export function SendMsgToRenderer<T extends keyof NoticeChannel>(
  webContents: Electron.WebContents,
  cmd: T,
  params: NoticeChannel[T],
): void {
  if (webContents.isLoading())
    webContents.on('did-finish-load', () => {
      webContents.send(cmd, params);
    });
  else webContents.send(cmd, params);
}
// 渲染进程 
ipcRenderer.on('user-open-file', (e: unknown, newfilepath: string) => {
      this.OpenDB(newfilepath);
}); 

参考: https://blog.csdn.net/WuQingLaoXingXing/article/details/105841278

相关文章

网友评论

      本文标题:electron右键打开文件

      本文链接:https://www.haomeiwen.com/subject/hacxiktx.html