Electron学习笔记(六)

ops/2025/2/22 8:25:42/

文章目录

    • 七、系统
      • 5、托盘图标
        • (1)、设置托盘图标
        • (2)、托盘图标闪烁
        • (3)、托盘图标菜单
      • 6、剪切板
        • (1)、写入剪切板
        • (2)、读取剪切板
      • 7、系统通知
      • 8、其他
        • (1)、使用系统默认应用打开文件
        • (2)、接收拖拽到窗口中的文件
        • (3)、使用系统字体

相关笔记

笔记说明

文本为学习《Electron 实战 入门、进阶与性能优化 刘晓伦 著》时所记录的笔记 主要将书本上的案例运行一遍,针对原理部分并无相关记录。笔记记录于 2023年9月。

七、系统

5、托盘图标

(1)、设置托盘图标

更新 index.js 文件内容如下:

const {app,BrowserWindow,Tray} = require('electron');
let path = require('path');
let tray;
let win = null;app.on('ready', function() {win = new BrowserWindow({// ...});// 获取 图标路径let iconPath = path.join(__dirname, 'icon.ico');tray = new Tray(iconPath);// ...
});
(2)、托盘图标闪烁

更新 index.js 文件内容如下:

const {app,BrowserWindow,Tray} = require('electron');
let path = require('path');
let tray;let win = null;
let iconPath = path.join(__dirname, 'icon.ico');
let iconPath2 = path.join(__dirname, 'icon2.ico');
let flag = true;app.on('ready', function() {win = new BrowserWindow({// ...});tray = new Tray(iconPath);// ...
});setInterval(() => {if (flag) {tray.setImage(iconPath2);flag = false;} else {tray.setImage(iconPath);flag = true;}
}, 600)

效果展示:

效果展示

(3)、托盘图标菜单

更新 index.js 文件内容如下:

const { app, BrowserWindow, Tray, Menu } = require('electron');
let path = require('path');
let tray;let win = null;
app.on('ready', function () {win = new BrowserWindow({// ...});let iconPath = path.join(__dirname, 'icon.ico');tray = new Tray(iconPath);// 响应鼠标点击事件tray.on('click', function () {win.show();});// 鼠标右键菜单let menu = Menu.buildFromTemplate([{click() { win.show(); },label: '显示窗口',type: 'normal'}, {click() { app.quit(); },label: '退出应用',type: 'normal'}]);tray.setContextMenu(menu);// ...
});

效果展示:

效果展示


6、剪切板

(1)、写入剪切板

clipboard:该模块渲染进程和主进程都可以直接使用。

文本内容写入:更新 index.html 文件内容如下:

<input type="text" name="" id=""><script>let { clipboard } = require("electron");// 向剪切板写入文本clipboard.writeText("你好Text");  // 向剪切板写入HTML        // clipboard.writeHTML("<h1>你好HTML</h1>");        
</script>

图片内容写入:程序运行后可在word文档或聊天框中粘贴

<body><script>let path = require("path");let { clipboard, nativeImage } = require("electron");let imgPath = path.join(__dirname, "1.jpg");let img = nativeImage.createFromPath(imgPath);clipboard.writeImage(img);</script>
</body>

清空剪切板:

clipboard.clear();

(2)、读取剪切板

读取剪切板内的文本内容:更新 index.html 文件内容如下:

<body><input type="text" name="" id=""><script>let { clipboard } = require("electron");console.log(clipboard.readText());        // 读取剪切板的文本// clipboard.readHTML();        // 读取剪切板的HTML</script>
</body>

读取剪切板内的图片内容

<body><script>let { clipboard } = require("electron");// 获取在系统文件夹里复制的图片文件路径let filePath = clipboard.readBuffer('FileNameW').toString('ucs2')filePath = filePath.replace(RegExp(String.fromCharCode(0), 'g'), '');// 创建一个 img 元素写入页面let imgDom = document.createElement('img')imgDom.src = filePath;document.body.appendChild(imgDom);</script>
</body>

7、系统通知

更新 index.html 文件内容如下:

<body><script>Notification.requestPermission(function (status) {if (status === "granted") {let notification = new Notification('来自Electron程序的消息', {body: '测试系统消息发送'});notification.onclick = function () {alert('用户点击了系统消息');}} else {// 用户拒绝授权}});</script>
</body>

效果展示:

效果展示


8、其他

(1)、使用系统默认应用打开文件

shell:该模块可以被 Electron 中主进程和渲染进程直接使用。

使用 shell 模块打开默认浏览器:

<body><script>const { shell } = require("electron");shell.openExternal('https://www.baidu.com');</script>
</body>

使用 shell 模块打开 word 文档:

<body><script>const { shell } = require("electron");let openFlag = shell.openPath("D:\\桌面\\test.docx");console.log(openFlag);</script>
</body>

使用 shell 模块将文件移入垃圾箱:(说明:经测试,该代码在主进程中有效,但在渲染进程中会报错)

index.js 文件中添加以下内容:

const { shell } = require("electron");
let delFlag = shell.trashItem("D:\\桌面\\test.docx");console.log(delFlag);

关于 shell 模块更多详情参见:https://www.electronjs.org/zh/docs/latest/api/shell


(2)、接收拖拽到窗口中的文件

修改 index.html 文件内容如下:

<body><script>document.addEventListener('dragover', ev => {ev.preventDefault();console.log('请在此处放置文件');});document.addEventListener('drop', ev => {console.log(ev.dataTransfer.files);ev.preventDefault();});</script>
</body>

效果展示:

效果展示


(3)、使用系统字体
<div style="font:caption">这是我的标题</div>
<div style="font:menu">菜单中的字体</div>
<div style="font:message-box">对话框中的字体</div>
<div style="font:status-bar">状态栏中的字体</div>

效果展示:

效果展示



http://www.ppmy.cn/ops/40005.html

相关文章

数据库出现死锁的解决方法参考

死锁引起的原因一般是多个用户并发访问数据库导致的问题&#xff0c;或是因为某个进程挂死以后资源未释放导致的。通过onstat –p可查看deadlks项大于0即表示历史总计死锁次数。对于被锁的表进行操作的时候会出现-143 ISAM error: deadlock detected的错误。当其他会话访问此表…

autodl 上 使用 LLaMA-Factory 微调 中文版 llama3

autodl 上 使用 LLaMA-Factory 微调 中文版 llama3 环境准备创建虚拟环境下载微调工具 LLaMA-Factory下载 llama3-8B开始微调测试微调结果模型合并后导出vllm 加速推理 环境准备 autodl 服务器&#xff1a; https://www.autodl.com/console/homepage/personal 基本上充 5 块钱…

NPDP|传统行业产品经理如何跨越鸿沟,从用户角度审视产品

随着科技的飞速发展和互联网的普及&#xff0c;产品经理的角色已经从单纯的产品规划者逐渐转变为全方位的用户体验设计者。对于传统行业的产品经理来说&#xff0c;这是一个挑战与机遇并存的时代。他们不仅要面对激烈的市场竞争&#xff0c;还要学会如何跨越与新兴科技行业之间…

最少数量线段覆盖-华为OD

系列文章目录 文章目录 系列文章目录前言一、题目描述二、输入描述三、输出描述四、java代码五、测试用例 前言 本人最近再练习算法&#xff0c;所以会发布一些解题思路&#xff0c;希望大家多指教 一、题目描述 给定坐标轴上的一组线段&#xff0c;线段的起点和终点均为整数…

如何excel里面数据格式设置为utf-8

在Excel中&#xff0c;直接设置数据的编码格式为UTF-8是不直接支持的&#xff0c;因为Excel文件&#xff08;如.xlsx或.xls&#xff09;本身并不直接具有一个“编码”属性&#xff0c;像文本文件&#xff08;如.txt或.csv&#xff09;那样。然而&#xff0c;你可以通过保存Exce…

在ubuntu服务器上创建一个 Systemd 服务单元文件以启动和管理您的应用程序

在Ubuntu服务器上创建一个环境的配置服务文件 创建服务单元文件&#xff1a; 在服务器上使用文本编辑器&#xff08;如 Nano 或 Vim&#xff09;创建一个新的服务单元文件。 例如&#xff0c;使用以下命令创建一个名为 my-webapi.service 的新文件&#xff1a; sudo vim /et…

MySQL报错:You can‘t specify target table ‘user‘ for update in FROM clause

一、问题 执行delete语句时&#xff0c;报错“You can’t specify target table ‘user’ for update in FROM clause” 翻译为“不能先select出同一表中的某些值&#xff0c;再update这个表(在同一语句中)”&#xff0c;即delete的目标表不能在其直接子查询中存在&#xff0…

pandas DataFrame 常用遍历方法

在Pandas中&#xff0c;可以使用多种方法遍历DataFrame中的数据。以下是几种常见的方法&#xff1a; 基于索引遍历DataFrame的每一行。 基于行号遍历DataFrame的每一行, 该方式通过行号获取行数据信息&#xff0c;格式为Series&#xff0c;无法获取改行的index信息。 使用iterr…