zoukankan      html  css  js  c++  java
  • Go获取Windows下的窗口

    Go调用Windows下的DLL

    package main
    
    import (
    	"fmt"
    	"syscall"
    	"unsafe"
    )
    
    var (
    	kernel32DLL = syscall.MustLoadDLL("User32.dll")
    	procEnumWindows = kernel32DLL.MustFindProc("EnumWindows")
    )
    
    func StringToCharPtr(str string) *uint8 {
    	chars := append([]byte(str), 0)
    	return &chars[0]
    }
    
    // 回调函数,用于EnumWindows中的回调函数,第一个参数是hWnd,第二个是自定义穿的参数
    func AddElementFunc(hWnd syscall.Handle, hWndList *[]syscall.Handle) uintptr {
    	*hWndList = append(*hWndList, hWnd)
    	return 1
    }
    
    // 获取桌面下的所有窗口句柄,包括没有Windows标题的或者是窗口的。
    func GetDesktopWindowHWND() {
    	var hWndList []syscall.Handle
    	hL := &hWndList
    	r1, _, err := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(syscall.NewCallback(AddElementFunc)), uintptr(unsafe.Pointer(hL)), 0)
    	if err != 0 {
    		fmt.Println(err)
    	}
    	fmt.Println(r1)
    	fmt.Println(hWndList)
    }
    
    func main() {
    	GetDesktopWindowHWND()
    }
    

    关于syscall.Syscall中,第一个参数是DLL加载进来函数的指针,第二个是参数的个数,后边三个是参数,没有的话用0代替,其他几个同理。

  • 相关阅读:
    laravel底层源码解析:pipeline,db,console
    composer命令清单
    composer使用笔记
    git常见问题
    JS阻止冒泡和取消默认事件(默认行为)
    vue项目构建:vue-cli+webpack常用配置
    MVC和三层架构
    SSM框架初始配置
    Java对象间的关系
    Spring框架
  • 原文地址:https://www.cnblogs.com/linga/p/13589981.html
Copyright © 2011-2022 走看看