【问题标题】:Re-size electron app window dynamically动态调整电子应用程序窗口的大小
【发布时间】: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


    【解决方案1】:

    简短回答(来自手机):使用 ipcRender 模块将事件发送到 main 并调用 resize 方法。

    长答案: 我找不到电子游乐场!

    // 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)
    })
    

    【讨论】:

    • const {ipcRenderer} = electron; ipcRenderer.onData('resize', function (e, x, y) { win.setSize(x, y); }); const electron = require('electron'); const {ipcRenderer} = electron; let btn = document.querySelector('button'); btn.addEventListener('click', function (e) { e.preventDefault(); ipcRenderer.send('resize', 600, 800); });
    • 看看这个page
    • 谢谢!我已经发现了我在代码中犯的错误!我在 main.js 中使用 ipcRenderer 而不是使用 ipcMain !但谢谢我接受了你的回答!
    • 你在哪里定义“赢”?
    • main.js 第 5 行
    【解决方案2】:

    现在您可以在代码中获取当前的 BrowserWindow,然后像下面这样调整它的大小:

    const {remote} = require('electron');
    
    const win = remote.getCurrentWindow();
    
    let height = 400;
    let width = 200; 
    let animate = false;
    
    win.setSize(width, height, animate);

    关于 BrowserWindow.setSize(),它有三个参数。

    1. 宽度整数
    2. 高度整数
    3. 动画布尔(可选)macOS

    您可以在官方文档https://www.electronjs.org/docs/api/browser-window中找到更多关于BrowserWindow setSize方法的信息。

    【讨论】:

    • remote 已被弃用,因此这可能不是人们应该使用的方法。
    【解决方案3】:

    使用这个, 这行得通

    
        function createWindow() {
          // Create the browser window.
          const mainWindow = new BrowserWindow({ useContentSize: true });
          const view = new BrowserView();
          mainWindow.setBrowserView(view);
          view.setBounds({ x: 0, y: 0, width: 800, height: 600 });
          view.setAutoResize({ width: true, height: true });
          view.webContents.loadURL("https://youtube.com");
        }
    
    

    【讨论】:

      【解决方案4】:

      它就像 JS 中的魅力,打字稿

      <!DOCTYPE html lang="en">
          <body>
              <script>
                  function resize() {
                      var ipc = window.require("electron").ipcRenderer;
                      ipc.send("resize-window", 400, 200);
                  }
              </script>
              <div>
                <button onclick="resize()">Reload again</button>
              </div>
          </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2021-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 2021-04-06
        相关资源
        最近更新 更多