user1340531
user1340531

Reputation: 740

Taking control of another window with Go

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.

Upvotes: 0

Views: 1965

Answers (1)

Caleb
Caleb

Reputation: 9458

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.

Upvotes: 4

Related Questions