使用Go控制另一个窗口

huangapple go评论75阅读模式
英文:

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.orggo-search.org上找到。在这个例子中,我使用了w32w32syscall(它提供了一些额外的函数):

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.

huangapple
  • 本文由 发表于 2015年4月4日 22:52:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/29447807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定