【发布时间】:2018-03-15 19:33:55
【问题描述】:
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 的引用以在其上添加事件侦听器或如何获取当前窗口并应用一些操作。我是电子新手!
谢谢
【问题讨论】:
标签: javascript electron atom-editor