• 查看窗口名 调用dll setForegroundWindow


    package main
    
    import (
    	"fmt"
    	"log"
    	"syscall"
    	"unsafe"
    )
    
    var (
    	user32             = syscall.MustLoadDLL("user32.dll")
    	procEnumWindows    = user32.MustFindProc("EnumWindows")
    	procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
    )
    
    func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
    	r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
    	if r1 == 0 {
    		if e1 != 0 {
    			err = error(e1)
    		} else {
    			err = syscall.EINVAL
    		}
    	}
    	return
    }
    
    func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
    	r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
    	len = int32(r0)
    	if len == 0 {
    		if e1 != 0 {
    			err = error(e1)
    		} else {
    			err = syscall.EINVAL
    		}
    	}
    	return
    }
    
    func FindWindow(title string) (syscall.Handle, error) {
    	var hwnd syscall.Handle
    	cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
    		b := make([]uint16, 200)
    		_, err := GetWindowText(h, &b[0], int32(len(b)))
    		if err != nil {
    			// ignore the error
    			return 1 // continue enumeration
    		}
    		fmt.Printf("%s
    ", syscall.UTF16ToString(b))
    
    		if syscall.UTF16ToString(b) == title {
    			// note the window
    			hwnd = h
    			return 0 // stop enumeration
    		}
    		return 1 // continue enumeration
    	})
    	EnumWindows(cb, 0)
    	if hwnd == 0 {
    		return 0, fmt.Errorf("No window with title '%s' found", title)
    	}
    	return hwnd, nil
    }
    
    func main() {
    	//const title = "OpenTTD"
    	//const title = "FileZilla FTP Client"
    	const title = "OpenTTD"
    	h, err := FindWindow(title)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Printf("Found '%s' window: handle=0x%x
    ", title, h)
    }
    

      https://stackoverflow.com/questions/19436860/go-golang-trying-to-get-window-information-via-syscall-as-in-enumwindows-etc

    Go/Golang Trying to get window information via syscall. (As in EnumWindows, etc.) - Stack Overflow - Google Chrome
    任务管理器
    OpenTTD 1.3.0
    openttd.exe 属性
    filezilla.exe 属性
    OfficePowerManagerWindow
    game_manager [Y:devgame_manager] - C:Gosrcosexecexec.go - GoLand

    CandidateWindow
    Mode Indicator
    Mode Indicator
    电池指示器
    Network Flyout
    Mode Indicator
    Mode Indicator
    go_win_api [Y:devgo_win_api] - ...m.go - GoLand
    OpenTTD 1.3.0
    Found 'OpenTTD 1.3.0' window: handle=0x610d6
    CandidateWindow
    Mode Indicator
    Mode Indicator
    电池指示器
    Network Flyout
    Mode Indicator
    Mode Indicator
    go_win_api [Y:devgo_win_api] - ...m.go - GoLand
    OpenTTD 1.3.0
    Get Retrun Value Before setForegroundWindow Invoked: %!s(uintptr=1)
    

      

    package main
    
    import (
    	"fmt"
    	"log"
    	"syscall"
    	"unsafe"
    )
    
    var (
    	user32                 = syscall.MustLoadDLL("user32.dll")
    	procEnumWindows        = user32.MustFindProc("EnumWindows")
    	procGetWindowTextW     = user32.MustFindProc("GetWindowTextW")
    	user32_b, _            = syscall.LoadLibrary("user32.dll")
    	setForegroundWindow, _ = syscall.GetProcAddress(user32_b, "SetForegroundWindow")
    )
    
    func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
    	r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
    	if r1 == 0 {
    		if e1 != 0 {
    			err = error(e1)
    		} else {
    			err = syscall.EINVAL
    		}
    	}
    	return
    }
    
    func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
    	r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
    	len = int32(r0)
    	if len == 0 {
    		if e1 != 0 {
    			err = error(e1)
    		} else {
    			err = syscall.EINVAL
    		}
    	}
    	return
    }
    
    func FindWindow(title string) (syscall.Handle, error) {
    	var hwnd syscall.Handle
    	cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
    		b := make([]uint16, 200)
    		_, err := GetWindowText(h, &b[0], int32(len(b)))
    		if err != nil {
    			// ignore the error
    			return 1 // continue enumeration
    		}
    		fmt.Printf("%s
    ", syscall.UTF16ToString(b))
    
    		if syscall.UTF16ToString(b) == title {
    			// note the window
    			hwnd = h
    			return 0 // stop enumeration
    		}
    		return 1 // continue enumeration
    	})
    	EnumWindows(cb, 0)
    	if hwnd == 0 {
    		return 0, fmt.Errorf("No window with title '%s' found", title)
    	}
    	return hwnd, nil
    }
    
    func IntPtr(n int) uintptr {
    	return uintptr(n)
    }
    
    func abort(funcname string, err syscall.Errno) {
    	panic(funcname + " failed: " + err.Error())
    }
    
    func main() {
    	//const title = "OpenTTD"
    	//const title = "FileZilla FTP Client"
    	const title = "OpenTTD 1.3.0"
    	h, err := FindWindow(title)
    	if err != nil {
    		log.Fatal(err)
    	}
    	fmt.Printf("Found '%s' window: handle=0x%x
    ", title, h)
    
    	h, err = FindWindow(title)
    	if err != nil {
    		log.Fatal(err)
    	}
    
    	ret, _, callErr := syscall.Syscall(setForegroundWindow, 1, uintptr(h), IntPtr(0), IntPtr(0))
    	if callErr != 0 {
    		abort("Call setForegroundWindow", callErr)
    	}
    	fmt.Printf("Get Retrun Value Before setForegroundWindow Invoked: %s
    ", ret)
    }
    

      

    https://github.com/golang/go/wiki/WindowsDLLs

  • 相关阅读:
    (教程)怎么避免拼多多比价订单行为
    Ubuntu下搭建apache2+GGI环境
    搭建k8s
    我的2021年总结
    工作三年的一些感悟
    xshell6+xftp6免安装破解版
    centos7启动docker容器时提示Error response from daemon: Unknown runtime specified dockerrunc
    解决一个C#中定时任务被阻塞问题
    工程中实际问题解决两例——基于C#
    解决一次gitlab因异常关机导致启动失败
  • 原文地址:https://www.cnblogs.com/rsapaper/p/12197906.html
Copyright © 2020-2023  润新知