您的位置:首页 > 其它

autoIt DllCall 如何获得句柄和对窗口操作

2010-07-15 16:46 1236 查看
DllCall (部分引用他人blog)

调用指定DLL(动态链接库)文件里面的函数。

DllCall ( "dll", "返回值类型", "函数名称" [, "类型1", 参数1[, "类型n", 参数n]] )

参数

dll要使用的 DLL 文件名,例如 "user32.dll"。也可使用由 DllOpen 获得的句柄(请查看下面的注意部分)。
返回值类型函数的返回值类型(请查看下面的注意部分)。
函数名称调用DLL文件里面的函数名称, 如. "MessageBox" 或者使用序号, 如. 62
类型[可选] 参数的数据类型(请查看下面的注意部分).
参数[可选] 实参(请查看下面的注意部分)。
返回值

成功:@error = 0.
失败:@error = 1 不能使用 DLL 文件,
@error = 2 未知的 "返回值类型",
@error = 3 没有在DLL文件里面找到所指定的"函数名称".
查看下面的注意.

若调用函数失败则 @error 将被设为 1,否则将返回一个含有函数返回值及参数副本(包括函数已修改的参数)的数组。
$return[0] = 函数的返回值
$return[1] = 参数1
$return[2] = 参数2
...
$return
= 参数n

注意

若给定的是 DLL 文件名则在调用函数时该 DLL 文件将被自动加载并在结束调用后被关闭。若需自己控制 DLL 的加载及卸载,请使用 DllOpen 和 DllClose 函数(执行相应操作)并在调用本函数时使用句柄而不是文件名。

默认状态下, AutoIt 使用 'stdcall' 调用方法. 要使用 'cdecl' 调用方法,请在返回值类型后面加上 ':cdecl'.
DllCall("SQLite.dll", "int:cdecl", "sqlite3_open", "str", $sDatabase_Filename , "long_ptr", 0).

获得最前端活动窗口的句柄:
Func _My_WinAPI_GetForegroundWindow()
Local $aResult
$aResult = DllCall("User32.dll", "hwnd", "GetForegroundWindow")
Return $aResult[0]
EndFunc ;====>get the top windows

Func _My_WinAPI_IsWindowEnabled($hWnd)
Local $iResult
$iResult = DllCall("user32.dll", "int", "IsWindowEnabled", "hwnd", $hWnd)
If @error Or Not IsArray($iResult) Then Return SetError(-1, -1, False)
If $iResult[0] = 0 Then
Return False
Else
Return True
EndIf
EndFunc ;=====>check if window is enabled

向dll文件发送消息, 如单击,双击等对控件进行操作
Func WbxUtilClickButton($hBtnWnd, $hTargetWnd = 0, $nNotifyCmd = 0) ; BN_CLICKED = 0
Const $WM_COMMAND = 0x0111
Local $nBtnId = _WinAPI_GetWindowLong ($hBtnWnd,$GWL_ID)

If $hTargetWnd = 0 Then
$hTargetWnd = _WinAPI_GetParent($hBtnWnd)
If $hTargetWnd = 0 Then
$hTargetWnd = _My_WinAPI_GetActiveWindow()
EndIf
EndIf
If $hTargetWnd = 0 Then
Return SetError(-1,-1,-1)
EndIf
Local $wParam = $nNotifyCmd * 0x10000 + $nBtnId
Return _My_WinAPI_SendMessage($hTargetWnd,$WM_COMMAND,$wParam,$hBtnWnd)
EndFunc
Func _My_WinAPI_SendMessage($hWnd, $iMsg, $iwParam, $ilParam)
; Maybe we should call _SendMessage directly
Local $aResult

$aResult = DllCall("User32.dll", "int", "SendMessageA", "hwnd", $hWnd, "int", $iMsg, "int", $iwParam, "int", $ilParam)
Return $aResult[0]
EndFunc

;你也可以调用下面函数来对控件发送操作消息
Func _My_WinAPI_SendMessageW($hWnd, $iMsg, $iwParam, $ilParam)
Local $aResult

$aResult = DllCall("User32.dll", "int", "SendMessageW", "hwnd", $hWnd, "int", $iMsg, "int", $iwParam, "int", $ilParam)
Return $aResult[0]
EndFunc

您可以按需传递不限数量的"类型"和"参数"。请参考下面的示例。

有效的类型为:

类型详细信息
none没有值 (仅用作无返回值函数的返回类型,这点跟C语言类似)
short16 位整数
ushort无符号16 位整数
dword32 位整数
int32 位整数
long32 位整数
udword无符号32 位整数(无正负号)
uint无符号32 位整数(无正负号)
short_ptr指向 16 位整型的指针(a pointer to a 16 bit integer)
int_ptr指向 32 位整型的指针(a pointer to a 32 bit integer)
long_ptr指向 32 位整型的指针(a pointer to a 32 bit integer)
str字符串(不能超过65536)
wstr宽字符串(在调用函数时会转换为 ANSI 字符串或由 ANSI 字符串转换为宽字符串),不能超过 65536
hwnd一个窗口句柄
ptr通用指针(void *)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: