HoRain云--Electron API 速查与配置模板 主进程 API 速查表模块功能描述常用方法说明app控制应用生命周期app.on(ready)、app.quit()、app.getPath(userData)管理应用启动、退出、路径、协议注册等BrowserWindow创建与管理窗口new BrowserWindow(options)、win.loadFile()、win.loadURL()、win.webContentsElectron 应用的主要 UI 容器内部运行渲染进程ipcMain主进程通信接口ipcMain.on(channel, callback)、ipcMain.handle(channel, handler)监听渲染进程发来的消息或调用请求Menu创建应用菜单Menu.buildFromTemplate(menuTemplate)、Menu.setApplicationMenu(menu)可自定义菜单项、快捷键、右键菜单Tray系统托盘管理new Tray(icon)、tray.setToolTip()、tray.setContextMenu(menu)在任务栏显示图标常用于后台常驻应用Dialog系统原生对话框dialog.showOpenDialog()、dialog.showSaveDialog()、dialog.showMessageBox()文件选择、保存、消息提示等系统交互Notification系统通知new Notification({ title, body }).show()发送系统原生通知可带点击回调Shell调用系统外部资源shell.openExternal(url)、shell.showItemInFolder(path)打开外部网页、文件夹、执行文件Protocol注册自定义协议protocol.registerFileProtocol()、protocol.handle()用于自定义资源加载或深度链接Session管理会话、缓存、Cookiesession.defaultSession.cookies.get()、clearCache()对浏览数据和请求做底层控制Net原生 HTTP/HTTPS 网络请求net.request(options)更底层的网络模块性能优于fetchPowerMonitor监控系统电源状态powerMonitor.on(suspend)、on(resume)监听电脑睡眠、唤醒、锁屏等状态Clipboard剪贴板操作clipboard.readText()、clipboard.writeText()支持跨平台复制粘贴文本、图片等GlobalShortcut注册全局快捷键globalShortcut.register(CommandOrControlX, fn)跨窗口、后台生效的快捷键渲染进程 API 速查表模块功能描述常用方法说明ipcRenderer与主进程通信ipcRenderer.send()、ipcRenderer.invoke()、ipcRenderer.on()用于渲染进程发送、接收、调用主进程事件webFrame控制当前页面内容webFrame.setZoomFactor()、webFrame.insertCSS()动态修改页面缩放、样式contextBridge安全暴露 APIcontextBridge.exposeInMainWorld(api, {...})在启用contextIsolation时安全地与主进程交互desktopCapturer捕获屏幕内容desktopCapturer.getSources({ types: [window, screen] })用于截图或屏幕录制功能Notification通知 APInew Notification(标题, { body: 内容 })使用浏览器标准 Notification 发送通知Clipboard剪贴板操作navigator.clipboard.readText()可访问系统剪贴板部分功能受限制Web Storage / IndexedDB本地存储localStorage.setItem()、indexedDB.open()保存轻量或结构化数据Fetch / Axios网络请求fetch(url)、axios.get()用于访问远程接口、加载资源DOM / Window页面控制document.querySelector()、window.open()所有 Web API 均可使用常用辅助模块速查表模块主要功能示例fs文件操作fs.readFileSync(path)、fs.writeFile(path, data)path路径拼接path.join(__dirname, index.html)os系统信息os.platform()、os.homedir()child_process执行子进程exec(ls, callback)electron-store持久化配置store.set(key, value)、store.get(key)sqlite3 / better-sqlite3本地数据库db.prepare(SELECT * FROM notes).all()axios / node-fetch网络请求axios.post(url, data)node-gyp原生模块编译node-gyp rebuild配置文件模板与说明package.json 模板与说明{ name: my-electron-app, version: 1.0.0, description: 示例 Electron 应用, main: main.js, scripts: { start: electron ., pack: electron-builder --dir, dist: electron-builder }, devDependencies: { electron: ^33.0.0, electron-builder: ^24.6.0 }, build: { appId: com.example.myelectronapp, productName: MyElectronApp, directories: { output: dist }, files: [ main.js, preload.js, renderer/**/* ], win: { target: nsis, icon: assets/icon.ico }, mac: { category: public.app-category.productivity, icon: assets/icon.icns }, linux: { target: AppImage, icon: assets/ } } }说明字段含义name / version / description应用名称、版本号与描述信息main主进程入口文件必须scripts定义命令脚本npm start启动npm run dist打包devDependencies开发依赖Electron、打包工具等buildelectron-builder的配置项appId应用唯一 ID用于签名和更新识别productName打包后显示的应用名称directories.output打包输出路径files指定要打包进应用的文件和目录win/mac/linux各平台独立打包配置图标、目标格式electron-builder.yml 模板与说明appId: com.example.myelectronapp productName: MyElectronApp directories: output: dist files: - main.js - preload.js - renderer/**/* win: target: nsis icon: assets/icon.ico mac: target: dmg icon: assets/icon.icns linux: target: AppImage icon: assets/ publish: provider: github repo: my-electron-app owner: username说明配置项说明appId / productName应用唯一标识与名称directories.output打包输出目录files打包文件列表支持通配符win / mac / linux不同系统的构建目标与图标配置publish自动更新时的发布源配置如 GitHub ReleasesTypeScript tsconfig.json 模板与说明{ compilerOptions: { target: ES2020, module: commonjs, strict: true, outDir: ./dist, rootDir: ./src, esModuleInterop: true, moduleResolution: node, resolveJsonModule: true, skipLibCheck: true }, include: [src/**/*], exclude: [node_modules] }说明选项含义target编译目标 JavaScript 版本module模块系统Electron 主进程一般使用commonjsoutDir / rootDir输出与源码目录strict启用严格类型检查esModuleInterop允许默认导入非 ES 模块resolveJsonModule支持导入 JSON 文件skipLibCheck跳过声明文件检查加快编译速度

