LoginSignup
4
2

More than 3 years have passed since last update.

眺めて覚えるGo言語 その4 Windows10 GUI

Posted at

GO は、サーバー用言語だからGUIは、HTMLで十分言う考え方がある。

だけどGUI作りたい。

WALKを使えば簡単に作れます。
image.png
こんな感じのGUI
ライブラリのありか!
image.png

インストール

C:\Users\hirat\go-work\gui> go get github.com/lxn/walk

つぎにテストソース

test.go
package main

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

func main() {
    var inTE, outTE *walk.TextEdit

    MainWindow{
        Title:   "Hello Golang GUI",
        MinSize: Size{320, 240},
        Layout:  VBox{},
        Children: []Widget{
            HSplitter{
                Children: []Widget{
                    TextEdit{AssignTo: &inTE},
                    TextEdit{AssignTo: &outTE, ReadOnly: true},
                },
            },
            PushButton{
                Text: "クリック",
                OnClicked: func() {
                    outTE.SetText(strings.ToUpper(inTE.Text()))
                },
            },
        },
    }.Run()
}

つぎにマニフェスト(適当なWindows manifest)

test.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="HelloWord" type="win32"/>
    <dependency>
        <dependentAssembly>
            <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
        </dependentAssembly>
    </dependency>
    <application xmlns="urn:schemas-microsoft-com:asm.v3">
        <windowsSettings>
            <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
            <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
        </windowsSettings>
    </application>
</assembly>

次に、次のようにrsrcツールを使用してマニフェストをコンパイルします。

go get github.com/akavel/rsrc

rsrc -manifest test.manifest -o rsrc.syso

この時に

    rsrc.syso
    test.go
    test.manifest

ファイルがありります。

go build
go build -ldflags="-H windowsgui"
C:\Users\hirat\go-work\gui>tree /f
C:.
    gui.exe
    rsrc.syso
    test.go
    test.manifest

gui.exeは、C:\Users\hirat\go-work\gui作成されダブルクリックで実行できます。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2