go语言 FindWindow
时间: 2023-02-06 12:30:41 浏览: 105
FindWindow
Go 语言中的 FindWindow 函数是用于查找窗口句柄的函数。它是由 Windows API 提供的,在 Go 中通过 syscall 包来调用。
用法示例:
```
package main
import (
"fmt"
"syscall"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
findWindow = user32.NewProc("FindWindowW")
getWindowTextLength = user32.NewProc("GetWindowTextLengthW")
getWindowText = user32.NewProc("GetWindowTextW")
getWindowRect = user32.NewProc("GetWindowRect")
getClientRect = user32.NewProc("GetClientRect")
getWindowThreadProcessId = user32.NewProc("GetWindowThreadProcessId")
isWindowVisible = user32.NewProc("IsWindowVisible")
)
func FindWindow(className, windowName string) (hwnd syscall.Handle, err error) {
var cname, wname *uint16
if className != "" {
cname, err = syscall.UTF16PtrFromString(className)
if err != nil {
return 0, err
}
}
if windowName != "" {
wname, err = syscall.UTF16PtrFromString(windowName)
if err != nil {
return 0, err
}
}
r1, _, e1 := syscall.Syscall(findWindow.Addr(), 2, uintptr(unsafe.Pointer(cname)), uintptr(unsafe.Pointer(wname)), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
hwnd = syscall.Handle(r1)
return
}
func main() {
hwnd, err := FindWindow("", "Calculator")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println("window handle:", hwnd)
}
```
在这个例子中,FindWindow 函数被用来查找一个没有类名(className 参数为空),名称为 "Calculator" 的窗口的句柄。如果找到了,就输出窗口句柄的值。
阅读全文