How to set window position and make it unresizable in Go walk

huangapple go评论86阅读模式
英文:

How to set window position and make it unresizable in Go walk

问题

我正在尝试弄清楚如何使用Go GUI库 walk 的基础知识。

首先,我想要能够:

  1. 控制窗口的位置,希望能够以其他语言提供的方式进行操作(居中显示在屏幕上、居中显示在父窗口上、使用精确坐标等)。
  2. 使窗口不可调整大小

以下是我目前的代码,我原本希望通过MaxSize的声明来解决第二个问题,但实际上并没有起作用。我还在寻找一种类似于Position的声明,但是找不到任何对我来说有意义的内容。

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}, // 不起作用
		// Position: ...        // 不存在
		Layout: VBox{},
		Children: []Widget{
			Label{Text: "Hello World"},
		},
	}.Run()
}
英文:

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

得分: 4

看起来walk在其自己的API中并没有提供这个功能。然而,walk是基于win创建的,实际上只是Go语言对WinAPI的绑定。因此,你可以轻松调用任何WinAPI函数。例如,要在指定位置显示一个主窗口,可以调用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) // 获取当前样式
	newStyle := defaultStyle &^ win.WS_THICKFRAME                 // 移除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()
}

要使窗口不可调整大小,你需要取消WS_THICKFRAME样式,就像示例中所示的那样。更多信息可以在这里找到。

英文:

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.

huangapple
  • 本文由 发表于 2014年9月20日 22:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/25949966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定