Added a button to the test main window, including click handling.

This commit is contained in:
Pietro Gagliardi 2014-02-10 04:59:39 -05:00
parent ef5739229e
commit 6662e09c30
4 changed files with 49 additions and 3 deletions

View File

@ -13,6 +13,7 @@ var (
type HANDLE uintptr
type HWND HANDLE
type HBRUSH HANDLE
type HMENU HANDLE
const (
NULL = 0
@ -25,6 +26,16 @@ type WPARAM uintptr
type LPARAM uintptr
type LRESULT uintptr
func (w WPARAM) LOWORD() uint16 {
// according to windef.h
return uint16(w & 0xFFFF)
}
func (w WPARAM) HIWORD() uint16 {
// according to windef.h
return uint16((w >> 16) & 0xFFFF)
}
// microsoft's header files do this
func MAKEINTRESOURCE(what uint16) uintptr {
return uintptr(what)

27
main.go
View File

@ -19,8 +19,21 @@ func fatalf(format string, args ...interface{}) {
panic(fmt.Sprintf("error trying to warn user of internal error: %v\ninternal error:\n%s", err, s))
}
func wndProc(hwnd HWND, msg uint32, wparam WPARAM, lparam LPARAM) LRESULT {
const (
IDC_BUTTON = 100
)
func wndProc(hwnd HWND, msg uint32, wParam WPARAM, lParam LPARAM) LRESULT {
switch msg {
case WM_COMMAND:
if wParam.LOWORD() == IDC_BUTTON {
if wParam.HIWORD() == BN_CLICKED {
MessageBox(hwnd, "clicked", "", MB_OK)
} else if wParam.HIWORD() == BN_DOUBLECLICKED {
MessageBox(hwnd, "double clicked", "", MB_OK)
}
}
return 0
case WM_CLOSE:
err := DestroyWindow(hwnd)
if err != nil {
@ -34,7 +47,7 @@ func wndProc(hwnd HWND, msg uint32, wparam WPARAM, lparam LPARAM) LRESULT {
}
return 0
default:
return DefWindowProc(hwnd, msg, wparam, lparam)
return DefWindowProc(hwnd, msg, wParam, lParam)
}
fatalf("major bug: forgot a return on wndProc for message %d", msg)
panic("unreachable")
@ -86,6 +99,16 @@ func main() {
fatalf("error creating window: %v", err)
}
_, err = CreateWindowEx(
0,
"BUTTON", "Click Me",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP,
20, 20, 100, 100,
hwnd, HMENU(IDC_BUTTON), hInstance, NULL)
if err != nil {
fatalf("error creating button: %v", err)
}
_, err = ShowWindow(hwnd, nCmdShow)
if err != nil {
fatalf("error showing window: %v", err)

12
menus.go Normal file
View File

@ -0,0 +1,12 @@
// 10 february 2014
package main
import (
// "syscall"
// "unsafe"
)
// Menu notifications.
const (
WM_COMMAND = 0x0111
)

View File

@ -138,7 +138,7 @@ var (
)
// TODO use lpParam
func CreateWindowEx(dwExStyle uint32, lpClassName string, lpWindowName string, dwStyle uint32, x int, y int, nWidth int, nHeight int, hwndParent HWND, hMenu HANDLE, hInstance HANDLE, lpParam interface{}) (hwnd HWND, err error) {
func CreateWindowEx(dwExStyle uint32, lpClassName string, lpWindowName string, dwStyle uint32, x int, y int, nWidth int, nHeight int, hwndParent HWND, hMenu HMENU, hInstance HANDLE, lpParam interface{}) (hwnd HWND, err error) {
r1, _, err := createWindowEx.Call(
uintptr(dwExStyle),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpClassName))),