【问题标题】:How do I get the OS window position in electron?如何在电子中获取操作系统窗口位置?
【发布时间】:2019-04-08 00:11:15
【问题描述】:

我正在尝试让一个电子应用程序在屏幕上与上次关闭时相同的位置重新打开。

为此,我有一个配置文件,用于记录窗口关闭时的边界。

function set(settingKey, settingValue) {
  nconf.set(settingKey, settingValue);
  nconf.save();
};

mainWindow.on('close', function () {
  config.set('bounds', mainWindow.getBounds());
});

但是当我重新启动应用程序并通过指定 xywidthheight 选项或调用 @ 来设置 mainWindow 的位置时987654328@:

mainWindow.setBounds(config.get('bounds'));

窗口看起来比以前低了一点。我发现我得到的y 值没有考虑到窗口的标题栏高度。

This question 类似,但解决方案会导致相同的问题。

我试过了:

  • mainWindow.getPosition
  • mainWindow.getContentBounds 加上setContentBounds
  • electron.screen.getDisplayMatching(mainWindow.getBounds()).bounds

无济于事。前两种方法给了我完全相同的结果。最后一个给{ x: 0, y: 0, width: 1920, height: 1080 }

有人知道如何在电子中获取操作系统窗口位置吗?

如果它有帮助,我在 Wayland (Gnome 3.32)。

【问题讨论】:

    标签: user-interface position electron


    【解决方案1】:

    看起来像是 Linux 上一个长期未解决的错误:

    On Linux, returned position of fixed BrowserWindow gets unexpectedly modified

    例如:

    const { app, BrowserWindow } = require ('electron');
    let mainWindow = null;
    function onAppReady ()
    {
        let position = [ 200, 100 ];
        console.log ('init:', position);
        mainWindow = new BrowserWindow ({ x: position[0], y: position[1], width: 800, height: 600, show: false });
        console.log ('new:', mainWindow.getPosition ());
        mainWindow.loadURL (`file://${__dirname}/index.html`);
        mainWindow.on ('ready-to-show', () => { 
            mainWindow.show ();
            console.log ('show:', mainWindow.getPosition ()); });
        mainWindow.on ('close', () => { console.log ('close:', mainWindow.getPosition ()); });
        mainWindow.on ('closed', () => { app.quit (); });
    }
    app.on ('ready', onAppReady);
    

    结果:

    Linux Mint:

    init: [ 200, 100 ]
    new: [ 200, 100 ]
    show: [ 201, 125 ]
    close: [ 201, 125 ]
    

    Ubuntu:

    init: [ 200, 100 ]
    new: [ 200, 100 ]
    show: [ 200, 100 ]
    close: [ 200, 128 ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      相关资源
      最近更新 更多