douhan8430 2012-09-13 20:55
浏览 73
已采纳

如何在Windows中对窗口进行子类化? (使用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 {

    }
}
  • 写回答

2条回答 默认 最新

  • dongyi8795 2012-09-13 22:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真