Added a button to the test main window, including click handling.
This commit is contained in:
parent
ef5739229e
commit
6662e09c30
11
common.go
11
common.go
|
@ -13,6 +13,7 @@ var (
|
||||||
type HANDLE uintptr
|
type HANDLE uintptr
|
||||||
type HWND HANDLE
|
type HWND HANDLE
|
||||||
type HBRUSH HANDLE
|
type HBRUSH HANDLE
|
||||||
|
type HMENU HANDLE
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NULL = 0
|
NULL = 0
|
||||||
|
@ -25,6 +26,16 @@ type WPARAM uintptr
|
||||||
type LPARAM uintptr
|
type LPARAM uintptr
|
||||||
type LRESULT 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
|
// microsoft's header files do this
|
||||||
func MAKEINTRESOURCE(what uint16) uintptr {
|
func MAKEINTRESOURCE(what uint16) uintptr {
|
||||||
return uintptr(what)
|
return uintptr(what)
|
||||||
|
|
27
main.go
27
main.go
|
@ -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))
|
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 {
|
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:
|
case WM_CLOSE:
|
||||||
err := DestroyWindow(hwnd)
|
err := DestroyWindow(hwnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -34,7 +47,7 @@ func wndProc(hwnd HWND, msg uint32, wparam WPARAM, lparam LPARAM) LRESULT {
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
default:
|
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)
|
fatalf("major bug: forgot a return on wndProc for message %d", msg)
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
|
@ -86,6 +99,16 @@ func main() {
|
||||||
fatalf("error creating window: %v", err)
|
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)
|
_, err = ShowWindow(hwnd, nCmdShow)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatalf("error showing window: %v", err)
|
fatalf("error showing window: %v", err)
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
// 10 february 2014
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
// "syscall"
|
||||||
|
// "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Menu notifications.
|
||||||
|
const (
|
||||||
|
WM_COMMAND = 0x0111
|
||||||
|
)
|
|
@ -138,7 +138,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO use lpParam
|
// 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(
|
r1, _, err := createWindowEx.Call(
|
||||||
uintptr(dwExStyle),
|
uintptr(dwExStyle),
|
||||||
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpClassName))),
|
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpClassName))),
|
||||||
|
|
Loading…
Reference in New Issue