diff --git a/main.go b/main.go index ef2db67..93aa4a1 100644 --- a/main.go +++ b/main.go @@ -92,6 +92,11 @@ func wndProc(hwnd HWND, msg uint32, wParam WPARAM, lParam LPARAM) LRESULT { mm.PtMinTrackSize.X = 320 mm.PtMinTrackSize.Y = 240 return 0 + case WM_SIZE: + if wParam != SIZE_MINIMIZED { + resize(hwnd) + } + return 0 case WM_CLOSE: err := DestroyWindow(hwnd) if err != nil { @@ -119,6 +124,22 @@ func setFontAll(hwnd HWND, lParam LPARAM) (cont bool) { return true } +func resize(hwnd HWND) { + cr, err := GetClientRect(hwnd) + if err != nil { + fatalf("error getting window client rect: %v", err) + } + cr.Bottom -= 80 // Y position of listbox + cr.Bottom -= 20 // amount of pixels to leave behind + err = SetWindowPos(list, + HWND_TOP, + 20, 80, 100, int(cr.Bottom), + 0) + if err != nil { + fatalf("error resizing listbox: %v", err) + } +} + const className = "mainwin" func main() { @@ -271,6 +292,7 @@ func main() { if err != nil { fatalf("error setting font on controls: %v", err) } + resize(hwnd) _, err = ShowWindow(hwnd, nCmdShow) if err != nil { diff --git a/rectangles.go b/rectangles.go index d503edc..c0c29d5 100644 --- a/rectangles.go +++ b/rectangles.go @@ -12,3 +12,10 @@ type POINT struct { X int32 Y int32 } + +type RECT struct { + Left int32 + Top int32 + Right int32 + Bottom int32 +} diff --git a/windows.go b/windows.go index 0e4f7cc..2378aba 100644 --- a/windows.go +++ b/windows.go @@ -114,6 +114,39 @@ const ( COLOR_WINDOWTEXT = 8 ) +// SetWindowPos hWndInsertAfter values. +const ( + HWND_BOTTOM = HWND(1) + HWND_TOP = HWND(0) +) + +// SetWindowPos hWndInsertAfter values that Go won't allow as constants. +var ( + _HWND_NOTOPMOST = -2 + HWND_NOTOPMOST = HWND(_HWND_NOTOPMOST) + _HWND_TOPMOST = -1 + HWND_TOPMOST = HWND(_HWND_TOPMOST) +) + +// SetWindowPos uFlags values. +const ( + SWP_DRAWFRAME = 0x0020 + SWP_FRAMECHANGED = 0x0020 + SWP_HIDEWINDOW = 0x0080 + SWP_NOACTIVATE = 0x0010 + SWP_NOCOPYBITS = 0x0100 + SWP_NOMOVE = 0x0002 + SWP_NOOWNERZORDER = 0x0200 + SWP_NOREDRAW = 0x0008 + SWP_NOREPOSITION = 0x0200 + SWP_NOSENDCHANGING = 0x0400 + SWP_NOSIZE = 0x0001 + SWP_NOZORDER = 0x0004 + SWP_SHOWWINDOW = 0x0040 + SWP_ASYNCWINDOWPOS = 0x4000 + SWP_DEFERERASE = 0x2000 +) + // ShowWindow settings. const ( SW_FORCEMINIMIZE = 11 @@ -134,7 +167,9 @@ const ( var ( createWindowEx = user32.NewProc("CreateWindowExW") destroyWindow = user32.NewProc("DestroyWindow") + getClientRect = user32.NewProc("GetClientRect") enumChildWindows = user32.NewProc("EnumChildWindows") + setWindowPos = user32.NewProc("SetWindowPos") showWindow = user32.NewProc("ShowWindow") ) @@ -188,6 +223,33 @@ func EnumChildWindows(hWndParent HWND, lpEnumFunc WNDENUMPROC, lParam LPARAM) (e return nil } +// TODO return the rect itself? +func GetClientRect(hWnd HWND) (lpRect *RECT, err error) { + lpRect = new(RECT) + r1, _, err := getClientRect.Call( + uintptr(hWnd), + uintptr(unsafe.Pointer(lpRect))) + if r1 == 0 { // failure + return nil, err + } + return lpRect, nil +} + +func SetWindowPos(hWnd HWND, hWndInsertAfter HWND, X int, Y int, cx int, cy int, uFlags uint32) (err error) { + r1, _, err := setWindowPos.Call( + uintptr(hWnd), + uintptr(hWndInsertAfter), + uintptr(X), + uintptr(Y), + uintptr(cx), + uintptr(cy), + uintptr(uFlags)) + if r1 == 0 { // failure + return err + } + return nil +} + // TODO figure out how to handle errors func ShowWindow(hWnd HWND, nCmdShow int) (previouslyVisible bool, err error) { r1, _, _ := showWindow.Call(