梦想吧技术论坛's Archiver

似水年华 发表于 2007-5-2 09:03 PM

Autoit使用DllCall列出当前所有窗体

这个程序可以遍历当前窗体,并查找其标题,用消息框显示出来。
子程序是Larry大师的作品,可以做为功能子程序来调用。
尤其值得大家学习的是使用了DllCall,它的使用对我们还比较新鲜,请仔细学习。

[code]
AutoItSetOption("WinTitleMatchMode", 4);2 = Match any substring in the title
$h = GetWindowHandles()
$dataout = ""
For $i = 1 To $h[0]
    $title = WinGetTitle("handle=" & $h[$i])
    $dataout = $dataout & $h[$i] & "--->" & $title & @CRLF
Next
MsgBox(1, "Windows", $dataout)
Func GetWindowHandles()
    $GW_HWNDNEXT = 2
    $GW_CHILD = 5
    Dim $List[1]
    $List[0] = 0
    $x = DllCall("user32.dll", "hwnd", "GetDesktopWindow")
    If @error Then Return $List
    $x = DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $x[0], "int", $GW_CHILD)
    If @error Then Return $List
    While 1
        $x = DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $x[0], "int", $GW_HWNDNEXT)
        If @error Then ExitLoop
        If String($x[0]) = "00000000" Then ExitLoop
        If BitAND(WinGetState($x[0]), 2) Then
            $ub = UBound($List)
            ReDim $List[$ub + 1]
            $List[$ub] = $x[0]
            $List[0] = $ub
        EndIf
    WEnd
    Return $List
EndFunc    ;==>GetWindowHandles<!--/code0-->
[/code]

下面是另一个调用这个程序的应用,对所有当前窗体设置透明度。
注意使用这个程序后,自己修改透明度好再恢复。


[code]
$h = GetWindowHandles()
For $i = 1 To $h[0]
    WinSetTrans($h[$i], "", 100)
Next
Func GetWindowHandles()
    $GW_HWNDNEXT = 2
    $GW_CHILD = 5
    Dim $List[1]
    $List[0] = 0
    $x = DllCall("user32.dll", "hwnd", "GetDesktopWindow")
    If @error Then Return $List
    $x = DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $x[0], "int", $GW_CHILD)
    If @error Then Return $List
    While 1
        $x = DllCall("user32.dll", "hwnd", "GetWindow", "hwnd", $x[0], "int", $GW_HWNDNEXT)
        If @error Then ExitLoop
        If String($x[0]) = "00000000" Then ExitLoop
        If BitAND(WinGetState($x[0]), 2) Then
            $ub = UBound($List)
            ReDim $List[$ub + 1]
            $List[$ub] = $x[0]
            $List[0] = $ub
        EndIf
    WEnd
    Return $List
EndFunc    ;==>GetWindowHandles<!--/code1-->
[/code]

DllCall命令,是调用dll动态连接库里已有的功能函数。其格式是
DllCall("dll文件", "返回值类型", "功能", "参数类型", 参数)
虽然帮助文件里在介绍格式时,没有说明是否可以有省略的参数,但从上面的例子中一句
$x = DllCall("user32.dll", "hwnd", "GetDesktopWindow")
可以看出,在特定使用时,可以省略后两项。
DllCall的使用是要看具体的dll和dll中具体的功能及使用方法来定的。我没有研究我user32.dll里的功能,但从上面的例子来看,可以简单解释一下。
在user32.dll中,有函数GetDesktopWindow和GetWindow
GetDesktopWindow的功能是取得桌面的句柄
GetWindow的功能是取得窗体的句柄,其中,在赋整形值5时,返回第一个窗体的句柄;在赋整形值2时,返回下一个窗体的句柄。这点比较类型FileFindFirstFile和FileFindNextFile
用循环的方式取得所有窗体的句柄,直到String ($x[0]) = "00000000"。
并非所有窗体都是可见的,我们需要的只是可见的,所以BitAnd (WinGetState($x[0]), 2) ,过滤出可见的窗体。
最后用$title = WinGetTitle("handle=" & $h[$i]) 取得可见窗体的标题。

smilesail 发表于 2008-1-2 06:01 PM

学习一下   很有用  这样做出来的东西很漂亮

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.