javascript - 动态调整 Electron 应用程序窗口的大小

标签 javascript electron atom-editor

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/

相关文章:

从 "main"文件夹导入时 Golang 导入路径无效

atom-editor - 将智能感知添加到 Atom

javascript - 在颜色和尺寸之前更改“添加到购物车”按钮的文本

json - 保存的 JSON 文件被覆盖

javascript - 按嵌套属性对字典的键进行排序

javascript - 如何在 Electron 和 Svelte 中启用移动无框 window 的功能?

node.js - 为什么我在将我的 Electron 项目更新到最新版本后会看到 "Electron Security Warning"?

node.js - 如何在 scala.js 中提供和使用 require.js 模块(和扩展类)

javascript - 如何显示一个div直到只关闭一次

javascript - 检查重复 id 的 html javascript 代码