英文:
How to subclass a window in Windows? (using Go)
问题
我想要对实际窗口进行子类化,以便在其大小发生变化时进行检测。
以下是相关代码,我尝试使用CallWindowProcW和SetWindowLongW对其进行子类化,但是当我最大化窗口时没有显示任何消息,所以我猜测我可能错误地设置了其中一些过程。如何做到这一点?
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 {
}
}
英文:
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 {
}
}
答案1
得分: 2
我对winapi了解不多,但是你的代码似乎与go-winapi
包装器的一个示例非常相似。
使用这个包装器库,这个修改后的版本对我来说似乎可以工作:
(完整代码)
snip
<!-- language: lang-go -->
// 窗口过程
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!")
}
}
// 调用原始过程
return winapi.CallWindowProc(uintptr(oldWndProc), hwnd, msg, wparam, lparam)
}
我相信你可以查看那个包装器库并找到更直接的方法来实现它。
英文:
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:
snip
<!-- language: lang-go -->
// 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.
答案2
得分: 0
你的代码写着:
SetWindowLongW(syscall.Stdin, GWLP_WNDPROC, syscall.NewCallback(windowProc))
但是为什么你把syscall.Stdin传递给SetWindowLongW呢?你不应该传递窗口句柄吗?
Alex
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论