创建任务栏应用程序。单击任务栏图标可切换应用程序的可见性。当我隐藏然后显示它时,窗口似乎每次都向右和向右移动一点。
垃圾邮件单击托盘图标一百次,使我的窗口消失了一半的宽度。
const {app, BrowserWindow, ipcMain, Tray} = require('electron');
const path = require('path');
let tray = undefined;
let window = undefined;
const sceneWidth = 418;
const sceneHeight = 700;
const taskBarHeight = 45;
let toggleWindow = () => {
window.isVisible() ? hideWindow() : showWindow();
};
let hideWindow = () => {
console.log("[Info] Application frame hidden.");
window.hide();
};
let showWindow = () => {
console.log("[Info] Application frame visible.");
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.show();
};
app.on('ready', () => {
console.log("[Info] Application ready.");
createTray();
createWindow();
const windowProperties = getWindowPosition();
console.log("[Info] Screen resolution: " + windowProperties.dx + " x " + windowProperties.dy);
});
let createTray = () => {
tray = new Tray(path.join('assets/img/TrayIcon.png'));
tray.on('click', function (event) {
toggleWindow();
});
};
const getWindowPosition = () => {
const electron = require('electron');
const screenElectron = electron.screen;
const mainScreen = screenElectron.getPrimaryDisplay();
const dimensions = mainScreen.size;
const x = dimensions.width - sceneWidth;
const y = (dimensions.height - (sceneHeight - taskBarHeight));
return {
x: x,
y: y,
dx: dimensions.width,
dy: dimensions.height
};
};
const createWindow = () => {
window = new BrowserWindow({
width: sceneWidth,
height: sceneHeight,
show: false,
frame: false,
skipTaskbar: true,
fullscreenable: false,
resizable: false,
transparent: false,
webPreferences: {
backgroundThrottling: false
}
});
window.loadURL(`file://${path.join(__dirname, 'index.html')}`)
};
我看不到会导致此问题的原因。 也,当不聚焦窗口时,该应用程序没有隐藏,因此要再次显示它,您必须单击两次。试图解决此问题,但没有设法解决。当您通过单击隐藏窗口时,就会调用模糊事件,这是我发现的,但无法使其正常工作(试图将hideWindow()
函数添加到模糊事件中,但后来我无法隐藏单击该窗口,此后它从不可见变为可见)。