user1243746
user1243746

Reputation:

How to subclass a window in Windows? (using Go)

I want to sub-class the actual window to detect when its size has changed.

This is the code relevant where I've tried to subclassing it, using CallWindowProcW and SetWindowLongW, but it does not show any message when I maximize the window, so I'm supposed I've set wrongly some of those procedures. How to do it?

var oldWindowProc uintptr

func windowProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) (rc uintptr) {
    switch msg {
    case WM_SIZE:
        fmt.Println("Size")
        if wparam == SIZE_MAXIMIZED {
            fmt.Println("Changed!")
        }
    default:
        return CallWindowProcW(oldWindowProc, hwnd, msg, wparam, lparam)
    }
    return 0
}

func main() {
    oldWindowProc, _ = SetWindowLongW(syscall.Stdin, GWLP_WNDPROC,
        syscall.NewCallback(windowProc))

    for {

    }
}

Upvotes: 1

Views: 383

Answers (2)

alex
alex

Reputation: 2248

You code says:

SetWindowLongW(syscall.Stdin, GWLP_WNDPROC, syscall.NewCallback(windowProc))

But why are you passing syscall.Stdin to SetWindowLongW? Aren't you suppose to supply window handle instead?

Alex

Upvotes: 0

jdi
jdi

Reputation: 92567

I don't know much about winapi, but it seems your code closely resembles an example of go-winapi wrapper

And using that wrapper lib, this modified version seems to work for me:

(Full code)

snip

// window procedure
func WndProc(hwnd winapi.HWND, msg uint32, wparam uintptr, lparam uintptr) uintptr {
    switch msg {
    case winapi.WM_SIZE:
        if wparam == SIZE_MAXIMIZED {
            fmt.Println("Changed!")
        }
    }
    // call original procedure
    return winapi.CallWindowProc(uintptr(oldWndProc), hwnd, msg, wparam, lparam)
}

I am sure you could look at that wrapper lib and derive the more direct way of doing it.

Upvotes: 2

Related Questions