优化代码

This commit is contained in:
hero920103 committed 2026-10-28 08:47:09 +08:00
1 parent 3553790475
commit eda7e8bc96
23 files changed
+3113 -342

No files matched your search

+14
View File
@@ -16,6 +16,7 @@ var (
shcore = windows.NewLazySystemDLL("shcore.dll")
procFindWindowW = user32.NewProc("FindWindowW")
procFindWindowExW = user32.NewProc("FindWindowExW")
procGetWindowRect = user32.NewProc("GetWindowRect")
procSetWindowPos = user32.NewProc("SetWindowPos")
procGetCursorPos = user32.NewProc("GetCursorPos")
@@ -200,6 +201,19 @@ func FindWindowByTitle(title string) (syscall.Handle, error) {
return syscall.Handle(r), nil
}
// FindWindowByClass 按窗口类名查找顶层窗口句柄。
func FindWindowByClass(class string) (syscall.Handle, error) {
p, err := windows.UTF16PtrFromString(class)
if err != nil {
return 0, err
}
r, _, e := procFindWindowExW.Call(0, 0, uintptr(unsafe.Pointer(p)), 0)
if r == 0 {
return 0, fmt.Errorf("未找到类名为 %q 的窗口: %w", class, e)
}
return syscall.Handle(r), nil
}
// WindowRect 返回窗口在屏幕上的物理像素矩形。
func WindowRect(hwnd syscall.Handle) (Rect, error) {
var r nativeRect