lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.86k stars 887 forks source link

Can you help me improve the example? #432

Open katomasa1254 opened 5 years ago

katomasa1254 commented 5 years ago

Hi I'm Japanese, so sorry if my English is wrong. And this is my first time to use github, so sorry if manners are wrong.

I'm try to display GUI window with WALK(github.com/lxn/walk). But there is a problem dose not go well.

If you run this code, maybe the window displayed upper left of screen. I want to set the position of the window in center of screen. What should i do? Please improve this code.

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

type MyLoadWindow struct { walk.MainWindow progressBar walk.ProgressBar }

func Main() { mw := &MyLoadWindow{}

// 画面情報設定
MW := MainWindow{
    AssignTo: &mw.MainWindow, // Widgetを実体に割り当て
    Title:    "コンピュータの情報を取得中",
    Size: Size{
        300,
        100},
    Font:   Font{PointSize: 12},
    Layout: VBox{},

    Children: []Widget{ // ウィジェットを入れるスライス

        ProgressBar{
            AssignTo:    &mw.progressBar,
            MarqueeMode: true,
        },
    },
}

if _, err := MW.Run(); err != nil {
    println("Error")
    return
}

} `

lxn commented 5 years ago

Hi, thanks for giving walk a try!

Over the course of the next few days I hope to add something to make this easy. When it's done, I will let you know.

katomasa1254 commented 5 years ago

Hi, Thank you for replying.

It is my first time to use GO, so I was in trouble to do anything. But "wark" make me easy to used GUI for windows. I am grateful to you for the creator.

And I am even more happy if improvement is made.

wshon commented 5 years ago

I have a solution. I don't know if the teacher can help you.

package main

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

type MyLoadWindow struct {
    *walk.MainWindow
    progressBar *walk.ProgressBar
}

func main() {
    mw := &MyLoadWindow{}

    // 画面情報設定
    if err := (MainWindow{
        AssignTo: &mw.MainWindow, // Widgetを実体に割り当て
        Title:    "コンピュータの情報を取得中",
        Size:     Size{Width: 300, Height: 100},
        Visible:  false,
        Font:     Font{PointSize: 12},
        Layout:   VBox{},

        Children: []Widget{ // ウィジェットを入れるスライス

            ProgressBar{
                AssignTo:    &mw.progressBar,
                MarqueeMode: true,
            },
        },
    }).Create(); err != nil {
        log.Print(err)
    }

    screenX := int(win.GetSystemMetrics(win.SM_CXSCREEN))
    screenY := int(win.GetSystemMetrics(win.SM_CYSCREEN))
    if err := mw.MainWindow.SetBounds(walk.Rectangle{
        X:      (screenX - mw.MainWindow.Width()) / 2,
        Y:      (screenY - mw.MainWindow.Height()) / 2,
        Width:  mw.MainWindow.Width(),
        Height: mw.MainWindow.Height(),
    }); err != nil {
        log.Print(err)
        return
    }

    mw.MainWindow.SetVisible(true)
    mw.MainWindow.Run()

}
wshon commented 5 years ago

Getting screen size information through win.GetSystemMetrics(win.SM_CXSCREEN) and win.GetSystemMetrics(win.SM_CYSCREEN), The position of the window is reset according to the size information obtained. But before you reset the window, you need to hide it, otherwise there will be flickering。

katomasa1254 commented 5 years ago

Hi, Thank you for the advice! I made the following code based on your opinion.

` func Main() { mw := &MyLoadWindow{} // 画面情報設定 MW := MainWindow{ AssignTo: &mw.MainWindow, // Widgetを実体に割り当て Title: "コンピュータの情報を取得中", Font: Font{PointSize: 12}, Layout: VBox{}, Icon: icon, OnBoundsChanged: func() {

        // 画面の縦と横のサイズを取得
        scrWidth := win.GetSystemMetrics(win.SM_CXSCREEN)
        scrHeight := win.GetSystemMetrics(win.SM_CYSCREEN)

        // 描画開始位置を設定
        mw.MainWindow.SetBounds(walk.Rectangle{
            X:      int((scrWidth - sizeX) / 2),
            Y:      int((scrHeight - sizeY) / 2),
            Width:  sizeX,
            Height: sizeY,
        })
    },

    Children: []Widget{ // ウィジェットを入れるスライス

        ProgressBar{
            AssignTo:    &mw.progressBar,
            MarqueeMode: true,
        },
    },
}

if _, err := MW.Run(); err != nil {
    common.ErrorLogger(prop.CheckerConfig.ManualSaveLogName, "画面表示エラー"+err.Error())
    return
}

`

I can set the position of the window in center. This code made me happy. But a new problem has occurred...

When the window is displayed, I cant move this window. I want to move freely.

Some one Please tell me about solution.