DannyB
DannyB

Reputation: 14776

How to set window position and make it unresizable in 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()
}

Upvotes: 3

Views: 3245

Answers (1)

Ivan Velichko
Ivan Velichko

Reputation: 6709

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()

    defaultStyle := win.GetWindowLong(mw.Handle(), win.GWL_STYLE) // Gets current style
    newStyle := defaultStyle &^ win.WS_THICKFRAME                 // Remove WS_THICKFRAME
    win.SetWindowLong(mw.Handle(), win.GWL_STYLE, newStyle)

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

To make window unresizable you need to unset WS_THICKFRAME style, as shown in the example. More information can be found here.

Upvotes: 4

Related Questions