Main.js
const {app, BrowserWindow} = require('electron');
const path = require('path');
const url = require('url');
let win;
app.on('ready', createWindow);
function createWindow () {
// Create the browser window.
win = new BrowserWindow();
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'main.html'),
protocol: 'file:',
slashes: true
}));
// Open the DevTools.
win.webContents.openDevTools();
// Emitted when the window is closed.
win.on('closed', () => {
win = null;
});
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MyApp</title>
</head>
<body>
<h1>Hello !</h1>
<button id="resizeBtn">Resize</button>
</body>
</html>
我希望如果用户单击按钮,则应触发一个函数并更改已打开的主窗口的大小。我知道 win.setSize(width,height)
可以做到这一点但我不知道如何将其放入函数中。我不知道如何在 main.js 中获取 Button 的引用以在其上添加事件监听器或如何获取当前窗口并应用一些操作。我是 Electron 新手!
谢谢
最佳答案
简短回答(来自移动设备):使用 ipcRender 模块将事件发送到 main 并调用调整大小方法。
长答案: 我找不到 Electron Playground !
// In renderer process (web page).
const {ipcRenderer} = require('electron')
const myResizeBtn = document.getElementById('resizeBtn')
myResizeBtn.addEventListener('click', function () {
ipcRenderer.send('resize-me-please')
})
// in main.js
const {ipcMain} = require('electron')
ipcMain.on('resize-me-please', (event, arg) => {
win.setSize(width,height)
})
关于javascript - 动态调整 Electron 应用程序窗口的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564319/