相关新闻

最新新闻

AltoClef命令大全:30个实用指令让Minecraft自动化变得简单

AltoClef命令大全:30个实用指令让Minecraft自动化变得简单

AltoClef命令大全:30个实用指令让Minecraft自动化变得简单 【免费下载链接】altoclef Plays block game 项目地址: https://gitcode.com/gh_mirrors/al/altoclef 想要在Minecraft中实现自动化操作吗?AltoClef是一个强大的Minecraft自动化机器人&a…

2026/7/16 18:27:06
鸿蒙三方库 | harmony-utils之NetworkUtil网络连接属性详解

鸿蒙三方库 | harmony-utils之NetworkUtil网络连接属性详解

前言 网络连接属性包括连接类型、链路信息等详细数据,对网络诊断和优化有重要参考价值。pura/harmony-utils 的 NetworkUtil 封装了网络连接属性查询方法。本文将从API说明、代码实战、进阶用法、常见问题等多个维度进行全面讲解,帮助开发者快速掌握并应…

2026/7/16 18:27:06
基于SSM框架的在线宠物领养平台的设计与实现

基于SSM框架的在线宠物领养平台的设计与实现

一、功能实现截图展示1、运行系统后主页面:运行后首页如下所示,设置了几张轮播图(可以自动播放,也可以手动点击查看),此处截取部分展示。2、宠物知识模块:点击养猫与养狗需要注意的地方&#xf…

2026/7/16 18:27:06
HarmonyOS 平板适配实战:双栏布局、断点策略与状态保持

HarmonyOS 平板适配实战:双栏布局、断点策略与状态保持

HarmonyOS 平板适配实战:双栏布局、断点策略与状态保持 手机页面直接拉到平板上,常见结果是左右留白巨大、内容行太长、返回路径不清楚。平板不是“大号手机”,它更适合双栏、主从详情、悬浮面板和更高的信息密度。真正的平板适配&#xff0c…

2026/7/16 18:27:06
如何快速集成TXScrollLabelView?3种安装方式对比与实战教程

如何快速集成TXScrollLabelView?3种安装方式对比与实战教程

如何快速集成TXScrollLabelView?3种安装方式对比与实战教程 【免费下载链接】TXScrollLabelView 🌭TXScrollLabelView, the best way to show & display information such as adverts / boardcast / onsale e.g. with a customView. 项目地址: htt…

2026/7/16 18:27:06
PDF文档处理架构优化:PDF Arranger核心模块设计与实现

PDF文档处理架构优化:PDF Arranger核心模块设计与实现

PDF文档处理架构优化:PDF Arranger核心模块设计与实现 【免费下载链接】pdfarranger Small python-gtk application, which helps the user to merge or split PDF documents and rotate, crop and rearrange their pages using an interactive and intuitive graph…

2026/7/16 18:22:06

月新闻