英文:
Taking control of another window with Go
问题
我想知道是否有任何库可以帮助我控制另一个窗口。例如,如果用户正在运行calc.exe,我希望我的Go代码可以移动它、调整大小,甚至可能去掉它的边框,还可以附加其他内容,我不知道。
目前我只知道如何使用脚本语言(如AutoIt或AutoHotkey等)来实现这一点。
英文:
I'm wondering if there are any libraries that help me take control of another window. For example if user has calc.exe running, I'd like my go code to move it, resize it, maybe even remove it's frame, attach stuff to it, idk.
Right now I only know how to do it with scripting languages like autoit or autohotkey or whatnot.
答案1
得分: 4
是的,有几个库可以在godoc.org或go-search.org上找到。在这个例子中,我使用了w32和w32syscall(它提供了一些额外的函数):
package main
import (
"log"
"strings"
"syscall"
"github.com/AllenDang/w32"
"github.com/hnakamur/w32syscall"
)
func main() {
err := w32syscall.EnumWindows(func(hwnd syscall.Handle, lparam uintptr) bool {
h := w32.HWND(hwnd)
text := w32.GetWindowText(h)
if strings.Contains(text, "Calculator") {
w32.MoveWindow(h, 0, 0, 200, 600, true)
}
return true
}, 0)
if err != nil {
log.Fatalln(err)
}
}
这两个库只是简单地暴露了底层的win32 API,并进行了最小的封装,所以你需要阅读微软的相应文档来真正了解如何使用它们。
英文:
Yes there are several libraries which can be found using godoc.org or go-search.org. In this example I'm using w32 and w32syscall (which supplies some additional functions):
package main
import (
"log"
"strings"
"syscall"
"github.com/AllenDang/w32"
"github.com/hnakamur/w32syscall"
)
func main() {
err := w32syscall.EnumWindows(func(hwnd syscall.Handle, lparam uintptr) bool {
h := w32.HWND(hwnd)
text := w32.GetWindowText(h)
if strings.Contains(text, "Calculator") {
w32.MoveWindow(h, 0, 0, 200, 600, true)
}
return true
}, 0)
if err != nil {
log.Fatalln(err)
}
}
Both of these libraries are merely exposing the underlying win32 API with minimal wrapping, so you will have to read the corresponding documentation from Microsoft to really know how to use them.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论