1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Go で GUI を表示する(RegisterClassEx/CreateWindowEx/GetMessage)

Last updated at Posted at 2020-04-24

Go で Win32 を呼び出すコードを書き始めている。
とりあえずウィンドウを表示できたので、後の人のために残しておく。

main.go
package main

import (
	"syscall"
	"unsafe"

	"github.com/blono/win" // Fork of lxn/win see https://qiita.com/manymanyuni/items/c41f5bf0fd141e299336
)

func NewUTF16(s string) *uint16 {
	result, _ := syscall.UTF16PtrFromString(s)

	return result
}

func onMessage(window win.HWND, msg uint32, wParam uintptr, lParam uintptr) uintptr {
	switch msg {
	case win.WM_CLOSE:
		win.DestroyWindow(window)
	case win.WM_DESTROY:
		win.PostQuitMessage(0)
	default:
		return win.DefWindowProc(window, msg, wParam, lParam)
	}

	return 0
}

func main() {
	windowClass := win.WNDCLASSEX{
		LpfnWndProc:    syscall.NewCallback(onMessage),
		HInstance:   win.HINSTANCE(0),
		LpszClassName:  winut.NewUTF16("test"),
	}
	windowClass.CbSize = uint32(unsafe.Sizeof(windowClass))

	win.RegisterClassEx(&windowClass)

	window := win.CreateWindowEx(0, winut.NewUTF16("test"), winut.NewUTF16("test-window"),
		win.WS_OVERLAPPEDWINDOW | win.WS_VISIBLE, 100, 100, 500, 500, win.HWND(0), win.HMENU(0), win.HINSTANCE(0), nil)

	win.ShowWindow(window, win.SWP_SHOWWINDOW)
	win.UpdateWindow(window)

	var msg win.MSG

	for win.GetMessage(&msg, 0, 0, 0) > 0 {
		win.TranslateMessage(&msg)
		win.DispatchMessage(&msg)
	}
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?