duanfeigui6655 2014-09-20 14:47
浏览 1786

如何在Go walk中设置窗口位置并使其无法调整大小

I am trying to figure out the basics of working with the Go GUI library, walk.

For starters, I would like to be able to

  1. Control the window's position
    hopefully in a way similar to what other languages provide (center on screen, center to parent, exact coordinates etc).
  2. Make the window unresizable

This is the code I have, I was hoping the MaxSize declaration would solve the second problem, but it doesn't, and I was looking for some sort of a Position declaration but couldn't find anything that makes sense to me.

package main

import (
    // "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
)

func main() {
    MainWindow{
        Title:   "Test",
        MinSize: Size{300, 50},
        MaxSize: Size{300, 50}, // Doesn't work
        // Position: ...        // Doesn't exist
        Layout: VBox{},
        Children: []Widget{
            Label{Text: "Hello World"},
        },
    }.Run()
}
  • 写回答

1条回答 默认 最新

  • douluyezhen4512 2017-08-12 17:02
    关注

    It looks like walk doesn't provide this functionality in its own API. However, walk created on top of win which actually is just a Go bindings to WinAPI. So you can easily call any WinAPI functions. For example, to show a main window on a specified position one can call SetWindowPos():

    package main
    
    import (
        "github.com/lxn/win"
        "github.com/lxn/walk"
        . "github.com/lxn/walk/declarative"
    )
    
    const (
        SIZE_W = 600
        SIZE_H = 400
    )
    
    type MyMainWindow struct {
        *walk.MainWindow
    }
    
    func main() {
        mw := new(MyMainWindow)
    
        MainWindow{
            Visible: false,
            AssignTo: &mw.MainWindow,
        }.Create()
    
        win.SetWindowLong(mw.Handle(), win.GWL_STYLE, win.WS_BORDER) // removes default styling
    
        xScreen := win.GetSystemMetrics(win.SM_CXSCREEN);
        yScreen := win.GetSystemMetrics(win.SM_CYSCREEN);
        win.SetWindowPos(
            mw.Handle(),
            0,
            (xScreen - SIZE_W)/2,
            (yScreen - SIZE_H)/2,
            SIZE_W,
            SIZE_H,
            win.SWP_FRAMECHANGED,
        )
        win.ShowWindow(mw.Handle(), win.SW_SHOW);
    
        mw.Run()
    }
    

    And to make window unresizable you need to unset WS_THICKFRAME style. More information can be found here.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?