hakeの日記

Windows環境でプログラミングの勉強をしています。

Go言語 - WALKでGUI - MessageBox

MessageBoxの使用例。
ボタンクリックでMessageBoxを作成し表示させています。
4番目の引数でメッセージボックスのスタイルを指定しています。また各々のスタイルのボタン種類で戻り値が変わるので、戻り値に応じた処理が必要になります。


package main

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

import (
	"fmt"
	"os"
)

type MyMainWindow struct {
	*walk.MainWindow
	edit   *walk.TextEdit
}

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

	MW := MainWindow{
		AssignTo: &mw.MainWindow,
		Title: "MessageBoxテスト",
		MinSize: Size {150, 200},
		Size   : Size {300, 300},
		Layout: VBox {},
		Children: []Widget {
			TextEdit {
				AssignTo: &mw.edit,
			},
			PushButton {
				Text: "Open Message",
				OnClicked: mw.pbClicked,
			},
		},
	}

	if _, err := MW.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

/*
src\github.com\lxn\walk\messagebox.goより
	MsgBoxOK                MsgBoxStyle = win.MB_OK
	MsgBoxOKCancel          MsgBoxStyle = win.MB_OKCANCEL
	MsgBoxAbortRetryIgnore  MsgBoxStyle = win.MB_ABORTRETRYIGNORE
	MsgBoxYesNoCancel       MsgBoxStyle = win.MB_YESNOCANCEL
	MsgBoxYesNo             MsgBoxStyle = win.MB_YESNO
	MsgBoxRetryCancel       MsgBoxStyle = win.MB_RETRYCANCEL
	MsgBoxCancelTryContinue MsgBoxStyle = win.MB_CANCELTRYCONTINUE
	MsgBoxIconHand          MsgBoxStyle = win.MB_ICONHAND
	MsgBoxIconQuestion      MsgBoxStyle = win.MB_ICONQUESTION
	MsgBoxIconExclamation   MsgBoxStyle = win.MB_ICONEXCLAMATION
	MsgBoxIconAsterisk      MsgBoxStyle = win.MB_ICONASTERISK
	MsgBoxUserIcon          MsgBoxStyle = win.MB_USERICON
	MsgBoxIconWarning       MsgBoxStyle = win.MB_ICONWARNING
	MsgBoxIconError         MsgBoxStyle = win.MB_ICONERROR
	MsgBoxIconInformation   MsgBoxStyle = win.MB_ICONINFORMATION
	MsgBoxIconStop          MsgBoxStyle = win.MB_ICONSTOP
	MsgBoxDefButton1        MsgBoxStyle = win.MB_DEFBUTTON1
	MsgBoxDefButton2        MsgBoxStyle = win.MB_DEFBUTTON2
	MsgBoxDefButton3        MsgBoxStyle = win.MB_DEFBUTTON3
	MsgBoxDefButton4        MsgBoxStyle = win.MB_DEFBUTTON4
*/

/*
src\github.com\lxn\walk\dialog.goより
	DlgCmdNone     = 0
	DlgCmdOK       = win.IDOK
	DlgCmdCancel   = win.IDCANCEL
	DlgCmdAbort    = win.IDABORT
	DlgCmdRetry    = win.IDRETRY
	DlgCmdIgnore   = win.IDIGNORE
	DlgCmdYes      = win.IDYES
	DlgCmdNo       = win.IDNO
	DlgCmdClose    = win.IDCLOSE
	DlgCmdHelp     = win.IDHELP
	DlgCmdTryAgain = win.IDTRYAGAIN
	DlgCmdContinue = win.IDCONTINUE
	DlgCmdTimeout  = win.IDTIMEOUT
*/

func (mw *MyMainWindow) pbClicked() {

//	r := walk.MsgBox(mw, "タイトル", "メッセージ内容", walk.MsgBoxOK)        // 1:OK,x
	r := walk.MsgBox(mw, "タイトル", "メッセージ内容", walk.MsgBoxOKCancel)  // 1:OK 2:Cancel,x
//	r := walk.MsgBox(mw, "タイトル", "メッセージ内容", walk.MsgBoxAbortRetryIgnore)
//	r := walk.MsgBox(mw, "タイトル", "メッセージ内容", walk.MsgBoxIconInformation)
//	r := walk.MsgBox(mw, "タイトル", "メッセージ内容", walk.MsgBoxIconQuestion)

	s :="RetValue : " + fmt.Sprint(r) + "\r\n"
	mw.edit.AppendText(s)

	if r == walk.DlgCmdOK {
		mw.edit.AppendText("Click : OK\r\n")
	}
}