VB获取外部程序的窗体信息

VB如何获取外部程序的窗体的所有信息,包括控件句柄,类型呢。... VB如何获取外部程序的窗体的所有信息,包括控件句柄,类型呢。 展开
 我来答
一碗氮青47
2012-07-06 · TA获得超过137个赞
知道小有建树答主
回答量:89
采纳率:0%
帮助的人:91.7万
展开全部
’‘新建Command1,Text1,List1
''窗体代码
Private Sub Command1_Click()
Dim FormName As String
FormName = Text1
Dim t As Long
Dim Hwd As Long
Dim FormHwd As Long
List1.Clear
FormHwd = F_FindForm(FormName)
If FormHwd = 0 Then Exit Sub
S_GetAllCon (FormHwd)
For t = 1 To CollCon.Count
Hwd = CollCon.Item(t)
'''列出所有控件的句柄,类型,及内容
List1.AddItem t & "..." & Hwd & "..." & F_GetClass(Hwd) & "..." & F_GetText(Hwd)
Next
end sub
'''模块代码F.bas

Option Explicit

Public Enum E_ActionType
E_ActionType_None
E_ActionType_填入文本框
E_ActionType_点击按钮
E_ActionType_获取焦点
E_ActionType_填入组合框
E_ActionType_查找窗体
E_ActionType_手动暂停
E_ActionType_获取文本框
End Enum

Public Enum E_CID
E_CID_名称 = 1
E_CID_父窗体名称 = 2
E_CID_步骤类型 = 3
E_CID_控件ID = 4
E_CID_参数 = 5
End Enum

Public NowWindow As String
Public NowAction As Long
Public Pause As Boolean

Public TempStr As String

Public Arr()
'步骤名称
'步骤父窗体名称
'步骤动作 1 填入 2点击 3焦点
'控件ID
'步骤参数

Sub AddAction(Name As String, _
Optional Father As String, _
Optional Action As E_ActionType, _
Optional ID As Long, _
Optional Para As String)
ReDim Preserve Arr(0 To 10, 0 To UBound(Arr, 2) + 1)

Arr(1, UBound(Arr, 2)) = Name
Arr(2, UBound(Arr, 2)) = Father
Arr(3, UBound(Arr, 2)) = Action
Arr(4, UBound(Arr, 2)) = ID
Arr(5, UBound(Arr, 2)) = Para
End Sub

Function ExecuteAction() As Long
Dim Action As E_ActionType

Action = Arr(3, NowAction)

Dim FormName As String
Dim FormHwd As Long
Dim Hwd As Long
Dim ID As Long
Dim Para As String

If Action = E_ActionType_查找窗体 Then
FormName = Arr(5, NowAction)
If F.F_FindForm(FormName) = 0 Then
MsgBox "未找到窗体:" & Arr(5, NowAction)
ExecuteAction = 0
Exit Function
End If
ExecuteAction = 1
End If

If Action = E_ActionType_填入文本框 Then
FormName = Arr(2, NowAction)
FormHwd = F.F_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If

ID = Arr(4, NowAction)
Hwd = F.F_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If

Para = Arr(5, NowAction)
Call SendMessage(Hwd, WM_SETTEXT, 0, ByVal Para)

' If F.F_GetText(Hwd) <> Para Then
' MsgBox "验证失败"
' ExecuteAction = 0
' Exit Function
' End If
' End If

ExecuteAction = 1
End If

If Action = E_ActionType_获取文本框 Then
FormName = Arr(2, NowAction)
FormHwd = F.F_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If

ID = Arr(4, NowAction)
Hwd = F.F_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If
TempStr = F.F_GetText(Hwd)
ExecuteAction = 1
End If

If Action = E_ActionType_点击按钮 Then

FormName = Arr(2, NowAction)
FormHwd = F.F_FindForm(FormName)
If FormHwd = 0 Then
MsgBox "未找到父窗体:" & Arr(2, NowAction)
ExecuteAction = 0
Exit Function
End If

ID = Arr(4, NowAction)

Hwd = F.F_FindByID(FormHwd, ID)
If Hwd = 0 Then
MsgBox "未找到控件ID:" & ID
ExecuteAction = 0
Exit Function
End If

'F.F_Click (Hwd)
Call SendMessage(Hwd, BM_CLICK, 0, 0)
ExecuteAction = 1
End If

If Action = E_ActionType_手动暂停 Then
ExecuteAction = 2
End If

If Action = E_ActionType_获取焦点 Then
ExecuteAction = 2
End If

End Function

Function F_FindForm(Name As String, Optional Class As String = vbNullString, Optional Father As Long = 0, Optional Start As Long = 0) As Long
F_FindForm = FindWindowEx(Father, Start, Class, Name)
End Function

Function F_FindByID(Optional Father As Long = 0, Optional ID As Long = 1, Optional Class As String = vbNullString) As Long
Dim t As Long, p As Long
Dim Class1 As String * 255
Dim Class2 As String
F_FindByID = 0
For t = CollCon.Count To 1 Step -1
CollCon.Remove t
Next
EnumChildWindows Father, AddressOf EnumChildWindowsProc, ByVal 0&

If Class = vbNullString Then '任意控件
If ID > CollCon.Count Then
F_FindByID = 0
Exit Function
End If

F_FindByID = CollCon.Item(ID)

Else '制定控件
p = 0
For t = 1 To CollCon.Count
Call GetClassNameA(CollCon.Item(t), Class1, 255)
Class2 = Replace(Class1, Chr(0), "")
If Class = Class2 Then p = p + 1
If p = ID Then
F_FindByID = CollCon.Item(t)
Exit Function
End If
Next
End If
End Function

Sub S_GetAllCon(Optional Father As Long = 0)
Dim t As Long, p As Long
For t = CollCon.Count To 1 Step -1
CollCon.Remove t
Next
EnumChildWindows Father, AddressOf EnumChildWindowsProc, ByVal 0&

End Sub

Function F_GetClass(Hwd As Long) As String

Dim Class1 As String * 255
Call GetClassNameA(Hwd, Class1, 255)
F_GetClass = Replace(Class1, Chr(0), "")

End Function

Function F_GetText(Hwd As Long) As String
'Dim Text1 As String * 2550
'Call GetWindowText(Hwd, Text1, 2550)
'F_GetText = Replace(Text1, Chr(0), "")

Dim Text1 As String * 2550

Call SendMessage(Hwd, WM_GETTEXT, 2550, ByVal Text1)

F_GetText = Replace(Trim(Text1), Chr(0), "")

End Function

Function F_SetText(Hwd As Long, Str As String) As Long
'F_SetText = SetWindowText(Hwd, Str)

F_SetText = SendMessage(Hwd, WM_SETTEXT, 0, ByVal Str)

End Function

Function F_Click(Hwd As Long) As Long
'F_Click = SendMessage(Hwd, WM_LBUTTONDOWN, 0, 0)
'F_Click = SendMessage(Hwd, WM_LBUTTONUP, 0, 0)
'F_Click = SendMessage(Hwd, BM_CLICK, 0, 0)
'F_Click = SendMessage(Hwd, BM_CLICK, 0, 0)
'Call SendMessage(Hwd, BM_CLICK, 0, 0)
End Function
浮旻骞98
2012-07-08 · TA获得超过563个赞
知道答主
回答量:110
采纳率:0%
帮助的人:69.5万
展开全部
用api函数
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式