【问题标题】:Auto Size Main Window自动调整大小主窗口
【发布时间】:2021-08-04 23:40:02
【问题描述】:

我正在使用 Go-Astilectron(Go 的 Electron 框架)创建一个应用程序。

我的应用有一个无框透明窗口,应根据其内容调整大小。据我所知,我有义务在 Electron 中设置窗口的 widthheight 属性,否则它将默认为 800x600。

我想知道的是,Electron 是否有办法根据其内容自动调整窗口大小。

我可以使用“一刀切”的方法,但是由于我的窗口是无框透明的,它的某些部分最终会在其他东西之上,并且由于没有点击,用户会感到困惑以为他在点击其他应用程序,而他实际上是在点击我的应用程序。

这是我创建窗口的代码:

var w *astilectron.Window

log.Debug("Starting astilectron...")
window := []*bootstrap.Window{{
    Homepage: "http://localhost:3000",
    Adapter: func(i *astilectron.Window) {
        w = i
    },
    Options: &astilectron.WindowOptions{
        MinHeight:          astilectron.PtrInt(50),
        MinWidth:           astilectron.PtrInt(50),
        AlwaysOnTop:         astilectron.PtrBool(true),
        Transparent:         astilectron.PtrBool(true),
        Closable:            astilectron.PtrBool(false),
        Minimizable:         astilectron.PtrBool(false),
        Frame:                   astilectron.PtrBool(false),
        Movable:                 astilectron.PtrBool(true),
        SkipTaskbar:         astilectron.PtrBool(false),
        Resizable:           astilectron.PtrBool(false),
    },
}}

go func() {
    err := bootstrap.Run(bootstrap.Options{
        Windows: window,
        Debug: true,
    })

    if err != nil {
        log.WithError(err).Fatal("Error with Astilectron")
    }
}()

【问题讨论】:

    标签: go electron


    【解决方案1】:

    尝试设置UseContentSize: astilectron.PtrBool(true)

    UseContentSize 根据页面大小调整窗口大小。这就是它在文档中所说的:

    宽度和高度将作为网页的大小,这意味着实际窗口的大小将包括窗口框架的大小,并且会稍大一些。默认为假。

    Docs

    【讨论】:

    • 嗨,我试图在我的窗口上设置这个标志,但它仍然不起作用。窗口大小默认为 800x600。注意:我尝试了设置和不设置WidthHeight
    【解决方案2】:

    尝试做这样的事情

      const { screen } = require('electron');
      const size = screen.getPrimaryDisplay().workAreaSize;
      // Create the browser window.
      mainWindow = new BrowserWindow({
       x: 0,
       y: 0,
       width: size.width,
      height: size.height
    });
    

    希望它会起作用。

    【讨论】:

    • 我很抱歉,但我认为这不会起作用,因为我是在 Go 中创建我的窗口,而不是在 Javascript 中......我会更新我的问题。
    • const size = screen.getPrimaryDisplay().workAreaSize;上面的代码居然会填满整个屏幕!
    猜你喜欢
    • 2017-03-12
    • 2017-01-20
    • 1970-01-01
    • 2011-12-21
    • 2012-01-04
    • 2016-09-14
    • 2017-04-29
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多