const { app, screen, ipcMain, Menu, Tray, BrowserWindow, nativeImage } = require("electron");
// 系统托盘方法
const createTray = () => {
const icon = nativeImage.createFromPath(path.resolve(__dirname, "../public/icon/icon@16.png"));
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{ label: "TaroPet v1.0", type: "normal", enabled: false, icon: icon },
{ type: "separator" },
{
label: "重新加载",
type: "normal",
click: () => {
mainWindow.reload();
},
},
{
label: "退出",
type: "normal",
click: () => {
tray.destroy(); // 销毁托盘
app.quit(); // 退出应用程序
},
},
]);
tray.setToolTip("TaroPet v1.0");
tray.setContextMenu(contextMenu);
}
app.whenReady().then(() => {
createMainWindow();
createTray();
});
const { app, screen, ipcMain, Menu, Tray, BrowserWindow, nativeImage } = require("electron");
const path = require("path");
// 主窗口
let mainWindow = null;
// 拖拽的初始位置
let drapPosition = { x: 0, y: 0 };
// 拖拽定时器
let drapTimer = null;
const createMainWindow = () => {
// 获取当前桌面的宽度和高度
const size = screen.getPrimaryDisplay().workAreaSize;
const { width, height } = size;
mainWindow = new BrowserWindow({
width: 390,
height: 390,
// 起始位置是屏幕宽度减去窗口宽度,再减去10个像素
x: width - 390 - 10,
y: height - 390 - 10,
// 隐藏菜单栏
autoHideMenuBar: true,
// 设置为透明窗口
transparent: true,
// 隐藏窗口边框
frame: false,
// 窗口置顶
alwaysOnTop: true,
// 隐藏任务栏图标
skipTaskbar: true,
// 禁止改变窗口大小
resizable: false,
// 先隐藏窗口
show: false,
// Preload 脚本
webPreferences: {
preload: path.resolve(__dirname, "../_preload/preload.js"),
},
});
// 允许鼠标穿透
mainWindow.setIgnoreMouseEvents(true, { forward: true });
// 开启调试工具
mainWindow.webContents.openDevTools();
mainWindow.loadFile(path.resolve(__dirname, "../index.html"));
mainWindow.on("ready-to-show", () => {
mainWindow.show();
});
};
// 系统托盘方法
const createTray = () => {
const icon = nativeImage.createFromPath(path.resolve(__dirname, "../public/icon/icon@16.png"));
tray = new Tray(icon);
const contextMenu = Menu.buildFromTemplate([
{ label: "TaroPet v1.0", type: "normal", enabled: false, icon: icon },
{ type: "separator" },
{
label: "重新加载",
type: "normal",
click: () => {
mainWindow.reload();
},
},
{
label: "退出",
type: "normal",
click: () => {
tray.destroy(); // 销毁托盘
app.quit(); // 退出应用程序
},
},
]);
tray.setToolTip("TaroPet v1.0");
tray.setContextMenu(contextMenu);
}
app.whenReady().then(() => {
createMainWindow();
createTray();
});
// 鼠标移动监听,用于判断是否需要穿透
ipcMain.on("mouse-move", (event, obj) => {
if (obj.ignore) {
mainWindow.setIgnoreMouseEvents(true, { forward: true });
} else {
mainWindow.setIgnoreMouseEvents(false);
}
});
// 桌宠拖动开始,记录点击位置,让窗口粘着鼠标
ipcMain.on("mouse-drap-start", (event, obj) => {
drapPosition = {
x: obj.x,
y: obj.y,
};
drapTimer = setInterval(() => {
const { x, y } = screen.getCursorScreenPoint();
mainWindow.setPosition(x - drapPosition.x, y - drapPosition.y);
}, 10);
});
// 桌宠拖动结束,也就是再按一下右键,定时器清空
ipcMain.on("mouse-drap-end", (event, obj) => {
clearInterval(drapTimer);
});
此页面不支持夜间模式!
已进入夜间模式!
已进入普通模式!
搜索框不允许为空
签到成功!经验+5!芋圆币+2!
签到失败!今日已签到!
需要登录社区账号才可以进入!