背景
需要用AutoIt3控制音乐播放器,首先要获取音乐的窗口句柄
问题
解决办法
自己写一个_WinGetHandleByPnmAndCls方法,使用进程名(Music.exe)和窗口Class(TXGuiFoundation)获得窗口句柄
代码如下
#include
#include
#include
Local $hWnd = _WinGetHandleByPnmAndCls("Music.exe", "TXGuiFoundation") If Not $hWnd Then MsgBox($MB_SYSTEMMODAL, "", "窗口没找到") Exit EndIf WinActivate($hWnd) ; 根据pname和class获取窗口句柄,找不到则返回0 Func _WinGetHandleByPnmAndCls($pname, $class) ; 根据进程名查找进程id Local $pid = ProcessExists($pname) ; 如果进程存在,继续 If $pid Then return _WinGetHandleByPidAndCls($pid, $class) Else Return 0 EndIf EndFunc ; 根据pid和class获取窗口句柄,找不到则返回0 Func _WinGetHandleByPidAndCls($pid, $class) ; 这里使用枚举所有顶层窗口方法,WinList方法会返回大量隐藏窗口 Local $winArr = _WinAPI_EnumWindowsTop() ; 遍历所有窗口,进程id与指定进程id比较 For $i=1 To $winArr[0][0] If $pid=WinGetProcess($winArr[$i][0]) And $winArr[$i][1]=$class Then ; 一个进程会有多个窗口,所以要用class来筛选 return $winArr[$i][0] EndIf Next Return 0 EndFunc
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213303.html原文链接:https://javaforall.net
